public function FileImageFormatter::viewElements in File Entity (fieldable files) 8.2
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
- src/
Plugin/ Field/ FieldFormatter/ FileImageFormatter.php, line 116
Class
- FileImageFormatter
- Implementation of the 'image' formatter for the file_entity files.
Namespace
Drupal\file_entity\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$file = $items
->getEntity();
$image_style_setting = $this
->getSetting('image_style');
// Collect cache tags to be added for each item in the field.
$cache_tags = [];
if (!empty($image_style_setting)) {
$image_style = $this->imageStyleStorage
->load($image_style_setting);
$cache_tags = $image_style
->getCacheTags();
}
$cache_tags = Cache::mergeTags($cache_tags, $file
->getCacheTags());
if (isset($image_style)) {
$elements[0] = [
'#theme' => 'image_style',
'#style_name' => $image_style_setting,
];
}
else {
$elements[0] = [
'#theme' => 'image',
];
}
$elements[0] += [
'#uri' => $file
->getFileUri(),
'#cache' => [
'tags' => $cache_tags,
],
];
foreach ([
'title',
'alt',
] as $element_name) {
$field_name = $this
->getSetting($element_name);
if ($field_name !== '_none' && $file
->hasField($field_name)) {
$elements[0]['#' . $element_name] = $file->{$field_name}->value;
}
}
return $elements;
}