Browse Source

Avoid ::referencedEntities() call when it is not expected to exist. (#923)

pull/924/head
Adam 2 years ago committed by GitHub
parent
commit
6f2955b061
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      src/MediaSource/MediaSourceService.php

18
src/MediaSource/MediaSourceService.php

@ -3,6 +3,7 @@
namespace Drupal\islandora\MediaSource; namespace Drupal\islandora\MediaSource;
use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
use Drupal\Core\File\FileSystemInterface; use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountInterface;
@ -113,8 +114,12 @@ class MediaSourceService {
* @param \Drupal\media\MediaInterface $media * @param \Drupal\media\MediaInterface $media
* Media whose source field you are searching for. * Media whose source field you are searching for.
* *
* @return \Drupal\file\FileInterface * @return \Drupal\file\FileInterface|\Drupal\Core\Entity\EntityInterface|false|null
* File if it exists * The first source entity if there is one, generally expected to be of
* \Drupal\file\FileInterface. Boolean FALSE if there was no such entity.
* NULL if the source field does not refer to Drupal entities (as in, the
* field is not a \Drupal\Core\Field\EntityReferenceFieldItemListInterface
* implementation).
* *
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/ */
@ -127,10 +132,13 @@ class MediaSourceService {
} }
// Get the file from the media. // Get the file from the media.
$files = $media->get($source_field)->referencedEntities(); $source_list = $media->get($source_field);
$file = reset($files); if ($source_list instanceof EntityReferenceFieldItemListInterface) {
$files = $source_list->referencedEntities();
return reset($files);
}
return $file; return NULL;
} }
/** /**

Loading…
Cancel
Save