public function AlbumImageFormatter::viewElements in Open Social 10.1.x
Same name and namespace in other branches
- 10.3.x modules/social_features/social_post/modules/social_post_album/src/Plugin/Field/FieldFormatter/AlbumImageFormatter.php \Drupal\social_post_album\Plugin\Field\FieldFormatter\AlbumImageFormatter::viewElements()
- 10.0.x modules/social_features/social_post/modules/social_post_album/src/Plugin/Field/FieldFormatter/AlbumImageFormatter.php \Drupal\social_post_album\Plugin\Field\FieldFormatter\AlbumImageFormatter::viewElements()
- 10.2.x modules/social_features/social_post/modules/social_post_album/src/Plugin/Field/FieldFormatter/AlbumImageFormatter.php \Drupal\social_post_album\Plugin\Field\FieldFormatter\AlbumImageFormatter::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 ImageFormatter::viewElements
File
- modules/
social_features/ social_post/ modules/ social_post_album/ src/ Plugin/ Field/ FieldFormatter/ AlbumImageFormatter.php, line 32
Class
- AlbumImageFormatter
- Plugin implementation of the 'album_image' formatter.
Namespace
Drupal\social_post_album\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
// Grab all elements from the parent view.
$elements = parent::viewElements($items, $langcode);
if (!$items
->isEmpty()) {
// If it's only one, we can safely return without updating image styles.
if ($items
->count() === 1) {
return $elements;
}
// If it's more than one, lets remove it after we hit our limit,
// and render them using a different image style to make sure they are
// square in size and multiple can fit together in the post view.
foreach (array_reverse($this
->getEntitiesToView($items, $langcode)) as $delta => $file) {
$elements[$delta]['#image_style'] = 'social_x_large';
if (self::LIMIT < $delta) {
unset($elements[$delta]);
}
}
}
// Return all updated elements with a max of self::LIMIT.
return $elements;
}