function OEmbed::getMetadata

Same name in other branches
  1. 9 core/modules/media/src/Plugin/media/Source/OEmbed.php \Drupal\media\Plugin\media\Source\OEmbed::getMetadata()
  2. 8.9.x core/modules/media/src/Plugin/media/Source/OEmbed.php \Drupal\media\Plugin\media\Source\OEmbed::getMetadata()
  3. 11.x core/modules/media/src/Plugin/media/Source/OEmbed.php \Drupal\media\Plugin\media\Source\OEmbed::getMetadata()

Overrides MediaSourceBase::getMetadata

File

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

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, $media) ?: 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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.