You are here

views_showcase.module in Views Showcase 6.2

Same filename and directory in other branches
  1. 6 views_showcase.module
  2. 7 views_showcase.module

The implementation of Views Showcase module.

File

views_showcase.module
View source
<?php

/**
 * @file
 *  The implementation of Views Showcase module.
 */

/**
 * Implementation of hook_views_api().
 */
function views_showcase_views_api() {
  return array(
    'api' => 2,
  );
}

/**
 * Function to check and retreive libraries.
 */
function views_showcase_get_libraries() {
  $libraries['cycle'] = NULL;
  $libraries['easing'] = NULL;
  if ($path = libraries_get_path('cycle')) {
    $filename = reset(file_scan_directory($path, ".*.js"));
    $libraries['cycle'] = $filename->filename;
  }
  if ($path = libraries_get_path('easing')) {
    $filename = reset(file_scan_directory($path, ".*.js"));
    $libraries['easing'] = $filename->filename;
  }
  return $libraries;
}

/**
 * Implementation of template preprocess for the view
 */
function template_preprocess_views_showcase_view(&$vars) {
  $view = $vars['view'];
  $options = $view->style_plugin->options;
  $row = $view->row_plugin->options;

  // Values to pass to the template
  $vars['skin'] = $options['skin'];
  $vars['views_showcase_id'] = 'views-showcase-' . $view->name . '-' . $view->current_display;

  // Passing the display title, translated if i18nviews is enabled
  if (isset($view->display[$view->current_display]->display_options['title'])) {
    $untranslated_title = $view->display[$view->current_display]->display_options['title'];

    // For i18nviews integration
    $i18nstrings_context = 'views:' . $view->name . ':' . $view->current_display . ':title';
  }
  else {
    $untranslated_title = $view->display['default']->display_options['title'];

    // For i18nviews integration
    $i18nstrings_context = 'views:' . $view->name . ':default:title';
  }
  if (function_exists(i18nstrings)) {
    $vars['display_title'] = i18nstrings($i18nstrings_context, $untranslated_title);
  }
  else {
    $vars['display_title'] = $untranslated_title;
  }
  drupal_add_js(array(
    'views_showcase' => array(
      'easing' => $options['easing'],
      'cycle' => $options['cycle'],
      'sync' => $options['sync'],
      'timeout' => $options['timeout'],
      'listPause' => $options['listpause'],
      'pause' => $options['pause'],
    ),
  ), 'setting');
  $libs = views_showcase_get_libraries();
  drupal_add_js($libs['cycle']);
  if (!empty($options['easing'])) {
    drupal_add_js($libs['easing']);
  }
  drupal_add_js(drupal_get_path('module', 'views_showcase') . '/js/views_showcase.js');
  drupal_add_css(drupal_get_path('module', 'views_showcase') . '/css/views_showcase.css');
  $is_row_odd = TRUE;

  //Looping through the rows returned by the view to pass them in a proper structure to the template
  foreach ($view->result as $row_index => $row_to_render) {

    //Adding classes
    $classes = array();
    $classes[] = 'views-row';
    $classes[] = 'views-row-' . ($row_index + 1);
    if ($is_row_odd) {
      $classes[] = 'views-row-odd';
    }
    else {
      $classes[] = 'views-row-even';
    }
    if ($row_index === 0) {
      $classes[] = 'views-row-first';
    }
    if ($row_index === count($view->result) - 1) {
      $classes[] = 'views-row-last';
    }
    $nav_box_classes = $classes;
    $nav_box_classes[] = 'views-showcase-pager-item';
    $nav_box_classes = implode(' ', $nav_box_classes);
    $big_box_classes = $classes;
    $big_box_classes[] = 'views-showcase-item';
    $big_box_classes = implode(' ', $big_box_classes);
    $vars['structured_rows'][$row_index]['nav_box_classes'] = $nav_box_classes;
    $vars['structured_rows'][$row_index]['big_box_classes'] = $big_box_classes;
    $vars['structured_rows'][$row_index]['anchor_name'] = 'showcase-' . $vars['views_showcase_id'] . '-' . $row_index;
    $is_row_odd = !$is_row_odd;

    //Looping through the fields of each returned row
    foreach ($view->field as $id => $field) {

      // Render this even if set to exclude so it can be used elsewhere.
      $field_output = $view->field[$id]
        ->theme($row_to_render);
      if (empty($field->options['exclude'])) {
        $object = new stdClass();
        $object->content = $field_output;
        $object->handler =& $view->field[$id];
        $object->element_type = $object->handler
          ->element_type();
        $object->class = views_css_safe($id);
        $object->label = check_plain($view->field[$id]
          ->label());

        //First assume this field doesn't appear anywhere
        $object->big_box = FALSE;
        $object->navigation_box = FALSE;
        $object->link_anchor = FALSE;

        //Setting the flags for each field in the row according to the what was chosen in the settings
        if ($options['showcase_display_options']['big_box_field'][$id] != '0') {
          $object->big_box = TRUE;
        }
        if ($options['showcase_display_options']['link_anchor_field'][$id] != '0') {
          $object->link_anchor = TRUE;
        }
        if ($options['showcase_display_options']['navigation_box_field'][$id] != '0') {
          $object->navigation_box = TRUE;
        }

        //Now send the structured row objects to the template
        $vars['structured_rows'][$row_index][$id] = $object;
      }
    }
  }
}

/**
 * Implementation of hook_theme().
 */
function views_showcase_theme() {
  return array(
    'views_showcase_checkbox_table' => array(
      'arguments' => array(
        'element' => NULL,
      ),
    ),
  );
}

/**
 * Theming the style options form to render the table with the checkboxes used to choose where to display each field
 */
function theme_views_showcase_checkbox_table($element) {

  //Build the header of the table to be rendered
  $header = array(
    t('Field ID'),
    t('Big Box'),
    t('Navigation Box'),
    t('Link to Anchor'),
  );

  //Build the content of the table
  foreach ($element['navigation_box_field']['#options'] as $field_name => $field) {
    $row = array();

    //First Column the name of the field
    $row[] = $field;

    //Emptying the labels so that they aren't rendered for each checkbox in the table
    $element['big_box_field'][$field_name]['#title'] = '';
    $element['navigation_box_field'][$field_name]['#title'] = '';
    $element['link_anchor_field'][$field_name]['#title'] = '';

    //Now using the rendered HTML of each field and add it to the table
    $row[] = drupal_render($element['big_box_field'][$field_name]);
    $row[] = drupal_render($element['navigation_box_field'][$field_name]);
    $row[] = drupal_render($element['link_anchor_field'][$field_name]);

    //Stack all rows in one array
    $rows[] = $row;
  }

  //Render as an HTML table and return
  return theme('table', $header, $rows);
}

Functions

Namesort descending Description
template_preprocess_views_showcase_view Implementation of template preprocess for the view
theme_views_showcase_checkbox_table Theming the style options form to render the table with the checkboxes used to choose where to display each field
views_showcase_get_libraries Function to check and retreive libraries.
views_showcase_theme Implementation of hook_theme().
views_showcase_views_api Implementation of hook_views_api().