You are here

public function MediaThumbnailManager::createThumbnail in Media Thumbnails 8

Create a new media thumbnail.

Parameters

\Drupal\media\MediaInterface $media: The media object.

1 call to MediaThumbnailManager::createThumbnail()
MediaThumbnailManager::updateThumbnail in src/Plugin/MediaThumbnailManager.php
Update a media thumbnail.

File

src/Plugin/MediaThumbnailManager.php, line 73

Class

MediaThumbnailManager
Provides the Media thumbnail plugin manager.

Namespace

Drupal\media_thumbnails\Plugin

Code

public function createThumbnail(MediaInterface $media) {

  // Get a thumbnail plugin id for supported media types.
  if (!($plugin = $this
    ->getPluginId($media))) {
    return;
  }

  // Get the global configuration to pass it to the plugins.
  $config = $this->container
    ->get('config.factory')
    ->get('media_thumbnails.settings')
    ->get();

  // Create a plugin instance.
  try {

    /** @var \Drupal\media_thumbnails\Plugin\MediaThumbnailInterface $instance */
    $instance = $this
      ->createInstance($plugin, $config);
  } catch (PluginException $e) {
    return;
  }

  // Create the thumbnail file using the plugin.

  /** @var \Drupal\file\Entity\File $file */
  $file = $this
    ->getSource($media);
  if (!($file = $instance
    ->createThumbnail($file
    ->getFileUri()))) {
    return;
  }

  // Add this file to the media entity.
  $media
    ->set('thumbnail', $file);
}