You are here

protected function Media::loadThumbnail in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/media/src/Entity/Media.php \Drupal\media\Entity\Media::loadThumbnail()

Loads the file entity for the thumbnail.

If the file entity does not exist, it will be created.

Parameters

string $thumbnail_uri: (optional) The URI of the thumbnail, used to load or create the file entity. If omitted, the default thumbnail URI will be used.

Return value

\Drupal\file\FileInterface The thumbnail file entity.

File

core/modules/media/src/Entity/Media.php, line 182

Class

Media
Defines the media entity class.

Namespace

Drupal\media\Entity

Code

protected function loadThumbnail($thumbnail_uri = NULL) {
  $values = [
    'uri' => $thumbnail_uri ?: $this
      ->getDefaultThumbnailUri(),
  ];
  $file_storage = $this
    ->entityTypeManager()
    ->getStorage('file');
  $existing = $file_storage
    ->loadByProperties($values);
  if ($existing) {
    $file = reset($existing);
  }
  else {

    /** @var \Drupal\file\FileInterface $file */
    $file = $file_storage
      ->create($values);
    if ($owner = $this
      ->getOwner()) {
      $file
        ->setOwner($owner);
    }
    $file
      ->setPermanent();
    $file
      ->save();
  }
  return $file;
}