public function FieldSlideshow::viewElements in Field Slideshow 8.3
Same name and namespace in other branches
- 8 src/Plugin/Field/FieldFormatter/FieldSlideshow.php \Drupal\field_slideshow\Plugin\Field\FieldFormatter\FieldSlideshow::viewElements()
- 8.2 src/Plugin/Field/FieldFormatter/FieldSlideshow.php \Drupal\field_slideshow\Plugin\Field\FieldFormatter\FieldSlideshow::viewElements()
View elements.
Parameters
\Drupal\Core\Field\FieldItemListInterface $items: Items.
string $langcode: Langcode.
Return value
array Rendered array.
Throws
\Exception
Overrides ImageFormatter::viewElements
File
- src/
Plugin/ Field/ FieldFormatter/ FieldSlideshow.php, line 452
Class
- FieldSlideshow
- Plugin implementation of the 'slideshow' formatter.
Namespace
Drupal\field_slideshow\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$itemListInterface = $items;
$id = Html::getUniqueId('field-slideshow-id');
$slideshow_settings = $this
->getSetting('slideshow');
$files = $this
->getEntitiesToView($items, $langcode);
$items = parent::viewElements($items, $langcode);
$pager = NULL;
if (!count($items)) {
return [];
}
if (count($items) > 1) {
$pager = $this
->getSetting('slideshow_pager');
$pagerType = $this->pagerManager
->createInstance($pager['pager_type']);
$pager['pager_type'] = $pagerType
->viewPager($itemListInterface);
}
$output = [];
// Load cycle2swipe if needed.
if ($slideshow_settings['swipe']) {
$libraries[] = 'field_slideshow/field_slideshow.cycle2swipe';
}
$libraries[] = 'field_slideshow/field_slideshow.cycle2';
if ($this
->getSetting('image_link') === 'colorbox') {
$colorbox_style_setting = $this
->getSetting('colorbox_image_style');
if (!empty($colorbox_style_setting)) {
$image_style = $this->imageStyleStorage
->load($colorbox_style_setting);
}
$options = [
'attributes' => [
'class' => [
'colorbox',
],
'data-colorbox-gallery' => 'gallery-' . $id,
],
];
foreach ($items as $key => $item) {
// Create colorbox image url with image style.
$original_url = $url = $files[$key]
->getFileUri();
if (isset($image_style)) {
$url = $image_style
->buildUri($original_url);
if (!file_exists($url)) {
$image_style
->createDerivative($original_url, $url);
}
}
$url = Url::fromUri(file_create_url($url), $options);
// Image-formatter.html.twig does not give
// ability to add url attributes like class.
$img = $this->renderer
->render($items[$key]);
$link = Link::fromTextAndUrl($img, $url);
$items[$key] = $link
->toString();
}
// We can't use dependency injection for this
// service because colorbox is optional.
\Drupal::service('colorbox.attachment')
->attach($output);
}
$output = array_merge_recursive($output, [
'#theme' => 'field_slideshow',
'#items' => $items,
'#pager' => $pager,
'#id' => $id,
'#attached' => [
'library' => $libraries,
'drupalSettings' => [
'field_slideshow' => [
$id => $slideshow_settings,
],
],
],
]);
return $output;
}