You are here

public function VideoEmbedField::getMetadata in Video Embed Field 8.2

Gets the value for a metadata attribute for a given media item.

Parameters

\Drupal\media\MediaInterface $media: A media item.

string $attribute_name: Name of the attribute to fetch.

Return value

mixed|null Metadata attribute value or NULL if unavailable.

Overrides MediaSourceBase::getMetadata

File

modules/video_embed_media/src/Plugin/media/Source/VideoEmbedField.php, line 96

Class

VideoEmbedField
Provides media source plugin for video embed field.

Namespace

Drupal\video_embed_media\Plugin\media\Source

Code

public function getMetadata(MediaInterface $media, $attribute_name) {
  $url = $this
    ->getVideoUrl($media);
  switch ($attribute_name) {
    case 'default_name':
      if ($provider = $this->providerManager
        ->loadProviderFromInput($url)) {
        return $provider
          ->getName();
      }
      return parent::getMetadata($media, 'default_name');
    case 'id':
      if ($provider = $this->providerManager
        ->loadProviderFromInput($url)) {
        return $provider
          ->getIdFromInput($url);
      }
      return FALSE;
    case 'source':
    case 'source_name':
      $definition = $this->providerManager
        ->loadDefinitionFromInput($url);
      if (!empty($definition)) {
        return $definition['id'];
      }
      return FALSE;
    case 'image_local':
    case 'image_local_uri':
      $thumbnail_uri = $this
        ->getMetadata($media, 'thumbnail_uri');
      if (!empty($thumbnail_uri) && file_exists($thumbnail_uri)) {
        return $thumbnail_uri;
      }
      return parent::getMetadata($media, 'thumbnail_uri');
    case 'thumbnail_uri':
      if ($provider = $this->providerManager
        ->loadProviderFromInput($url)) {
        $provider
          ->downloadThumbnail();
        $thumbnail_uri = $provider
          ->getLocalThumbnailUri();
        if (!empty($thumbnail_uri)) {
          return $thumbnail_uri;
        }
      }
      return parent::getMetadata($media, 'thumbnail_uri');
  }
  return FALSE;
}