function OEmbedFormatter::viewElements
Same name in other branches
- 8.9.x core/modules/media/src/Plugin/Field/FieldFormatter/OEmbedFormatter.php \Drupal\media\Plugin\Field\FieldFormatter\OEmbedFormatter::viewElements()
- 10 core/modules/media/src/Plugin/Field/FieldFormatter/OEmbedFormatter.php \Drupal\media\Plugin\Field\FieldFormatter\OEmbedFormatter::viewElements()
- 11.x core/modules/media/src/Plugin/Field/FieldFormatter/OEmbedFormatter.php \Drupal\media\Plugin\Field\FieldFormatter\OEmbedFormatter::viewElements()
Overrides FormatterInterface::viewElements
File
-
core/
modules/ media/ src/ Plugin/ Field/ FieldFormatter/ OEmbedFormatter.php, line 159
Class
- OEmbedFormatter
- Plugin implementation of the 'oembed' formatter.
Namespace
Drupal\media\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$element = [];
$max_width = $this->getSetting('max_width');
$max_height = $this->getSetting('max_height');
foreach ($items as $delta => $item) {
$main_property = $item->getFieldDefinition()
->getFieldStorageDefinition()
->getMainPropertyName();
$value = $item->{$main_property};
if (empty($value)) {
continue;
}
try {
$resource_url = $this->urlResolver
->getResourceUrl($value, $max_width, $max_height);
$resource = $this->resourceFetcher
->fetchResource($resource_url);
} catch (ResourceException $exception) {
$this->logger
->error("Could not retrieve the remote URL (@url).", [
'@url' => $value,
]);
continue;
}
if ($resource->getType() === Resource::TYPE_LINK) {
$element[$delta] = [
'#title' => $resource->getTitle(),
'#type' => 'link',
'#url' => Url::fromUri($value),
];
}
elseif ($resource->getType() === Resource::TYPE_PHOTO) {
$element[$delta] = [
'#theme' => 'image',
'#uri' => $resource->getUrl()
->toString(),
'#width' => $max_width ?: $resource->getWidth(),
'#height' => $max_height ?: $resource->getHeight(),
];
}
else {
$url = Url::fromRoute('media.oembed_iframe', [], [
'query' => [
'url' => $value,
'max_width' => $max_width,
'max_height' => $max_height,
'hash' => $this->iFrameUrlHelper
->getHash($value, $max_width, $max_height),
],
]);
$domain = $this->config
->get('iframe_domain');
if ($domain) {
$url->setOption('base_url', $domain);
}
// Render videos and rich content in an iframe for security reasons.
// @see: https://oembed.com/#section3
$element[$delta] = [
'#type' => 'html_tag',
'#tag' => 'iframe',
'#attributes' => [
'src' => $url->toString(),
'frameborder' => 0,
'scrolling' => FALSE,
'allowtransparency' => TRUE,
'width' => $max_width ?: $resource->getWidth(),
'height' => $max_height ?: $resource->getHeight(),
'class' => [
'media-oembed-content',
],
],
'#attached' => [
'library' => [
'media/oembed.formatter',
],
],
];
// An empty title attribute will disable title inheritance, so only
// add it if the resource has a title.
$title = $resource->getTitle();
if ($title) {
$element[$delta]['#attributes']['title'] = $title;
}
CacheableMetadata::createFromObject($resource)->addCacheTags($this->config
->getCacheTags())
->applyTo($element[$delta]);
}
}
return $element;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.