You are here

class SlickViews in Slick Views 7.2

Implements a style type plugin for the Views module.

Hierarchy

Expanded class hierarchy of SlickViews

1 string reference to 'SlickViews'
slick_views_views_plugins in ./slick_views.views.inc
Implements hook_views_plugins().

File

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

View source
class SlickViews extends views_plugin_style {

  /**
   * {@inheritdoc}
   */
  public function init(&$view, &$display, $options = NULL) {
    parent::init($view, $display, $options);

    // Even empty active to call render; where library is attached if required.
    if ($view->use_ajax) {
      $this->definition['even empty'] = TRUE;
    }
  }

  /**
   * Provides default options.
   */
  public function option_definition() {
    module_load_include('inc', 'slick', 'includes/slick.global');
    $options = array(
      'slide_thumbnail' => array(
        'default' => '',
      ),
      'slide_field_wrapper' => array(
        'default' => FALSE,
      ),
      'id' => array(
        'default' => '',
      ),
    );
    foreach (slick_get_global_default_settings() as $key => $value) {
      $options[$key] = array(
        'default' => $value,
      );
    }
    drupal_alter('slick_views_options_info', $options);
    return $options + parent::option_definition();
  }

  /**
   * Shows a form to edit the style options.
   */
  public function options_form(&$form, &$form_state) {
    parent::options_form($form, $form_state);
    ctools_form_include($form_state, 'slick.admin', 'slick');
    ctools_form_include($form_state, 'admin', 'slick_views');
    _slick_views_options_form($form, $form_state, $this->view, $this->options);
  }

  /**
   * Performs some cleanup tasks on the options array before saving it.
   */
  public function options_submit(&$form, &$form_state) {
    $options =& $form_state['values']['style_options'];

    // The form is a #tree, but the expected output is a flattened array.
    if (!empty($options['slick'])) {

      // Pull the fieldset values one level up.
      $options += $options['slick'];
      unset($options['slick']);
    }
  }

  /**
   * Renders the slick instances.
   */
  public function renderSlick($slick, $settings) {
    return array(
      '#theme' => $this
        ->theme_functions(),
      '#view' => $this->view,
      '#options' => $settings,
      '#rows' => $slick,
    );
  }

  /**
   * Renders the display in this style.
   */
  public function render() {
    global $language;
    $langcode = isset($language->language) ? $language->language : LANGUAGE_NONE;
    $view = $this->view;
    $settings = $this->options;
    $view_name = $view->name;
    $current = $view->current_display;
    $id = slick_html_id("slick-views-{$view_name}", $settings['id']);
    $thumb_id = $id . '-thumbnail';
    $asnavfor = $settings['optionset_thumbnail'];
    $attach = $build = array();

    // Renders slicks quickly from cache if any, as render cache is just cache.
    $settings['count'] = count($view->result);
    $settings['nav'] = $asnavfor && $settings['count'] > 1;
    $settings['cid'] = $settings['optionset'] . $settings['skin'] . $view_name . $current . $langcode;
    if (!empty($settings['cache']) && ($cache = slick_render_cache($settings))) {
      return $this
        ->renderSlick($cache, $settings);
    }

    // Otherwise do the routines before a cache stored, or when disabled.
    module_load_include('inc', 'slick', 'includes/slick.global');
    $optionset = slick_optionset_load($settings['optionset']);
    $optionset_thumbnail = !$settings['nav'] ? NULL : slick_optionset_load($asnavfor);
    $attachments = slick_attach($attach, $settings);
    $lazy = $optionset->options['settings']['lazyLoad'];
    $settings['blazy'] = $lazy == 'blazy' || !empty($settings['breakpoints']);
    $settings['lazy'] = $settings['blazy'] ? 'blazy' : $lazy;
    $settings['has_pattern'] = !empty($optionset->options['general']['goodies']['pattern']);
    $settings['view_name'] = $view_name;
    $settings['current_view_mode'] = $current;
    $settings['current_display'] = 'main';
    $settings['ratio'] = !empty($settings['aspect_ratio']) ? $settings['aspect_ratio'] : FALSE;
    $build = array();
    foreach ($this
      ->render_grouping($view->result, $settings['grouping']) as $rows) {
      $js = array();
      if ($settings['nav']) {
        $js['asNavFor'] = "#{$thumb_id}-slider";
      }

      // Build the single/main display Slick.
      $items = $this
        ->buildElements($rows, $settings);
      $slick[0] = slick_build($items, $js, $settings, $attachments, $id, $optionset);
      if (empty($items) && !$view->use_ajax) {

        // Return empty $build if there are no results.
        // Otherwise views will never show empty behaviour.
        // Important: If use_ajax is on, we can't do this, as the library may be missing then. See below.
        return $build;
      }
      elseif (empty($items) && $view->use_ajax) {

        // Warning: THIS MAY LEAD TO UNEXPECTED BEHAVIOUR, SEE https://www.drupal.org/project/slick_views/issues/3210378
        // Attach library if there is no results and ajax is active,
        // otherwise library will not be attached on ajax callback.
        $slick[0]['#attached'] = $attachments;
      }
      if ($settings['nav']) {
        $settings['optionset'] = $asnavfor;
        $settings['current_display'] = 'thumbnail';
        $js['asNavFor'] = "#{$id}-slider";

        // Build the thumbnail+/text navigation Slick.
        $thumbs = $this
          ->buildElements($rows, $settings, TRUE);
        $slick[1] = slick_build($thumbs, $js, $settings, array(), $id, $optionset_thumbnail);
        unset($thumbs, $optionset_thumbnail);
      }

      // Build the Slick.
      $build = $this
        ->renderSlick($slick, $settings);
      unset($slick, $items, $optionset, $settings, $view);
    }
    return $build;
  }

