Skip to content

Commit

Permalink
Avoid ::referencedEntities() call when it is not expected to exist. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-vessey authored Jan 6, 2023
1 parent cefee61 commit 6f2955b
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/MediaSource/MediaSourceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Drupal\islandora\MediaSource;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Session\AccountInterface;
Expand Down Expand Up @@ -113,8 +114,12 @@ public function getSourceFieldName($media_type) {
* @param \Drupal\media\MediaInterface $media
* Media whose source field you are searching for.
*
* @return \Drupal\file\FileInterface
* File if it exists
* @return \Drupal\file\FileInterface|\Drupal\Core\Entity\EntityInterface|false|null
* 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
*/
Expand All @@ -127,10 +132,13 @@ public function getSourceFile(MediaInterface $media) {
}

// Get the file from the media.
$files = $media->get($source_field)->referencedEntities();
$file = reset($files);
$source_list = $media->get($source_field);
if ($source_list instanceof EntityReferenceFieldItemListInterface) {
$files = $source_list->referencedEntities();
return reset($files);
}

return $file;
return NULL;
}

/**
Expand Down

0 comments on commit 6f2955b

Please sign in to comment.