public function YouTubeThumbFormatter::viewElements in YouTube Field 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/ YouTubeThumbFormatter.php, line 87
Class
- YouTubeThumbFormatter
- Plugin implementation of the 'youtube_thumbnail' formatter.
Namespace
Drupal\youtube\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$element = [];
$entity = $items
->getEntity();
$image_link = $this
->getSetting('image_link');
// Check if the formatter involves a link.
if (!empty($image_link)) {
if ($image_link == 'content' && $entity
->hasLinkTemplate('canonical')) {
$url = $entity
->toUrl();
$url
->setOption('html', TRUE);
}
elseif ($image_link == 'youtube') {
$link_youtube = TRUE;
}
}
foreach ($items as $delta => $item) {
// If the thumbnail is linked to its youtube page, take the original url.
if (isset($link_youtube) && $link_youtube) {
$url = Url::fromUri($item->input);
$url
->setOption('html', TRUE);
}
$element[$delta] = [
'#theme' => 'youtube_thumbnail',
'#video_id' => $item->video_id,
'#entity_title' => $items
->getEntity()
->label(),
'#image_style' => $this
->getSetting('image_style'),
'#image_link' => isset($url) ? $url : '',
];
}
return $element;
}