You are here

public function OEmbed::getMetadata in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/media/src/Plugin/media/Source/OEmbed.php \Drupal\media\Plugin\media\Source\OEmbed::getMetadata()

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

core/modules/media/src/Plugin/media/Source/OEmbed.php, line 218

Class

OEmbed
Provides a media source plugin for oEmbed resources.

Namespace

Drupal\media\Plugin\media\Source

Code

public function getMetadata(MediaInterface $media, $name) {
  $media_url = $this
    ->getSourceFieldValue($media);

  // The URL may be NULL if the source field is empty, in which case just
  // return NULL.
  if (empty($media_url)) {
    return NULL;
  }
  try {
    $resource_url = $this->urlResolver
      ->getResourceUrl($media_url);
    $resource = $this->resourceFetcher
      ->fetchResource($resource_url);
  } catch (ResourceException $e) {
    $this->messenger
      ->addError($e
      ->getMessage());
    return NULL;
  }
  switch ($name) {
    case 'default_name':
      if ($title = $this
        ->getMetadata($media, 'title')) {
        return $title;
      }
      elseif ($url = $this
        ->getMetadata($media, 'url')) {
        return $url;
      }
      return parent::getMetadata($media, 'default_name');
    case 'thumbnail_uri':
      return $this
        ->getLocalThumbnailUri($resource) ?: parent::getMetadata($media, 'thumbnail_uri');
    case 'type':
      return $resource
        ->getType();
    case 'title':
      return $resource
        ->getTitle();
    case 'author_name':
      return $resource
        ->getAuthorName();
    case 'author_url':
      return $resource
        ->getAuthorUrl();
    case 'provider_name':
      $provider = $resource
        ->getProvider();
      return $provider ? $provider
        ->getName() : '';
    case 'provider_url':
      $provider = $resource
        ->getProvider();
      return $provider ? $provider
        ->getUrl() : NULL;
    case 'cache_age':
      return $resource
        ->getCacheMaxAge();
    case 'thumbnail_width':
      return $resource
        ->getThumbnailWidth();
    case 'thumbnail_height':
      return $resource
        ->getThumbnailHeight();
    case 'url':
      $url = $resource
        ->getUrl();
      return $url ? $url
        ->toString() : NULL;
    case 'width':
      return $resource
        ->getWidth();
    case 'height':
      return $resource
        ->getHeight();
    case 'html':
      return $resource
        ->getHtml();
    default:
      break;
  }
  return NULL;
}