You are here

public function FieldSlideshow::viewElements in Field Slideshow 8

Same name and namespace in other branches
  1. 8.3 src/Plugin/Field/FieldFormatter/FieldSlideshow.php \Drupal\field_slideshow\Plugin\Field\FieldFormatter\FieldSlideshow::viewElements()
  2. 8.2 src/Plugin/Field/FieldFormatter/FieldSlideshow.php \Drupal\field_slideshow\Plugin\Field\FieldFormatter\FieldSlideshow::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 ImageFormatter::viewElements

File

src/Plugin/Field/FieldFormatter/FieldSlideshow.php, line 377
Contains \Drupal\field_slideshow\Plugin\Field\FieldFormatter\Slideshow.

Class

FieldSlideshow
Plugin implementation of the 'slideshow' formatter.

Namespace

Drupal\field_slideshow\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {

  // get image html from parent method.
  $images = parent::viewElements($items, $langcode);
  static $slideshow_count;
  $slideshow_count = is_int($slideshow_count) ? $slideshow_count + 1 : 1;
  $files = $this
    ->getEntitiesToView($items, $langcode);

  // 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
          ->getValue()['title'];
      }
      elseif ($caption_settings == 'alt') {
        $item_settings[$delta]['caption'] = $item
          ->getValue()['alt'];
      }
      $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 : '';
              $item
                ->set($path, $uri);
            }
            break;
          case 'file':
            foreach ($files as $file_delta => $file) {
              $image_uri = $file
                ->getFileUri();
              $uri = Url::fromUri(file_create_url($image_uri));
              $uri = !empty($uri) ? $uri : '';
              $items[$file_delta]
                ->set($path, $uri);
            }
            break;
          case 'colorbox':
            $attrib = array();

            // check if we need a thumbnail and change the link
            $entity = $item
              ->getEntity();
            foreach ($files as $file_delta => $file) {
              $image_uri = $file
                ->getFileUri();
              $uri = Url::fromUri(file_create_url($image_uri));
              $uri = !empty($uri) ? $uri : '';
              if ($this
                ->getSetting('slideshow_colorbox_image_style') != '') {
                $uri = ImageStyle::load($this
                  ->getSetting('slideshow_colorbox_image_style'))
                  ->buildUrl($image_uri);
                $attrib['uri'] = $uri;
              }
              else {
                $attrib['uri'] = $uri
                  ->getUri();
              }

              //add correct attributes
              $attrib['attributes'] = array(
                'class' => array(
                  'colorbox',
                ),
                'rel' => 'field-slideshow[' . 'nid' . '-' . $entity
                  ->id() . ']',
              );
              if ($this
                ->getSetting('slideshow_caption') != '' && isset($items[$file_delta]
                ->getValue()['caption'])) {
                $attrib['attributes']['title'] = $items[$file_delta]
                  ->getValue()['caption'];
              }
              $colorbox_slideshow = $this
                ->getSetting('slideshow_colorbox_slideshow');
              if (!empty($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');
              }
              $items[$file_delta]
                ->set($path, $attrib);
            }
            break;
        }
      }
    }
  }
  $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,
  );
  $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' => $images,
    '#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;
}