public function VarbaseOEmbedFormatter::viewElements in Varbase Media 8.7
Same name and namespace in other branches
- 9.0.x src/Plugin/Field/FieldFormatter/VarbaseOEmbedFormatter.php \Drupal\varbase_media\Plugin\Field\FieldFormatter\VarbaseOEmbedFormatter::viewElements()
Builds a renderable array for a field value.
Parameters
\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.
string $langcode: The language that should be used to render the field.
Return value
array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.
Overrides OEmbedFormatter::viewElements
File
- src/
Plugin/ Field/ FieldFormatter/ VarbaseOEmbedFormatter.php, line 31
Class
- VarbaseOEmbedFormatter
- Plugin implementation of the 'oembed' formatter for Varbase.
Namespace
Drupal\varbase_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;
}
$media = $item
->getEntity();
$provider = $media->field_provider->value;
$url = Url::fromRoute('media.oembed_iframe', [], [
'query' => [
'url' => $value,
'max_width' => $max_width,
'max_height' => $max_height,
'type' => "remote_video",
'provider' => strtolower($provider),
'hash' => $this->iFrameUrlHelper
->getHash($value, $max_width, $max_height, $provider),
],
]);
// 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,
'height' => $max_height,
'loading' => 'lazy',
'class' => [
'media-oembed-content',
],
],
'#attached' => [
'library' => [
'media/oembed.formatter',
],
],
];
}
return $element;
}