You are here

public function FieldCollectionSlideshow::viewElements in Field Slideshow 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/FieldCollectionSlideshow.php \Drupal\field_slideshow\Plugin\Field\FieldFormatter\FieldCollectionSlideshow::viewElements()

File

src/Plugin/Field/FieldFormatter/FieldCollectionSlideshow.php, line 441
Contains \Drupal\field_slideshow\Plugin\Field\FieldFormatter\Slideshow.

Class

FieldCollectionSlideshow
Plugin implementation of the 'slideshow' formatter.

Namespace

Drupal\field_slideshow\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $image_field = $this
    ->getSetting('image_field');
  $caption = $this
    ->getSetting('slideshow_caption');
  $alt = array();
  foreach ($items as $key => $item) {
    $render_item[] = $item
      ->getFieldCollectionItem()
      ->get($image_field)
      ->view($display_options = array());
  }
  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();
  $files = 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($include_computed = false)[0]['title'];
      }
      elseif ($caption_settings == 'alt') {
        $item_settings[$delta]['caption'] = $item
          ->getFieldCollectionItem()
          ->get($image_field)
          ->getValue($include_computed = false)[0]['alt'];
      }
      else {
        $item_settings[$delta]['caption'] = $item
          ->getFieldCollectionItem()
          ->get($caption)
          ->getValue($include_computed = false)[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
            if ($this
              ->getSetting('slideshow_colorbox_image_style') != '') {
              $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 : '';
              $uri = ImageStyle::load($this
                ->getSetting('slideshow_colorbox_image_style'))
                ->buildUrl($file);
              $attrib['uri'] = $uri;

              //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();

      // kint(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'),
    //'#carousel_image_style' => $this->getSetting('slideshow_carousel_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'),
      // 'carousel_visible'     => $this->getSetting('slideshow_carousel_visible'),
      // 'carousel_scroll'      => $this->getSetting('slideshow_carousel_scroll'),
      // 'carousel_speed'       => $this->getSetting('slideshow_carousel_speed'),
      // 'carousel_vertical'    => $this->getSetting('slideshow_carousel_vertical'),
      // 'carousel_circular'    => $this->getSetting('slideshow_carousel_circular'),
      // 'carousel_follow'      => $this->getSetting('slideshow_carousel_follow'),
      // 'carousel_skin'        => $this->getSetting('slideshow_carousel_skin'),
      // Need to access the following variables in js too
      'pager' => $this
        ->getSetting('slideshow_pager'),
      'controls' => $this
        ->getSetting('slideshow_controls') === 1 ? $controls : array(),
    ),
  );
  return $elements;
}