You are here

class lightgallery_style_plugin in Lightgallery 7

Style plugin to render views as lightgallery instance.

Hierarchy

Expanded class hierarchy of lightgallery_style_plugin

1 string reference to 'lightgallery_style_plugin'
lightgallery_views_plugins in views/lightgallery.views.inc
Implements hook_views_plugins().

File

views/lightgallery_style_plugin.inc, line 13
Contains the lightgallery style plugin.

View source
class lightgallery_style_plugin extends views_plugin_style {

  /**
   * Provide default options.
   */
  function option_definition() {
    $options = parent::option_definition();
    $options['lightgallery'] = array(
      'default' => array(),
    );
    return $options;
  }

  /**
   * Adds Lightgallery configuration form options.
   */
  function options_form(&$form, &$form_state) {

    // Check if fields have been added.  Table style plugin will set
    // error_markup if fields are not added.
    // @see views_plugin_style::options_form()
    if (isset($form['error_markup'])) {
      return;
    }
    $fields = $this
      ->conf_get_field_sources();
    $missing_field_warning = '';
    if (empty($fields['field_options_images'])) {
      $missing_field_warning = t('<strong>You must add a field of type image, file or file ID to your view display before this value can be set.</strong><br/>');
    }

    // CORE OPTIONS.
    $form['lightgallery_core'] = array(
      '#type' => 'fieldset',
      '#title' => t('Lightgallery core settings'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    );
    $form['lightgallery_core']['thumb_field'] = array(
      '#title' => t('Thumbnail field'),
      '#type' => 'select',
      '#options' => $fields['field_options_images'],
      '#default_value' => isset($this->options['lightgallery']['thumb_field']) ? $this->options['lightgallery']['thumb_field'] : NULL,
      '#description' => t('Select the field you want to use to display the thumbnails on page load.'),
      '#suffix' => $missing_field_warning,
      '#required' => TRUE,
    );
    $form['lightgallery_core']['image_field'] = array(
      '#title' => t('Image field'),
      '#type' => 'select',
      '#options' => $fields['field_options_images'],
      '#default_value' => isset($this->options['lightgallery']['image_field']) ? $this->options['lightgallery']['image_field'] : NULL,
      '#description' => t('Select the field you want to use to display the images in the Lightgallery.'),
      '#suffix' => $missing_field_warning,
      '#required' => TRUE,
    );
    $form['lightgallery_core']['title'] = array(
      '#title' => t('Title field'),
      '#type' => 'select',
      '#options' => array(
        '' => t('None'),
      ) + $fields['field_options'],
      '#default_value' => isset($this->options['lightgallery']['title']) ? $this->options['lightgallery']['title'] : NULL,
      '#description' => t('Select the field you want to use as title in the Lightgallery. Leave empty to omit titles.'),
      '#required' => FALSE,
    );
    $form['lightgallery_core']['mode'] = array(
      '#type' => 'select',
      '#title' => t('Transition'),
      '#options' => _lightgallery_get_modes(),
      '#default_value' => isset($this->options['lightgallery']['mode']) ? $this->options['lightgallery']['mode'] : NULL,
      '#description' => t('Type of transition between images.'),
    );
    $form['lightgallery_core']['preload'] = array(
      '#type' => 'select',
      '#title' => t('Preload'),
      '#options' => drupal_map_assoc(array(
        1,
        2,
        3,
        4,
      )),
      '#default_value' => isset($this->options['lightgallery']['preload']) ? $this->options['lightgallery']['preload'] : 1,
      '#description' => t('number of preload slides. will exicute only after the current slide is fully loaded. ' . 'ex:// you clicked on 4th image and if preload = 1 then 3rd slide and 5th slide will be loaded in the background after the 4th slide is fully loaded. ' . 'If preload is 2 then 2nd 3rd 5th 6th slides will be preloaded'),
    );
    $form['lightgallery_core']['closable'] = array(
      '#type' => 'checkbox',
      '#title' => t('Closable'),
      '#default_value' => isset($this->options['lightgallery']['closable']) ? $this->options['lightgallery']['closable'] : TRUE,
      '#description' => t('Allows clicks on dimmer to close gallery.'),
    );
    $form['lightgallery_core']['loop'] = array(
      '#type' => 'checkbox',
      '#title' => t('Loop'),
      '#default_value' => isset($this->options['lightgallery']['loop']) ? $this->options['lightgallery']['loop'] : TRUE,
      '#description' => t('If not selected, the ability to loop back to the beginning of the gallery when on the last element, will be disabled.'),
    );
    $form['lightgallery_core']['esc_key'] = array(
      '#type' => 'checkbox',
      '#title' => t('Escape key'),
      '#default_value' => isset($this->options['lightgallery']['esc_key']) ? $this->options['lightgallery']['esc_key'] : TRUE,
      '#description' => t('Whether the LightGallery could be closed by pressing the "Esc" key.'),
    );
    $form['lightgallery_core']['key_press'] = array(
      '#type' => 'checkbox',
      '#title' => t('Keyboard'),
      '#default_value' => isset($this->options['lightgallery']['key_press']) ? $this->options['lightgallery']['key_press'] : TRUE,
      '#description' => t('Enable keyboard navigation.'),
    );
    $form['lightgallery_core']['controls'] = array(
      '#type' => 'checkbox',
      '#title' => t('Controls'),
      '#default_value' => isset($this->options['lightgallery']['controls']) ? $this->options['lightgallery']['controls'] : TRUE,
      '#description' => t('If not checked, prev/next buttons will not be displayed.'),
    );
    $form['lightgallery_core']['mouse_wheel'] = array(
      '#type' => 'checkbox',
      '#title' => t('Mouse wheel'),
      '#default_value' => isset($this->options['lightgallery']['mouse_wheel']) ? $this->options['lightgallery']['mouse_wheel'] : TRUE,
      '#description' => t('Chane slide on mousewheel'),
    );
    $form['lightgallery_core']['download'] = array(
      '#type' => 'checkbox',
      '#title' => t('Download'),
      '#default_value' => isset($this->options['lightgallery']['download']) ? $this->options['lightgallery']['download'] : TRUE,
      '#description' => t('Enable download button. ' . 'By default download url will be taken from data-src/href attribute but it supports only for modern browsers.'),
    );
    $form['lightgallery_core']['counter'] = array(
      '#type' => 'checkbox',
      '#title' => t('Counter'),
      '#default_value' => isset($this->options['lightgallery']['counter']) ? $this->options['lightgallery']['counter'] : TRUE,
      '#description' => t('Whether to show total number of images and index number of currently displayed image.'),
    );
    $form['lightgallery_core']['drag'] = array(
      '#type' => 'checkbox',
      '#title' => t('Drag'),
      '#default_value' => isset($this->options['lightgallery']['drag']) ? $this->options['lightgallery']['drag'] : TRUE,
      '#description' => t('Enables desktop mouse drag support.'),
    );
    $form['lightgallery_core']['touch'] = array(
      '#type' => 'checkbox',
      '#title' => t('Touch'),
      '#default_value' => isset($this->options['lightgallery']['touch']) ? $this->options['lightgallery']['touch'] : TRUE,
      '#description' => t('Enables touch support.'),
    );

    // THUMB OPTIONS.
    $form['lightgallery_thumbs'] = array(
      '#type' => 'fieldset',
      '#title' => t('Lightgallery thumbnail settings'),
      '#collapsible' => TRUE,
      '#collapsed' => isset($this->options['lightgallery']['thumbnails']) ? !$this->options['lightgallery']['thumbnails'] : FALSE,
    );
    $form['lightgallery_thumbs']['thumbnails'] = array(
      '#type' => 'checkbox',
      '#title' => t('Use thumbnails'),
      '#default_value' => isset($this->options['lightgallery']['thumbnails']) ? $this->options['lightgallery']['thumbnails'] : TRUE,
      '#description' => t('Indicate if you want to use thumbnails in the LightGallery.'),
    );
    $form['lightgallery_thumbs']['animate_thumb'] = array(
      '#type' => 'checkbox',
      '#title' => t('Animate thumbnails'),
      '#default_value' => isset($this->options['lightgallery']['animate_thumb']) ? $this->options['lightgallery']['animate_thumb'] : TRUE,
      '#description' => t('Enable thumbnail animation.'),
    );
    $form['lightgallery_thumbs']['current_pager_position'] = array(
      '#type' => 'select',
      '#title' => t('Position'),
      '#options' => array(
        'left' => t('Left'),
        'middle' => t('Middle'),
        'right' => t('Right'),
      ),
      '#default_value' => isset($this->options['lightgallery']['current_pager_position']) ? $this->options['lightgallery']['current_pager_position'] : 'middle',
      '#description' => t('Position of selected thumbnail.'),
    );
    $form['lightgallery_thumbs']['thumb_width'] = array(
      '#type' => 'textfield',
      '#title' => t('Width'),
      '#default_value' => isset($this->options['lightgallery']['thumb_width']) ? $this->options['lightgallery']['thumb_width'] : 100,
      '#description' => t('Width of each thumbnails.'),
    );
    $form['lightgallery_thumbs']['thumb_cont_height'] = array(
      '#type' => 'textfield',
      '#title' => t('Height'),
      '#default_value' => isset($this->options['lightgallery']['thumb_cont_height']) ? $this->options['lightgallery']['thumb_cont_height'] : 100,
      '#description' => t('Height of the thumbnail container including padding and border.'),
    );

    // AUTOPLAY OPTIONS.
    $form['lightgallery_autoplay'] = array(
      '#type' => 'fieldset',
      '#title' => t('Lightgallery autoplay settings'),
      '#collapsible' => TRUE,
      '#collapsed' => isset($this->options['lightgallery']['autoplay']) ? !$this->options['lightgallery']['autoplay'] : FALSE,
    );
    $form['lightgallery_autoplay']['autoplay'] = array(
      '#type' => 'checkbox',
      '#title' => t('Autoplay'),
      '#default_value' => isset($this->options['lightgallery']['autoplay']) ? $this->options['lightgallery']['autoplay'] : TRUE,
      '#description' => t('Enable/Disable gallery autoplay.'),
    );
    $form['lightgallery_autoplay']['pause'] = array(
      '#type' => 'textfield',
      '#title' => t('Pause'),
      '#default_value' => isset($this->options['lightgallery']['pause']) ? $this->options['lightgallery']['pause'] : 5000,
      '#description' => t('The time (in ms) between each auto transition.'),
    );
    $form['lightgallery_autoplay']['progress_bar'] = array(
      '#type' => 'checkbox',
      '#title' => t('Progress bar'),
      '#default_value' => isset($this->options['lightgallery']['progress_bar']) ? $this->options['lightgallery']['progress_bar'] : TRUE,
      '#description' => t('Enable/Disable autoplay progress bar.'),
    );
    $form['lightgallery_autoplay']['autoplay_controls'] = array(
      '#type' => 'checkbox',
      '#title' => t('Autoplay controls'),
      '#default_value' => isset($this->options['lightgallery']['autoplay_controls']) ? $this->options['lightgallery']['autoplay_controls'] : TRUE,
      '#description' => t('Show/hide autoplay controls.'),
    );

    // FULL SCREEN OPTIONS.
    $form['lightgallery_full_screen'] = array(
      '#type' => 'fieldset',
      '#title' => t('Lightgallery fullscreen settings'),
      '#collapsible' => TRUE,
      '#collapsed' => isset($this->options['lightgallery']['full_screen']) ? !$this->options['lightgallery']['full_screen'] : FALSE,
    );
    $form['lightgallery_full_screen']['full_screen'] = array(
      '#type' => 'checkbox',
      '#title' => t('Full screen'),
      '#default_value' => isset($this->options['lightgallery']['full_screen']) ? $this->options['lightgallery']['full_screen'] : TRUE,
      '#description' => t('Enable/Disable fullscreen mode.'),
    );

    // PAGER OPTIONS.
    $form['lightgallery_pager'] = array(
      '#type' => 'fieldset',
      '#title' => t('Lightgallery pager settings'),
      '#collapsible' => TRUE,
      '#collapsed' => isset($this->options['lightgallery']['pager']) ? !$this->options['lightgallery']['pager'] : FALSE,
    );
    $form['lightgallery_pager']['pager'] = array(
      '#type' => 'checkbox',
      '#title' => t('Pager'),
      '#default_value' => isset($this->options['lightgallery']['pager']) ? $this->options['lightgallery']['pager'] : TRUE,
      '#description' => t('Enable/Disable pager.'),
    );

    // ZOOM OPTIONS.
    $form['lightgallery_zoom'] = array(
      '#type' => 'fieldset',
      '#title' => t('Lightgallery zoom settings'),
      '#collapsible' => TRUE,
      '#collapsed' => isset($this->options['lightgallery']['zoom']) ? !$this->options['lightgallery']['zoom'] : FALSE,
    );
    $form['lightgallery_zoom']['zoom'] = array(
      '#type' => 'checkbox',
      '#title' => t('Zoom'),
      '#default_value' => isset($this->options['lightgallery']['zoom']) ? $this->options['lightgallery']['zoom'] : TRUE,
      '#description' => t('Enable/Disable zoom option.'),
    );
    $form['lightgallery_zoom']['scale'] = array(
      '#type' => 'select',
      '#title' => t('Scale'),
      '#options' => drupal_map_assoc(array(
        1,
        2,
        3,
        4,
      )),
      '#default_value' => isset($this->options['lightgallery']['scale']) ? $this->options['lightgallery']['scale'] : 1,
      '#description' => t('Value of zoom that should be incremented/decremented.'),
    );

    // HASH OPTIONS.
    $form['lightgallery_hash'] = array(
      '#type' => 'fieldset',
      '#title' => t('Lightgallery hash settings'),
      '#collapsible' => TRUE,
      '#collapsed' => isset($this->options['lightgallery']['hash']) ? !$this->options['lightgallery']['hash'] : FALSE,
    );
    $form['lightgallery_hash']['hash'] = array(
      '#type' => 'checkbox',
      '#title' => t('Update hash in url'),
      '#default_value' => isset($this->options['lightgallery']['hash']) ? $this->options['lightgallery']['hash'] : TRUE,
      '#description' => t('Enable/Disable hash option.'),
    );
    $form['lightgallery_hash']['gallery_id'] = array(
      '#type' => 'textfield',
      '#title' => t('Gallery ID'),
      '#default_value' => isset($this->options['lightgallery']['gallery_id']) ? $this->options['lightgallery']['gallery_id'] : 1,
      '#description' => t('Unique id for each gallery. It is mandatory when you use hash plugin for multiple galleries on the same page.'),
    );
  }

  /**
   * Add form validation for options.
   */
  function options_validate(&$form, &$form_state) {
    $style_options = $form_state['values']['style_options'];

    // Flatten style options array.
    $form_state['values']['style_options']['lightgallery'] = $this
      ->flatten_array($style_options);
    parent::options_validate($form, $form_state);
  }
  function validate() {
    parent::validate();
    $errors = array();

    // Make sure the pager is not enabled.
    if ($this->display->handler
      ->use_pager()) {
      $errors[] = t('The Lighgallery style cannot be used with a pager. Please disable the "Use pager" option for this display.');
    }
    return $errors;
  }

  /**
   * Renders the view fields.
   * @param type $result
   * @return type
   */
  function render_fields($result) {
    $this->rendered_fields = array();
    $this->view->row_index = 0;
    $keys = array_keys($this->view->field);

    // If all fields have a field::access FALSE there might be no fields, so
    // there is no reason to execute this code.
    if (!empty($keys)) {
      $fields = $this
        ->conf_get_field_sources();
      $image_fields = array_keys($fields['field_options_images']);
      foreach ($result as $count => $row) {
        $this->view->row_index = $count;
        foreach ($keys as $id) {
          if (in_array($id, $image_fields)) {

            // This is an image/thumb field.
            // Create URI for selected image style.
            $image_style = $this->view->field[$id]->options['settings']['image_style'];
            $uri = !empty($result[$count]->{'field_' . $id}[0]['raw']['uri']) ? $result[$count]->{'field_' . $id}[0]['raw']['uri'] : FALSE;
            if ($uri) {
              if (!empty($image_style)) {
                $this->rendered_fields[$count][$id] = file_create_url(image_style_url($image_style, $uri));
              }
              else {
                $this->rendered_fields[$count][$id] = file_create_url($uri);
              }
            }
          }
          else {

            // Just render the field as views would do.
            $this->rendered_fields[$count][$id] = $this->view->field[$id]
              ->theme($row);
          }
        }
        $this->row_tokens[$count] = $this->view->field[$id]
          ->get_render_tokens(array());
      }
    }
    unset($this->view->row_index);
    return $this->rendered_fields;
  }

  /**
   * Fetches field options for all fields definied in view.
   * @return type
   */
  private function conf_get_field_sources() {
    $view = $this->view;
    $options = array(
      'field_options_images' => array(),
      'field_options_images_type' => array(),
      'field_options' => array(),
    );
    $field_handlers = $this->display->handler
      ->get_handlers('field');
    if (!empty($view->base_table) && $view->base_table == 'file_managed') {
      $options['field_options_images_type']['file_base'] = 'file_base';
    }
    $field_labels = $this->display->handler
      ->get_field_labels();

    // Get the label for each field.
    foreach ($field_handlers as $field => $handler) {
      $name = $field_labels[$field];

      // Separate image fields from non-image fields. For image fields we can
      // work with fids and fields of type image or file.
      if (isset($handler->field) && $handler->field == 'fid') {
        $options['field_options_images'][$field] = $name;
        $options['field_options_images_type'][$field] = 'file_id_field';
      }
      elseif (isset($handler->field_info['type']) && ($handler->field_info['type'] == 'image' || $handler->field_info['type'] == 'file')) {
        $options['field_options_images'][$field] = $name;
        $options['field_options_images_type'][$field] = 'file_field';
      }
      else {
        $options['field_options'][$field] = $name;
      }
    }
    return $options;
  }

  /**
   * Flatten array and preserve keys.
   * @param array $array
   * @return array
   */
  private function flatten_array(array $array) {
    $flattened_array = array();
    array_walk_recursive($array, function ($a, $key) use (&$flattened_array) {
      $flattened_array[$key] = $a;
    });
    return $flattened_array;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
lightgallery_style_plugin::conf_get_field_sources private function Fetches field options for all fields definied in view.
lightgallery_style_plugin::flatten_array private function Flatten array and preserve keys.
lightgallery_style_plugin::options_form function Adds Lightgallery configuration form options. Overrides views_plugin_style::options_form
lightgallery_style_plugin::options_validate function Add form validation for options. Overrides views_plugin_style::options_validate
lightgallery_style_plugin::option_definition function Provide default options. Overrides views_plugin_style::option_definition
lightgallery_style_plugin::render_fields function Renders the view fields. Overrides views_plugin_style::render_fields
lightgallery_style_plugin::validate function Validate that the plugin is correct and can be saved. Overrides views_plugin_style::validate
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::options_submit public function Handle any special handling on the validate form. 9
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::init public function Initialize a style plugin.
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 public function Render the display in this style. 5
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.