public function JssorFieldFormatter::viewElements in Jssor Slider 8
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/ JssorFieldFormatter.php, line 224 - Contains \Drupal\jssor\Plugin\Field\FieldFormatter\JssorFieldFormatter.
Class
- JssorFieldFormatter
- Plugin for responsive image formatter.
Namespace
Drupal\jssor\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
$entity = $items
->getEntity();
$field_instance = $items
->getFieldDefinition();
$entity_type_id = $entity
->getEntityTypeId();
$entity_id = $entity
->id();
$field_name = $field_instance
->getName();
$display_name = $this->viewMode;
$files = $this
->getEntitiesToView($items, $langcode);
$settings = $this
->getSettings();
// Early opt-out if the field is empty.
if (empty($files)) {
return $elements;
}
// Let's get a unique uuid.
if ($this->uuidService) {
$uuid = $this->uuidService
->generate();
}
$image_style_setting = $this
->getSetting('image_style');
// Collect cache tags to be added to the container.
$images_cache_tags = array();
if (!empty($image_style_setting)) {
$image_style = $this->imageStyleStorage
->load($image_style_setting);
$images_cache_tags = $image_style
->getCacheTags();
}
foreach ($files as $delta => $file) {
// Collect cache tags to be added for each item in the field.
$image_cache_tags = array();
if (!empty($image_style_setting)) {
$image_style = $this->imageStyleStorage
->load($image_style_setting);
$image_cache_tags = $image_style
->getCacheTags();
}
$images_cache_tags = Cache::mergeTags($images_cache_tags, $file
->getCacheTags());
$image_cache_tags = Cache::mergeTags($image_cache_tags, $file
->getCacheTags());
// Extract field item attributes for the theme function, and unset them
// from the $item so that the field template does not re-render them.
$item = $file->_referringItem;
$item_attributes = $item->_attributes;
unset($item->_attributes);
$elements[$delta] = array(
'#theme' => 'image_jssor_formatter',
'#item' => $item,
'#item_attributes' => $item_attributes,
'#image_style' => $image_style_setting,
'#caption' => $this
->getSetting('caption'),
'#url' => '',
'#settings' => $settings,
'#cache' => array(
'tags' => $image_cache_tags,
),
);
}
$container = array(
'#theme' => 'images_jssor_formatter',
'#children' => $elements,
'#settings' => $settings,
'#attributes' => array(
'class' => array(
'slider',
),
'id' => array(
'slider-dom-id-' . $uuid,
),
),
'#cache' => array(
'tags' => $images_cache_tags,
),
);
// Attach library.
$container['#attached']['library'][] = 'jssor/jquery.jssor.slider';
// Build settings.
$settings = [];
// Unique Uuid.
$settings['view_dom_id'] = $uuid;
// Global settings.
$settings['$ArrowKeyNavigation'] = TRUE;
// Arrows navigator.
if ($this
->getSetting('arrownavigator')) {
$settings['$ArrowNavigatorOptions'] = array(
'$Class' => '$JssorArrowNavigator$',
'$ChanceToShow' => 1,
// Mouse Over.
'$AutoCenter' => 2,
// Vertical.
'$Scale' => TRUE,
);
}
// Bullets navigator.
if ($this
->getSetting('bulletnavigator')) {
$settings['$BulletNavigatorOptions'] = array(
'$Class' => '$JssorBulletNavigator$',
'$ChanceToShow' => 2,
// Always.
'$AutoCenter' => 1,
// Vertical.
'$Scale' => TRUE,
);
}
// Attach settings.
$container['#attached']['drupalSettings']['views']['jssorViews'][] = $settings;
return $container;
}