public function PhotoswipeFieldFormatter::viewElements in PhotoSwipe 8.2
Same name and namespace in other branches
- 8 src/Plugin/Field/FieldFormatter/PhotoswipeFieldFormatter.php \Drupal\photoswipe\Plugin\Field\FieldFormatter\PhotoswipeFieldFormatter::viewElements()
- 3.x src/Plugin/Field/FieldFormatter/PhotoswipeFieldFormatter.php \Drupal\photoswipe\Plugin\Field\FieldFormatter\PhotoswipeFieldFormatter::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/ PhotoswipeFieldFormatter.php, line 253
Class
- PhotoswipeFieldFormatter
- Plugin implementation of the 'photoswipe_field_formatter' formatter.
Namespace
Drupal\photoswipe\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$settings = $this
->getSettings();
if ($items
->isEmpty()) {
$default_image = $this
->getFieldSetting('default_image');
// If we are dealing with a configurable field, look in both
// instance-level and field-level settings.
if (empty($default_image['uuid']) && $this->fieldDefinition instanceof FieldConfigInterface) {
$default_image = $this->fieldDefinition
->getFieldStorageDefinition()
->getSetting('default_image');
}
if (!empty($default_image['uuid']) && ($file = \Drupal::service('entity.repository')
->loadEntityByUuid('file', $default_image['uuid']))) {
// Clone the FieldItemList into a runtime-only object for the formatter,
// so that the fallback image can be rendered without affecting the
// field values in the entity being rendered.
$items = clone $items;
$items
->setValue([
'target_id' => $file
->id(),
'alt' => $default_image['alt'],
'title' => $default_image['title'],
'width' => $default_image['width'],
'height' => $default_image['height'],
'entity' => $file,
'_loaded' => TRUE,
'_is_default' => TRUE,
]);
}
}
\Drupal::service('photoswipe.assets_manager')
->attach($elements);
if (!empty($items) && count($items) > 1) {
// If there are more than 1 elements, add the gallery wrapper.
// Otherwise this is done in javascript for more flexibility.
$elements['#prefix'] = '<div class="photoswipe-gallery">';
$elements['#suffix'] = '</div>';
}
foreach ($items as $delta => $item) {
$elements[$delta] = [
'#theme' => 'photoswipe_image_formatter',
'#item' => $item,
'#entity' => $items
->getEntity(),
'#display_settings' => $settings,
'#delta' => $delta,
];
}
return $elements;
}