Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Media.php

function 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 203

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;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal