public function RoyalSliderFormatter::viewElements in RoyalSlider Integration 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/ RoyalSliderFormatter.php, line 144 - Contains \Drupal\royalslider\Plugin\Field\FieldFormatter\RoyalSliderFormatter.
Class
- RoyalSliderFormatter
- Plugin implementation of the 'royalslider' formatter.
Namespace
Drupal\royalslider\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items) {
$optionset_setting = $this
->getSetting('royalslider_optionset');
// @TODO load a default optionset if it is set to default.
$optionset = entity_load('royalslider_optionset', $optionset_setting);
$entity = $items
->getEntity();
$fieldname = $items
->getName();
$slider_id = 'royalslider-' . $entity
->getEntityTypeId() . '-' . $entity
->id() . '-' . $fieldname;
$elements = array(
'#theme' => 'royalslider_formatter',
'#attributes' => array(
'id' => $slider_id,
),
'#items' => array(),
'#optionset' => $optionset_setting,
'#cache' => [
'tags' => $optionset
->getCacheTags(),
],
);
foreach ($items as $delta => $item) {
$elements['#items'][] = [
'#theme' => 'image_formatter',
'#item' => $item,
];
}
$elements['#attached'] = [
'library' => [
'royalslider/royalslider',
'royalslider/royalslider-rsdefault',
],
'drupalSettings' => [
'royalslider' => [
'optionsets' => [
$optionset
->label() => $optionset
->buildJsOptionset(),
],
'instances' => [
$slider_id => [
'optionset' => $optionset
->label(),
],
],
],
],
];
return $elements;
}