public function ImageFormatterLinkToImageStyleFormatter::viewElements in Image formatter link to image style 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/ ImageFormatterLinkToImageStyleFormatter.php, line 210
Class
- ImageFormatterLinkToImageStyleFormatter
- Plugin implementation of the 'image_formatter_link_to_image_style' formatter.
Namespace
Drupal\image_formatter_link_to_image_style\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$files = $this
->getEntitiesToView($items, $langcode);
// Early opt-out if the field is empty.
if (empty($files)) {
return $elements;
}
$image_style_setting = $this
->getSetting('image_style');
// Collect cache tags to be added for each item in the field.
$base_cache_tags = [];
if (!empty($image_style_setting)) {
$image_style = $this->imageStyleStorage
->load($image_style_setting);
$base_cache_tags = $image_style
->getCacheTags();
}
foreach ($files as $delta => $file) {
$cache_tags = Cache::mergeTags($base_cache_tags, $file
->getCacheTags());
$cache_contexts = [
'url.site',
];
// Extract field item attributes for the theme function, and unset them
// from the $item so that the field template does not re-render them.
$item = $file->_referringItem;
$item_attributes = $item->_attributes;
unset($item->_attributes);
if (!empty($this
->getSetting('image_link_image_class'))) {
if (!isset($item_attributes['class'])) {
$item_attributes['class'] = [];
}
elseif (!is_array($item_attributes['class'])) {
$item_attributes['class'] = explode(' ', $item_attributes['class']);
}
$item_attributes['class'] = array_merge($item_attributes['class'], explode(' ', $this
->getSetting('image_link_image_class')));
}
if (!empty($this
->getSetting('image_link_style'))) {
$image_link_style = ImageStyle::load($this
->getSetting('image_link_style'));
$image_uri = $image_link_style
->buildUrl($file
->getFileUri());
}
else {
$image_uri = $file
->getFileUri();
}
$url = Url::fromUri(file_create_url($image_uri));
$url_attributes = [];
if (!empty($this
->getSetting('image_link_class'))) {
$url_attributes['class'] = explode(' ', $this
->getSetting('image_link_class'));
}
if (!empty($this
->getSetting('image_link_rel'))) {
$url_attributes['rel'] = explode(' ', $this
->getSetting('image_link_rel'));
}
$elements[$delta] = [
'#theme' => 'image_formatter_link_to_image_style_formatter',
'#item' => $item,
'#item_attributes' => $item_attributes,
'#url' => $url,
'#url_attributes' => $url_attributes,
'#image_style' => $image_style_setting,
'#cache' => [
'tags' => $cache_tags,
'contexts' => $cache_contexts,
],
];
}
return $elements;
}