function Media::loadThumbnail

Same name in other branches
  1. 9 core/modules/media/src/Entity/Media.php \Drupal\media\Entity\Media::loadThumbnail()
  2. 10 core/modules/media/src/Entity/Media.php \Drupal\media\Entity\Media::loadThumbnail()
  3. 11.x 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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.