public function PhotoswipeFieldFormatter::viewElements in PhotoSwipe 3.x
Same name and namespace in other branches
- 8.2 src/Plugin/Field/FieldFormatter/PhotoswipeFieldFormatter.php \Drupal\photoswipe\Plugin\Field\FieldFormatter\PhotoswipeFieldFormatter::viewElements()
- 8 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
1 call to PhotoswipeFieldFormatter::viewElements()
- PhotoswipeResponsiveFieldFormatter::viewElements in src/
Plugin/ Field/ FieldFormatter/ PhotoswipeResponsiveFieldFormatter.php - Builds a renderable array for a field value.
1 method overrides PhotoswipeFieldFormatter::viewElements()
- PhotoswipeResponsiveFieldFormatter::viewElements in src/
Plugin/ Field/ FieldFormatter/ PhotoswipeResponsiveFieldFormatter.php - Builds a renderable array for a field value.
File
- src/
Plugin/ Field/ FieldFormatter/ PhotoswipeFieldFormatter.php, line 385
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 = $this->entityRepository
->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,
]);
}
}
$this->photoswipeAssetManager
->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;
}