You are here

public function MediaThumbnailManager::getPluginId in Media Thumbnails 8

Get the thumbnail plugin id for a media entity, if any.

Parameters

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

Return value

string|null Plugin id if there is a plugin, NULL otherwise

2 calls to MediaThumbnailManager::getPluginId()
MediaThumbnailManager::createThumbnail in src/Plugin/MediaThumbnailManager.php
Create a new media thumbnail.
MediaThumbnailManager::hasPlugin in src/Plugin/MediaThumbnailManager.php
Check if the media source has a thumbnail plugin.

File

src/Plugin/MediaThumbnailManager.php, line 238

Class

MediaThumbnailManager
Provides the Media thumbnail plugin manager.

Namespace

Drupal\media_thumbnails\Plugin

Code

public function getPluginId(MediaInterface $media) {
  if (!$this
    ->isLocal($media)) {
    return NULL;
  }
  $source = $media
    ->getSource()
    ->getConfiguration()['source_field'];
  try {
    $first = $media
      ->get($source)
      ->first();
    $file = $first ? $first
      ->getValue() : NULL;
  } catch (MissingDataException $e) {
    return NULL;
  }
  if (!isset($file['target_id'])) {
    return NULL;
  }

  /** @var \Drupal\file\Entity\File $file */
  $file = $this->container
    ->get('entity_type.manager')
    ->getStorage('file')
    ->load($file['target_id']);
  if (!$file) {
    return NULL;
  }
  $mime = $file
    ->getMimeType();
  return $this->plugins[$mime] ?? NULL;
}