public function PhotosAlbumFormatter::viewElements in Album Photos 6.0.x
Same name and namespace in other branches
- 8.5 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 150
Class
- PhotosAlbumFormatter
- Plugin implementation of the 'photos_album' 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 = [];
if (isset($entity->album) && isset($entity->album['cover_id'])) {
$coverId = $entity->album['cover_id'];
$photos_album = new PhotosAlbum($entity
->id());
$albumView = $photos_album
->getCover($coverId);
}
// @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;
}