public function FieldCollectionSlideshow::viewElements in Field Slideshow 8.2
Same name and namespace in other branches
- 8 src/Plugin/Field/FieldFormatter/FieldCollectionSlideshow.php \Drupal\field_slideshow\Plugin\Field\FieldFormatter\FieldCollectionSlideshow::viewElements()
File
- src/
Plugin/ Field/ FieldFormatter/ FieldCollectionSlideshow.php, line 439
Class
- FieldCollectionSlideshow
- Plugin implementation of the 'slideshow' formatter.
Namespace
Drupal\field_slideshow\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$image_field = $this
->getSetting('image_field');
if ($image_field == '') {
drupal_set_message(t('Please choose image field in slideshow option in manage display, Now we displayed Field Collections items options.'), 'error');
return parent::viewElements($items, $langcode);
}
$caption = $this
->getSetting('slideshow_caption');
foreach ($items as $key => $item) {
$render = $item
->getFieldCollectionItem()
->get($image_field)
->first();
$render_value['#theme'] = 'image_formatter';
$render_value['#label_display'] = 'hidden';
$render_value['#url'] = NULL;
$render_value['#item'] = $render;
$render_value['#image_style'] = $this
->getSetting('image_style');
$render_value['#item_attributes'] = array();
$render_item[] = $render_value;
}
static $slideshow_count;
$slideshow_count = is_int($slideshow_count) ? $slideshow_count + 1 : 1;
// Check plugins.
if (\Drupal::moduleHandler()
->moduleExists('libraries')) {
if (!file_exists(DRUPAL_ROOT . '/libraries/jquery.cycle/jquery.cycle.all.min.js') && !file_exists(DRUPAL_ROOT . '/libraries/jquery.cycle/jquery.cycle.all.js')) {
$url = Url::fromRoute('system.status');
drupal_set_message(t('JQuery Cycle must be installed in order to run the slideshow. Please go to !page for instructions.', array(
'!page' => \Drupal::l(t('Status Report'), $url),
)), 'warning', FALSE);
}
}
else {
$url = Url::fromUri('http://drupal.org/project/libraries');
drupal_set_message(t('Please install the !module module in order to use Field Slideshow.', array(
'!module' => \Drupal::l('Libraries API', $url),
)), 'warning', FALSE);
}
$elements = array();
$entity = array();
$links = array(
'image_link' => 'path',
'slideshow_caption_link' => 'caption_path',
);
// Loop through required links (because image and
// caption can have different links).
foreach ($items as $delta => $item) {
$uri = array();
// Set Image caption.
if ($this
->getSetting('slideshow_caption') != '') {
$caption_settings = $this
->getSetting('slideshow_caption');
if ($caption_settings == 'title') {
$item_settings[$delta]['caption'] = $item
->getFieldCollectionItem()
->get($image_field)
->getValue()[0]['title'];
}
elseif ($caption_settings == 'alt') {
$item_settings[$delta]['caption'] = $item
->getFieldCollectionItem()
->get($image_field)
->getValue()[0]['alt'];
}
else {
$item_settings[$delta]['caption'] = $item
->getFieldCollectionItem()
->get($caption)
->getValue()[0]['value'];
}
$item
->set('caption', $item_settings[$delta]['caption']);
}
// Set Image and Caption Link.
foreach ($links as $setting => $path) {
if ($this
->getSetting($setting) != '') {
switch ($this
->getSetting($setting)) {
case 'content':
$entity = $item
->getEntity();
if (!$entity
->isNew()) {
$uri = $entity
->urlInfo();
$uri = !empty($uri) ? $uri : '';
if ($setting == 'image_link') {
$item
->set('fc_path', $uri);
}
elseif ($setting == 'slideshow_caption_link') {
$item
->set($path, $uri);
}
}
break;
case 'file':
$target_id = $item
->getFieldCollectionItem()
->get($image_field)
->first()
->getValue()['target_id'];
$file = File::load($target_id)
->getFileUri();
$uri = Url::fromUri(file_create_url($file));
$uri = !empty($uri) ? $uri : '';
if ($setting == 'image_link') {
$item
->set('fc_file_path', $uri);
}
elseif ($setting == 'slideshow_caption_link') {
$item
->set($path, $uri);
}
break;
case 'colorbox':
$attrib = array();
// Check if we need a thumbnail and change the link.
$entity = $item
->getEntity();
$target_id = $item
->getFieldCollectionItem()
->get($image_field)
->first()
->getValue()['target_id'];
$file = File::load($target_id)
->getFileUri();
$uri = Url::fromUri(file_create_url($file));
$uri = !empty($uri) ? $uri : '';
if ($this
->getSetting('slideshow_colorbox_image_style') != '') {
$uri = ImageStyle::load($this
->getSetting('slideshow_colorbox_image_style'))
->buildUrl($file);
$attrib['uri'] = $uri;
}
else {
$attrib['uri'] = $uri
->getUri();
}
// Add correct attributes.
$attrib['attributes'] = array(
'class' => array(
'colorbox',
),
'rel' => 'field-collection-slideshow[' . 'nid' . '-' . $entity
->id() . ']',
);
if ($this
->getSetting('slideshow_caption') != '' && isset($item_settings[$delta]['caption'])) {
$attrib['attributes']['title'] = $item_settings[$delta]['caption'];
}
$colorbox_slideshow = $this
->getSetting('slideshow_colorbox_slideshow');
if (isset($colorbox_slideshow) && $colorbox_slideshow != '') {
$attrib['attributes']['class'] = array(
'colorbox',
);
$attrib['uri'] .= strpos($attrib['uri'], '?') === FALSE ? '?' : '&';
$attrib['uri'] .= 'slideshow=true&slideshowAuto=' . ($this
->getSetting('slideshow_colorbox_slideshow') == 'automatic' ? 'true' : 'false') . '&slideshowSpeed=' . $this
->getSetting('slideshow_colorbox_slideshow_speed') . '&speed=' . $this
->getSetting('slideshow_colorbox_speed') . '&transition=' . $this
->getSetting('slideshow_colorbox_transition');
}
$file_delta = $item
->getName();
$items[$file_delta]
->set($path, $attrib);
break;
}
}
}
if ($this
->getSetting('slideshow_pager') == 'image') {
$target_id = $item
->getFieldCollectionItem()
->get($image_field)
->first()
->getValue()['target_id'];
$file = File::load($target_id)
->getFileUri();
$item
->set('fc_thumbnail_path', $file);
}
}
$pager = array(
'#theme' => 'field_slideshow_pager',
'#items' => $items,
'#pager' => $this
->getSetting('slideshow_pager'),
'#pager_image_style' => $this
->getSetting('slideshow_pager_image_style'),
'#slideshow_id' => $slideshow_count,
'#check' => 'field_collection',
'#fc_image_field' => $this
->getSetting('image_field'),
);
$controls = array(
'#theme' => 'field_slideshow_controls',
'#slideshow_id' => $slideshow_count,
'#controls_pause' => $this
->getSetting('slideshow_controls_pause'),
);
$elements[] = array(
'#theme' => 'field_slideshow',
'#items' => $items,
'#image_style' => $this
->getSetting('image_style'),
'#image' => $render_item,
'#order' => $this
->getSetting('slideshow_order'),
'#controls' => $this
->getSetting('slideshow_controls') == 1 ? $controls : array(),
'#controls_position' => $this
->getSetting('slideshow_controls_position'),
'#pager' => $this
->getSetting('slideshow_pager') !== '' ? $pager : array(),
'#pager_position' => $this
->getSetting('slideshow_pager_position'),
'#entity' => $entity,
'#slideshow_id' => $slideshow_count,
'#js_variables' => array(
'fx' => $this
->getSetting('slideshow_fx'),
'speed' => $this
->getSetting('slideshow_speed'),
'timeout' => $this
->getSetting('slideshow_timeout'),
'pause' => $this
->getSetting('slideshow_pause'),
'start_on_hover' => $this
->getSetting('slideshow_start_on_hover'),
// Need to access the following variables in js too.
'pager' => $this
->getSetting('slideshow_pager'),
'controls' => $this
->getSetting('slideshow_controls') === 1 ? $controls : array(),
),
);
return $elements;
}