You are here

class juicebox_style_plugin in Juicebox HTML5 Responsive Image Galleries 7

Style plugin to render each item in a views list.

Hierarchy

Expanded class hierarchy of juicebox_style_plugin

1 string reference to 'juicebox_style_plugin'
juicebox_views_plugins in ./juicebox.views.inc
Implements hook_views_plugin().

File

plugins/juicebox_style_plugin.inc, line 15
Contains the Juicebox views style plugin.

View source
class juicebox_style_plugin extends views_plugin_style {

  /**
   * Define default plugin options.
   */
  function option_definition() {
    $options = parent::option_definition();
    $options = array(
      'image_field' => array(
        'default' => '',
      ),
      'image_field_style' => array(
        'default' => '',
      ),
      'thumb_field' => array(
        'default' => '',
      ),
      'thumb_field_style' => array(
        'default' => 'thumbnail',
      ),
      'title_field' => array(
        'default' => '',
      ),
      'caption_field' => array(
        'default' => '',
      ),
      'show_title' => array(
        'default' => 0,
      ),
      'jlib_galleryWidth' => array(
        'default' => '100%',
      ),
      'jlib_galleryHeight' => array(
        'default' => '100%',
      ),
      'jlib_backgroundColor' => array(
        'default' => '#222222',
      ),
      'jlib_textColor' => array(
        'default' => 'rgba(255,255,255,1)',
      ),
      'jlib_thumbFrameColor' => array(
        'default' => 'rgba(255,255,255,.5)',
      ),
      'jlib_showOpenButton' => array(
        'default' => 1,
      ),
      'jlib_showExpandButton' => array(
        'default' => 1,
      ),
      'jlib_showThumbsButton' => array(
        'default' => 1,
      ),
      'jlib_useThumbDots' => array(
        'default' => 0,
      ),
      'jlib_useFullscreenExpand' => array(
        'default' => 0,
      ),
      'manual_config' => array(
        'default' => '',
      ),
      'custom_parent_classes' => array(
        'default' => '',
      ),
      'apply_markup_filter' => array(
        'default' => 1,
      ),
      'linkurl_source' => array(
        'default' => '',
      ),
      'linkurl_target' => array(
        'default' => '_blank',
      ),
    );
    return $options;
  }

  /**
   * Define plugin options form.
   */
  function options_form(&$form, &$form_state) {
    parent::options_form($form, $form_state);

    // Get the active field options
    $field_options = array();
    $field_options_images = array();
    $field_handlers = $this->display->handler
      ->get_handlers('field');
    foreach ($field_handlers as $field => $handler) {
      if ($label = $handler
        ->label()) {
        $name = $label;
      }
      else {
        $name = $handler
          ->ui_name();
      }

      // Separate image fields from non-image fields
      if (isset($handler->field_info['type'])) {
        if ($handler->field_info['type'] == 'image') {
          $field_options_images[$field] = $name;
        }
        else {
          $field_options[$field] = $name;
        }
      }
      else {
        $field_options[$field] = $name;
      }
    }

    // Get available image style presets
    $presets = image_style_options(FALSE);

    // Initialize the "settings" values before working with them. This is
    // required for legacy support.
    $settings = _juicebox_init_display_settings($this->options);

    // Start with the "common" form elements.
    $form = _juicebox_common_form_elements($form, $settings);

    // Add the view-specific elements.
    $form['image_field'] = array(
      '#type' => 'select',
      '#title' => t('Main Image Field'),
      '#default_value' => $settings['image_field'],
      '#description' => t('The view\'s image field that should be used for each image in the gallery.'),
      '#options' => $field_options_images,
    );
    $form['image_field_style'] = array(
      '#type' => 'select',
      '#title' => t('Main Image Field Style'),
      '#default_value' => $settings['image_field_style'],
      '#description' => t('The style formatter for the image. Any formatting settings configured on the field itself will be ignored and this style setting will always be used.'),
      '#options' => $presets,
      '#empty_option' => t('None (original image)'),
    );
    $form['thumb_field'] = array(
      '#type' => 'select',
      '#title' => t('Thumbnail Field'),
      '#default_value' => $settings['thumb_field'],
      '#description' => t('The view\'s image field that should be used for each thumbnail in the gallery. Typically you will choose the same value that was set in the "Main Image Field" option above.'),
      '#options' => $field_options_images,
    );
    $form['thumb_field_style'] = array(
      '#type' => 'select',
      '#title' => t('Thumbnail Field Style'),
      '#default_value' => $settings['thumb_field_style'],
      '#description' => t('The style formatter for the thumbnail. Any formatting settings configured on the field itself will be ignored and this style setting will always be used.'),
      '#options' => $presets,
      '#empty_option' => t('None (original image)'),
    );
    $form['title_field'] = array(
      '#type' => 'select',
      '#title' => t('Title Field'),
      '#default_value' => $settings['title_field'],
      '#description' => t('The view\'s field that should be used for the title of each image in the gallery. Any formatting settings configured on the field itself will be respected.'),
      '#options' => $field_options,
      '#empty_option' => t('None'),
    );
    $form['caption_field'] = array(
      '#type' => 'select',
      '#title' => t('Caption Field'),
      '#default_value' => $settings['caption_field'],
      '#description' => t('The view\'s field that should be used for the caption of each image in the gallery. Any formatting settings configured on the field itself will be respected.'),
      '#options' => $field_options,
      '#empty_option' => t('None'),
    );
    $form['show_title'] = array(
      '#type' => 'checkbox',
      '#title' => t('Show Gallery Title'),
      '#default_value' => $settings['show_title'],
      '#description' => t('Show the view display title as the gallery title.'),
    );

    // Add view-sepcific field options for the linkURL setting.
    $linkurl_field_options = array();
    foreach ($field_options as $field_key => $field_name) {
      $linkurl_field_options[$field_key] = t('Field') . ' - ' . $field_name;
    }

    // Adjust some advanced settings that are specific to views.
    $form['linkurl_source']['#description'] = $form['linkurl_source']['#description'] . '</br><strong>' . t('If using a field for this source the field must render a properly formatted URL and nothing else.') . '</strong>';
    $form['linkurl_source']['#options'] = array_merge($form['linkurl_source']['#options'], $linkurl_field_options);
  }