  /**
   * Returns slick contents.
   */
  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;
  }

  /**
   * Returns the rendered field, either string or array.
   */
  public function getFieldRendered($index, $field_name = '', $restricted = FALSE) {
    if (!empty($field_name) && ($output = $this
      ->get_field($index, $field_name))) {
      return is_array($output) ? $output : array(
        '#markup' => $restricted ? filter_xss_admin($output) : $output,
      );
    }
    return array();
  }

  /**
   * Gets renderable array of field containing rendered and raw data.
   */
  public function getFieldData($row, $field_name, $multiple = FALSE) {
    $field = $this->view->field[$field_name]->handler_type . '_' . $field_name;
    return $multiple && isset($row->{$field}) ? $row->{$field} : (isset($row->{$field}[0]) ? $row->{$field}[0] : '');
  }

  /**
   * Returns the values for the expected Title, ER, List, Term.
   */
  public function getFieldValue($row, $field_name, $index) {
    $values = array();

    // Content title/List/Text, either as link or plain text.
    // @todo recheck multi-values.
    if ($value = $this
      ->get_field_value($index, $field_name)) {
      if (is_array($value)) {
        foreach (array_filter($value) as $key => $val) {
          $v = isset($val['value']) ? $val['value'] : $val;
          $value[$key] = drupal_clean_css_identifier(drupal_strtolower($v));
        }
      }
      $string = is_string($value) ? $value : implode(' ', $value);
      $values[$index] = empty($string) ? '' : $string;
    }
    elseif ($renderable = $this
      ->getFieldData($row, $field_name, TRUE)) {
      $value = array();
      foreach ($renderable as $key => $render) {
        $class = '';
        if (isset($render['rendered']['#title'])) {
          $class = $render['rendered']['#title'];
        }
        elseif (isset($render['raw']['value'])) {
          $class = $render['raw']['value'];
        }
        $class = $class ? $class : drupal_render($render['rendered']);
        $class = strip_tags($class);
        $value[$key] = drupal_clean_css_identifier(drupal_strtolower($class));
      }
      $values[$index] = empty($value) ? '' : implode(' ', $value);
    }
    return $values;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SlickViews::buildElements public function Returns slick contents.
SlickViews::getFieldData public function Gets renderable array of field containing rendered and raw data.
SlickViews::getFieldRendered public function Returns the rendered field, either string or array.
SlickViews::getFieldValue public function Returns the values for the expected Title, ER, List, Term.
SlickViews::init public function Initialize a style plugin. Overrides views_plugin_style::init
SlickViews::options_form public function Shows a form to edit the style options. Overrides views_plugin_style::options_form
SlickViews::options_submit public function Performs some cleanup tasks on the options array before saving it. Overrides views_plugin::options_submit
SlickViews::option_definition public function Provides default options. Overrides views_plugin_style::option_definition
SlickViews::render public function Renders the display in this style. Overrides views_plugin_style::render
SlickViews::renderSlick public function Renders the slick instances.
views_object::$definition public property Handler's definition.
views_object::$options public property Except for displays, options for the object will be held here. 1
views_object::altered_option_definition function Collect this handler's option definition and alter them, ready for use.
views_object::construct public function Views handlers use a special construct function. 4
views_object::export_option public function 1
views_object::export_options public function
views_object::export_option_always public function Always exports the option, regardless of the default value.
views_object::options Deprecated public function Set default options on this object. 1
views_object::set_default_options public function Set default options.
views_object::set_definition public function Let the handler know what its full definition is.
views_object::unpack_options public function Unpack options over our existing defaults, drilling down into arrays so that defaults don't get totally blown away.
views_object::unpack_translatable public function Unpack a single option definition.
views_object::unpack_translatables public function Unpacks each handler to store translatable texts.
views_object::_set_option_defaults public function
views_plugin::$display public property The current used views display.
views_plugin::$plugin_name public property The plugin name of this plugin, for example table or full.
views_plugin::$plugin_type public property The plugin type of this plugin, for example style or query.
views_plugin::$view public property The top object of a view. Overrides views_object::$view 1
views_plugin::additional_theme_functions public function Provide a list of additional theme functions for the theme info page.
views_plugin::plugin_title public function Return the human readable name of the display.
views_plugin::summary_title public function Returns the summary of the settings in the display. 8
views_plugin::theme_functions public function Provide a full list of possible theme templates used by this style.
views_plugin_style::$row_plugin public property The row plugin, if it's initialized and the style itself supports it.
views_plugin_style::$row_tokens public property Store all available tokens row rows.
views_plugin_style::build_sort public function Called by the view builder to see if this style handler wants to interfere with the sorts. If so it should build; if it returns any non-TRUE value, normal sorting will NOT be added to the query. 1
views_plugin_style::build_sort_post public function Called by the view builder to let the style build a second set of sorts that will come after any other sorts in the view. 1
views_plugin_style::destroy public function Destructor. Overrides views_object::destroy
views_plugin_style::even_empty public function Should the output of the style plugin be rendered even if it's empty. 1
views_plugin_style::get_field public function Get a rendered field.
views_plugin_style::get_field_value public function Get the raw field value.
views_plugin_style::get_row_class public function Return the token replaced row class for the specified row.
views_plugin_style::options_validate public function Validate the options form. Overrides views_plugin::options_validate
views_plugin_style::pre_render public function Allow the style to do stuff before each row is rendered.
views_plugin_style::query public function Add anything to the query that we might need to. Overrides views_plugin::query 2
views_plugin_style::render_fields public function Render all of the fields for a given style and store them on the object.
views_plugin_style::render_grouping public function Group records as needed for rendering.
views_plugin_style::render_grouping_sets public function Render the grouping sets.
views_plugin_style::tokenize_value public function Take a value and apply token replacement logic to it.
views_plugin_style::uses_fields public function Return TRUE if this style also uses fields.
views_plugin_style::uses_row_class public function Return TRUE if this style also uses a row plugin.
views_plugin_style::uses_row_plugin public function Return TRUE if this style also uses a row plugin.
views_plugin_style::uses_tokens public function Return TRUE if this style uses tokens.
views_plugin_style::validate public function Validate that the plugin is correct and can be saved. Overrides views_plugin::validate