You are here

public function SlickViews::buildElements in Slick Views 7.2

Returns slick contents.

1 call to SlickViews::buildElements()
SlickViews::render in ./SlickViews.inc
Renders the display in this style.

File

./SlickViews.inc, line 163
Slick style plugin for the Views module.

Class

SlickViews
Implements a style type plugin for the Views module.

Code

public function buildElements($rows, $settings = array(), $nav = FALSE) {
  $build = $dimensions = array();
  $view = $this->view;
  $keys = array_keys($view->field);
  $stage = $settings['slide_image'];
  $class = $settings['slide_classes'];

  // Defines image dimensions once if applicable to reduce function calls.
  // Think of tons of images with Slick grid.
  if (!empty($stage) && !empty($rows)) {
    $image = $this
      ->getFieldData($rows[0], $stage);

    // Not-empty behavior and filter fail, so add own check here.
    if (isset($image['rendered']) && $this
      ->get_field(0, $stage)) {
      $rendered = $image['rendered'];
      $supported = isset($rendered['#theme']) && $rendered['#theme'] == 'image_formatter';
      if (isset($image['raw']) && $supported) {
        $settings['image_style'] = isset($rendered['#image_style']) ? $rendered['#image_style'] : '';
        if (!empty($settings['image_style'])) {
          $dimensions = slick_get_dimensions($image['raw'], $settings['image_style']);
        }
      }
    }
  }
  foreach ($rows as $index => $row) {
    $view->row_index = $index;
    $thumb = array();
    $slide = array(
      'caption' => array(),
      'slide' => array(),
    );
    $settings['delta'] = $index;

    // Uses all Views markups, ignoring Slick markups.
    if (empty($settings['slide_field_wrapper'])) {
      $slide['slide'] = $view->style_plugin->row_plugin
        ->render($row);
    }
    else {

      // Add layout field, may be a list field, or builtin layout options.
      if ($layout = $settings['slide_layout']) {
        if (strpos($layout, 'field_') !== FALSE) {
          $settings['slide_layout'] = check_plain($this
            ->get_field($index, $layout));
        }
        $settings['layout'] = $settings['slide_layout'];
      }

      // Add main image field if so configured.
      if (!empty($stage)) {

        // Not-empty behavior and filter fail, so add own check here.
        if ($rendered_image = $this
          ->get_field($index, $stage)) {

          // See if we have renderable array to work with.
          $image = $this
            ->getFieldData($row, $stage);
          if (isset($image['rendered'])) {
            $media = $dimensions;
            $rendered = $image['rendered'];
            $settings['type'] = 'image';

            // Only lazyLoad known formatter: image_formatter.
            $supported = isset($rendered['#theme']) && $rendered['#theme'] == 'image_formatter';
            if (!empty($settings['lazy']) && isset($image['raw']) && $supported) {
              $settings['image_style'] = isset($rendered['#image_style']) ? $rendered['#image_style'] : '';
              if (isset($rendered['#path']['path'])) {
                $settings['media_switch'] = 'content';
                $settings['url'] = $settings['entity_uri'] = $rendered['#path']['path'];
              }
              $slide['slide'] = slick_get_image($settings, $media, $image['raw']);
            }
            else {
              $slide['slide'] = $rendered;
            }
          }
          else {

            // Otherwise fallback to rendered image.
            $slide['slide'] = $rendered_image;
          }
        }
      }

      // Add all caption fields if so configured.
      if ($captions = $settings['slide_caption']) {
        $captions = is_array($captions) ? array_filter($captions) : (array) $captions;
        $caption_items = array();
        foreach ($captions as $key => $caption) {
          $caption_rendered = $this
            ->get_field($index, $caption);
          if (empty($caption_rendered)) {
            continue;
          }
          if (in_array($caption, array_values($keys))) {
            $caption_items[$key]['#markup'] = $caption_rendered;
          }
        }
        if ($caption_items) {
          $slide['caption']['data'] = $caption_items;
          unset($caption_items);
        }
      }

      // Add caption fields if so configured.
      $slide['caption']['title'] = empty($settings['slide_title']) ? array() : $this
        ->getFieldRendered($index, $settings['slide_title'], TRUE);
      $slide['caption']['link'] = empty($settings['slide_link']) ? array() : $this
        ->getFieldRendered($index, $settings['slide_link']);
      $slide['caption']['overlay'] = empty($settings['slide_overlay']) ? array() : $this
        ->getFieldRendered($index, $settings['slide_overlay']);

      // Add thumbnail navigation if so configured.
      if ($slide_thumbnail = $settings['slide_thumbnail']) {
        $thumbnail = $this
          ->getFieldData($row, $slide_thumbnail);
        if (isset($thumbnail['rendered']) && ($thumbnail_rendered = $this
          ->get_field($index, $slide_thumbnail))) {
          $thumb['slide'] = $thumbnail['rendered'];
        }
      }

      // Allows text-only thumbnail navigation, like regular tabs.
      // Use Views UI "Rewrite results" to sanitize the caption.
      if ($thumbnail_caption = $settings['thumbnail_caption']) {
        $thumb['caption']['data'] = $this
          ->getFieldRendered($index, $thumbnail_caption);
        if (!isset($thumb['slide'])) {
          $thumb['slide'] = array();
          $settings['type'] = 'text';
        }
      }
    }
    if (!empty($class)) {
      if ($this
        ->get_field($index, $class) && ($classes = $this
        ->getFieldValue($row, $class, $index))) {
        $classes = array_filter($classes);
        $settings['slide_classes'] = empty($classes[$index]) ? '' : $classes[$index];
      }
      else {
        $settings['slide_classes'] = '';
      }
    }

    // Build thumbnail slide items, otherwise main.
    $slide['settings'] = $settings;
    $build[] = $nav ? $thumb : $slide;
    unset($thumb, $slide);
  }
  unset($view->row_index);
  return $build;
}