public function CKEditorMediaEmbedFormatter::viewElements in CKEditor Media Embed Plugin 8
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 FormatterInterface::viewElements
File
- src/
Plugin/ Field/ FieldFormatter/ CKEditorMediaEmbedFormatter.php, line 78
Class
- CKEditorMediaEmbedFormatter
- Plugin implementation of the 'ckeditor_media_embed_link_formatter' formatter.
Namespace
Drupal\ckeditor_media_embed\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$element = [];
foreach ($items as $delta => $item) {
$url = $item
->getUrl();
$output = $this->embed
->getEmbedObject($url
->toUriString());
if (isset($output->html)) {
$element[$delta] = [
'#type' => 'inline_template',
'#template' => '{{ content|raw }}',
'#context' => [
'content' => $output->html,
],
];
}
else {
// If we didn't get an oembed response, just show the URL.
$element[$delta] = [
'#type' => 'link',
'#url' => $url,
'#title' => $url
->toString(),
];
}
}
return $element;
}