public function MediaThumbnailFormatter::viewElements in Drupal 8
Same name and namespace in other branches
- 9 core/modules/media/src/Plugin/Field/FieldFormatter/MediaThumbnailFormatter.php \Drupal\media\Plugin\Field\FieldFormatter\MediaThumbnailFormatter::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 ImageFormatter::viewElements
File
- core/
modules/ media/ src/ Plugin/ Field/ FieldFormatter/ MediaThumbnailFormatter.php, line 132
Class
- MediaThumbnailFormatter
- Plugin implementation of the 'media_thumbnail' formatter.
Namespace
Drupal\media\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$media_items = $this
->getEntitiesToView($items, $langcode);
// Early opt-out if the field is empty.
if (empty($media_items)) {
return $elements;
}
$image_style_setting = $this
->getSetting('image_style');
/** @var \Drupal\media\MediaInterface[] $media_items */
foreach ($media_items as $delta => $media) {
$elements[$delta] = [
'#theme' => 'image_formatter',
'#item' => $media
->get('thumbnail')
->first(),
'#item_attributes' => [],
'#image_style' => $this
->getSetting('image_style'),
'#url' => $this
->getMediaThumbnailUrl($media, $items
->getEntity()),
];
// Add cacheability of each item in the field.
$this->renderer
->addCacheableDependency($elements[$delta], $media);
}
// Add cacheability of the image style setting.
if ($this
->getSetting('image_link') && ($image_style = $this->imageStyleStorage
->load($image_style_setting))) {
$this->renderer
->addCacheableDependency($elements, $image_style);
}
return $elements;
}