public function PhotosAlbumFormatter::viewElements in Album Photos 8.5
Same name and namespace in other branches
- 6.0.x src/Plugin/Field/FieldFormatter/PhotosAlbumFormatter.php \Drupal\photos\Plugin\Field\FieldFormatter\PhotosAlbumFormatter::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 FormatterInterface::viewElements
File
- src/
Plugin/ Field/ FieldFormatter/ PhotosAlbumFormatter.php, line 149
Class
- PhotosAlbumFormatter
- Plugin implementation of the 'media_thumbnail' formatter.
Namespace
Drupal\photos\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$photosDisplayType = $this
->getSetting('photos_display_type');
foreach ($this
->getEntitiesToView($items, $langcode) as $delta => $entity) {
// @todo fall back on label display if not a photo album.
$label = $entity
->label();
// If the link is to be displayed and the entity has a uri, display a
// link.
switch ($photosDisplayType) {
case 'none':
// Render nothing.
break;
case 'cover':
$albumView = [];
// @todo this is going to be photos_image view mode.
if (isset($entity->album['cover'])) {
$albumView = $entity->album['cover'];
}
// @todo fallback on image?
$elements[$delta] = $albumView;
break;
}
if (!$entity
->isNew()) {
if (!empty($items[$delta]->_attributes)) {
$elements[$delta]['#options'] += [
'attributes' => [],
];
$elements[$delta]['#options']['attributes'] += $items[$delta]->_attributes;
// Unset field item attributes since they have been included in the
// formatter output and shouldn't be rendered in the field template.
unset($items[$delta]->_attributes);
}
}
else {
$elements[$delta] = [
'#plain_text' => $label,
];
}
$elements[$delta]['#cache']['tags'] = $entity
->getCacheTags();
$elements[$delta]['#cache']['tags'][] = 'photos:album:' . $entity
->id();
}
return $elements;
}