  /**
   * Define validation rules for the plugin display.
   */
  function validate() {
    $errors = parent::validate();

    // Make sure block caching is not enabled. With block caching enabled the
    // Juicebox javascript library may not get included.
    if (isset($this->display->handler->options['block_caching']) && $this->display->handler->options['block_caching'] != DRUPAL_NO_CACHE) {
      $errors[] = t('The Juicebox style cannot be used with block caching. Please disable the "Block caching" option for this display');
    }

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

    // We want to somewhat "nag" the user if they have not yet configured the
    // Juicebox-specific plugin settings (because things won't work until they
    // do). However, we do NOT want to formally set an error. This is because
    // this validate method can run on pages where the user can't actaully touch
    // the Juicebox-specific plugin settings (such as
    // admin/structure/views/add).
    if (empty($this->options['image_field'])) {
      drupal_set_message(t("To ensure a fully functional Juicebox gallery please remember to add at least one image field to your Juicebox view display and configure the Juicebox Gallery format settings."), 'warning', FALSE);
    }
    return $errors;
  }

  /**
   * Render the view page display.
   *
   * This is where the Juicebox embed code is built for the view.
   */
  function render() {
    $view = $this->view;

    // Generate a unique ID that can be used to identify this gallery and view
    // source details.
    $xml_id = 'view/' . $view->name . '/' . $view->current_display;
    foreach ($view->args as $arg) {
      $xml_id .= '/' . $arg;
    }
    $xml_path = 'juicebox/xml/' . $xml_id;

    // If we are previewing the view in the admin interface all the necessary
    // <script> elements in our embed markup seem to get stripped. This means
    // that the preview cannot render and so there is no point in loading the
    // Juicebox lib (doing so can actually cause display problems).
    if (strpos(current_path(), 'admin/structure/views') !== 0) {

      // Load the juicebox library, this will include the appropriate js in the
      // page header, etc.
      libraries_load('juicebox');
    }
    else {

      // Post a notice to admins so that they don't think things are broken
      // when the preview does not produce anything useful.
      drupal_set_message(t("Due to javascript requirements Juicebox galleries cannot be viewed as a live preview. Please save your view and visit the full page URL for this display to preview this gallery."), 'warning');
    }

    // Initialize the "settings" values before working with them. This is
    // required for legacy support.
    $view->style_plugin->options = _juicebox_init_display_settings($view->style_plugin->options);

    // Calculate the data that will ultimately be used to render the gallery
    // XML. We won't officially generate the XML until the Juicebox javascript
    // requests it, but we will still need the related data within parts of the
    // embed code.
    $view->juicebox_gallery_data = juicebox_build_gallery_data_from_view($view, $xml_path);

    // Get a checksum for the gallery data. This can be useful for invalidating
    // any old caches of this data. Note json_encode() is faster than
    // serialize().
    $gallery_checksum = md5(json_encode($view->juicebox_gallery_data));

    // Construct the query parameters that should be added to the XML path. Be
    // sure to retain any currently active query parameters.
    $query = array_merge(array(
      'checksum' => $gallery_checksum,
    ), drupal_get_query_parameters());

    // Set template variables for embed markup.
    $variables['gallery_id'] = preg_replace('/[^0-9a-zA-Z-]/', '_', str_replace('/', '-', $xml_id));
    $variables['gallery_xml_path'] = url($xml_path, array(
      'query' => $query,
    ));
    $variables['settings'] = $view->style_plugin->options;
    $variables['data'] = $view->juicebox_gallery_data;
    return theme('juicebox_embed_markup', $variables);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
juicebox_style_plugin::options_form function Define plugin options form. Overrides views_plugin_style::options_form
juicebox_style_plugin::option_definition function Define default plugin options. Overrides views_plugin_style::option_definition
juicebox_style_plugin::render function Render the view page display. Overrides views_plugin_style::render
juicebox_style_plugin::validate function Define validation rules for the plugin display. 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::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.