Builds a renderable array for a field value.
Drupal\Core\Entity\EntityInterface $entity: The entity being displayed.
string $langcode: The language associated with $items.
array $items: Array of values for this field.
array A renderable array for $items, as an array of child elements keyed by numeric indexes starting from 0.
Overrides FormatterInterface::viewElements
public function viewElements(EntityInterface $entity, $langcode, array $items) {
$elements = array();
// Check if the formatter involves a link.
if ($this
->getSetting('image_link') == 'content') {
$uri = $entity
->uri();
}
elseif ($this
->getSetting('image_link') == 'file') {
$link_file = TRUE;
}
$breakpoint_styles = array();
$fallback_image_style = '';
$picture_mapping = entity_load('picture_mapping', $this
->getSetting('picture_mapping'));
if ($picture_mapping) {
foreach ($picture_mapping->mappings as $breakpoint_name => $multipliers) {
// Make sure there are multipliers.
if (!empty($multipliers)) {
// Make sure that the breakpoint exists and is enabled.
// @todo add the following when breakpoint->status is added again:
// $picture_mapping->breakpointGroup->breakpoints[$breakpoint_name]->status
if (isset($picture_mapping->breakpointGroup->breakpoints[$breakpoint_name])) {
$breakpoint = $picture_mapping->breakpointGroup->breakpoints[$breakpoint_name];
// Determine the enabled multipliers.
$multipliers = array_intersect_key($multipliers, $breakpoint->multipliers);
foreach ($multipliers as $multiplier => $image_style) {
// Make sure the multiplier still exists.
if (!empty($image_style)) {
// First mapping found is used as fallback.
if (empty($fallback_image_style)) {
$fallback_image_style = $image_style;
}
if (!isset($breakpoint_styles[$breakpoint_name])) {
$breakpoint_styles[$breakpoint_name] = array();
}
$breakpoint_styles[$breakpoint_name][$multiplier] = $image_style;
}
}
}
}
}
}
// Check if the user defined a custom fallback image style.
if ($this
->getSetting('fallback_image_style')) {
$fallback_image_style = $this
->getSetting('fallback_image_style');
}
foreach ($items as $delta => $item) {
if (isset($link_file)) {
$uri = array(
'path' => file_create_url($item['uri']),
'options' => array(),
);
}
$elements[$delta] = array(
'#theme' => 'picture_formatter',
'#attached' => array(
'library' => array(
array(
'picture',
'picturefill',
),
),
),
'#item' => $item,
'#image_style' => $fallback_image_style,
'#breakpoints' => $breakpoint_styles,
'#path' => isset($uri) ? $uri : '',
);
}
return $elements;
}