You are here

views_rows_wrapper.module in Views Rows Wrapper 7

Same filename and directory in other branches
  1. 8.2 views_rows_wrapper.module
  2. 8 views_rows_wrapper.module

Views Rows Wrapper module.

File

views_rows_wrapper.module
View source
<?php

/**
 * @file
 * Views Rows Wrapper module.
 */

/**
 * Implements hook_help().
 *
 * @inheritdoc
 */
function views_rows_wrapper_help($path) {
  switch ($path) {
    case "admin/help#views_rows_wrapper":
      return t("This is simple view display style plugin, that combines user defined number of rows into sets, wrapped by chosen HTML element and attribute.");
      break;
  }
}

/**
 * Returns wrapper element types().
 */
function views_rows_wrapper_element_types() {
  return [
    "DIV",
    "SPAN",
  ];
}

/**
 * Returns wrapper attribute types().
 */
function views_rows_wrapper_attribute_types() {
  return [
    "CLASS",
    "ID",
  ];
}

/**
 * Implements hook_views_api().
 */
function views_rows_wrapper_views_api() {
  return [
    'api' => 3,
  ];
}

/**
 * Implements hook_preprocess_HOOK() for theme_views_rows_wrapper().
 *
 * @inheritdoc
 */
function template_preprocess_views_rows_wrapper(&$vars) {
  $view = $vars['view'];
  $rows = $vars['rows'];
  $style = $view->style_plugin;
  $options = $style->options;
  $element_types = views_rows_wrapper_element_types();
  $attribute_types = views_rows_wrapper_attribute_types();
  $use_wrapper = isset($options['use_wrapper']) ? $options['use_wrapper'] : FALSE;
  $element_type = isset($options['element_type']) ? $element_types[$options['element_type']] : $element_types[0];
  $element_type = strtolower($element_type);
  $attribute_type = isset($options['attribute_type']) ? $attribute_types[$options['attribute_type']] : $attribute_types[0];
  $attribute_type = strtolower($attribute_type);
  $attribute_name = isset($options['attribute_name']) ? $options['attribute_name'] : '';
  $attribute_name = preg_replace('/[^a-zA-Z0-9-_\\s]/', '', $attribute_name);
  $rows_number = isset($options['rows_number']) ? $options['rows_number'] : 2;
  $wrap_method = isset($options['wrap_method']) ? $options['wrap_method'] : 1;
  $default_rows = isset($options['default_rows']) ? $options['default_rows'] : FALSE;
  $strip_rows = isset($options['strip_rows']) ? $options['strip_rows'] : FALSE;
  $count = 0;
  $k = 0;
  $max = count($rows);
  $is_wrapped_once = FALSE;
  foreach ($rows as $row) {
    $classes = array();
    $count++;
    $k++;
    if ($default_rows) {
      $classes[] = 'views-row';
      $classes[] = 'views-row-' . $count;
    }
    if ($strip_rows) {
      $classes[] = 'views-row-' . ($count % 2 ? 'odd' : 'even');
      if ($count == 1) {
        $classes[] = 'views-row-first';
      }
      if ($count == $max) {
        $classes[] = 'views-row-last';
      }
    }
    if ($default_rows || $strip_rows) {
      $class = implode(" ", $classes);
      $row = '<div class="' . $class . '">' . $row . '</div>';
    }
    if ($use_wrapper && !$is_wrapped_once) {
      if ($k == 1) {
        $row = '<' . $element_type . ' ' . $attribute_type . '="' . $attribute_name . '">' . $row;
      }
      if ($count % $rows_number == 0 || $count == $max && $k < $rows_number) {
        $row = $row . '</' . $element_type . '>';
      }
    }
    if ($k >= $rows_number) {
      $k = 0;
      if ($wrap_method == 1) {
        $is_wrapped_once = TRUE;
      }
    }
    $vars['rows_wrapped'][] = $row;
  }
}

Functions

Namesort descending Description
template_preprocess_views_rows_wrapper Implements hook_preprocess_HOOK() for theme_views_rows_wrapper().
views_rows_wrapper_attribute_types Returns wrapper attribute types().
views_rows_wrapper_element_types Returns wrapper element types().
views_rows_wrapper_help Implements hook_help().
views_rows_wrapper_views_api Implements hook_views_api().