views_column_class.module in Views Column Class 7
@todo.
File
views_column_class.moduleView source
<?php
/**
* @file
* @todo.
*/
/**
* Implements hook_help().
*/
function views_column_class_help($section = 'admin/help#views_column_class', $arg = NULL) {
$output = '';
switch ($section) {
case 'admin/help#views_column_class':
$output = t('<p>This module allows you to assign class to the listed items of an ordered or unordered list that is generate as a display in views.</p>
<p>The main application for this is when generating blocks that will be styled to appear in columns.</p>
<p>It is even more powerful when used with themes with responsive design because it encourages you to use grid class to specify the width of your block which can then be given a different width depending on the gird that is being apply based upon the screen resolution.</p>
<p>You can address the problem of having margin on items that in your left and right columns by giving those on the left a class of alpha and those on the right a class of omega.</p>
<p>These means that you can avoid setting fixed widths on blocks that wrap your view and giving the outer wrapping div and overflow of hidden to accommodate the margin on the right of your blocks.</p>
<p>Additionally, you can apply multiple column class settings using the advanced classes textarea. This has the potential of allow you to display your items in 4 columns for higher screen resolutions and 2 columns for your modile devices.</p>');
break;
}
return $output;
}
/**
* Implements hook_views_api().
*/
function views_column_class_views_api() {
return array(
'api' => 3,
);
}
/**
* Implements template_preprocess_HOOK().
*
* Populate each instance of $variables['classes'][$id] where $id is the
* current position in the view results with the classes specified by the
* primary and advanced settings in our form.
*
* @see template_preprocess_views_view_list()
* @see views_column_class_prepare_advanced()
* @see views_column_class_set_row_classes()
*/
function template_preprocess_views_view_column_class(&$variables) {
template_preprocess_views_view_list($variables);
$rows = $variables['rows'];
$view = $variables['view'];
$options = $view->style_plugin->options;
$columns = $options['columns'];
$advanced_classes = views_column_class_prepare_advanced($options['advanced_classes']);
$count = 0;
foreach ($rows as $id => $row) {
views_column_class_set_row_classes($variables['classes'][$id], $count, $options['offset'], $columns, $options['class_column_left'], $options['class_column_right'], $options['column_class_prefix'], $options['row_class_prefix'], $options['column_row_class_prefix']);
foreach ($advanced_classes as $adv_class) {
views_column_class_set_row_classes($variables['classes'][$id], $count, $adv_class[1], $adv_class[0], $adv_class[2], $adv_class[3], $adv_class[4], $adv_class[5], $adv_class[6]);
}
// Flatten the classes to a string for each row for the template file.
$variables['classes_array'][$id] = implode(' ', preg_replace('/\\s\\s+/', ' ', $variables['classes'][$id]));
$count++;
}
}
/**
* Prepare array of values that will be applied as classes to a row
*
* @param mixed $vars_class_id
* Referenced value to store the classes to be be applied to the row template.
*
* @param int $count
* The current row in the view results.
*
* @param int $columns
* Number of columns imposed by the defined settings.
*
* @param string $class_left
* Add this value in the array to be applied if current position is in the
* first column of results.
*
* @param string $class_right
* Add this value in the array to be applied if current position is in the
* last column of results.
*
* @param string $column_class_prefix
* Add this value in each array if not blank to be applied. The current
* column position will be appended to this string.
*
* @param string $row_class_prefix
* Add this value in each array if not blank to be applied. The current row
* position will be appended to this string.
*
* @param string $column_row_class_prefix
* Add this value in each array if not blank to be applied. Hybrid value where
* values are substituted if {c}, {r}, {a} or {o} is detected in the string.
*/
function views_column_class_set_row_classes(&$variables_class_id, $count, $offset, $columns, $class_left, $class_right, $column_class_prefix, $row_class_prefix, $column_row_class_prefix) {
if ($columns > 0) {
if (!is_int($offset)) {
$offset = (int) $offset;
if ($offset < 0) {
$offset *= -1;
}
}
$count += $offset;
$combined_lr = array();
$combined_cr = array();
$triggers = array();
if ($count % $columns == 0) {
if ($class_left) {
$combined_lr[] = $class_left;
$variables_class_id[] = $class_left;
}
$triggers['a'] = TRUE;
}
if (($count + 1) % $columns == 0) {
if ($class_right) {
$combined_lr[] = $class_right;
$variables_class_id[] = $class_right;
}
$triggers['o'] = TRUE;
}
if (trim($column_class_prefix) != '') {
$combined_cr[] = trim($column_class_prefix) . ($count % $columns + 1);
$variables_class_id[] = trim($column_class_prefix) . ($count % $columns + 1);
}
$triggers['c'] = $count % $columns + 1;
if (trim($row_class_prefix) != '') {
$combined_cr[] = trim($row_class_prefix) . (floor($count / $columns) + 1);
$variables_class_id[] = trim($row_class_prefix) . (floor($count / $columns) + 1);
}
$triggers['r'] = floor($count / $columns) + 1;
$combined_classes = array_merge($combined_cr, $combined_lr);
if (count($combined_classes) > 0) {
$tmp = implode(' ', $combined_classes);
$tmp = preg_replace('/\\s+/', '-', $tmp);
if (!in_array($tmp, $variables_class_id)) {
$variables_class_id[] = $tmp;
}
}
if (trim($column_row_class_prefix) != '') {
$col = $count % $columns + 1;
$row = floor($count / $columns) + 1;
$tmp = trim($column_row_class_prefix);
preg_match_all('/([A-z0-9\\-_]*)\\{([crao])\\}/', $tmp, $out);
$search = array();
$replace = array();
// used to order the search strings in order of string length
// descending to avoid partial replaces
$order = array();
if (count($out[0]) > 0) {
for ($i = 0; $i < count($out[0]); $i++) {
$search[] = $out[0][$i];
$order[] = drupal_strlen($out[0][$i]);
$tmp_replace = '';
switch ($out[2][$i]) {
case 'c':
case 'r':
$tmp_replace = isset($triggers[$out[2][$i]]) ? $out[1][$i] . $triggers[$out[2][$i]] : '';
break;
case 'a':
case 'o':
$tmp_replace = isset($triggers[$out[2][$i]]) ? $out[1][$i] : '';
break;
}
$replace[] = $tmp_replace;
}
arsort($order);
$tmp_search = array();
$tmp_replace = array();
foreach ($order as $k => $v) {
$tmp_search[] = $search[$k];
$tmp_replace[] = $replace[$k];
}
$tmp = str_replace($tmp_search, $tmp_replace, $tmp);
}
$variables_class_id[] = $tmp;
}
}
$count++;
}
/**
* Parse the input from the Advanced classes textarea.
*
* @param string $advanced
* Expects one entry per line with each line accepting up to 4 values
* using | as a delimiter.
* (e.g. columns|class_left|class_right|column_class_prefix)
*
* @return
* array that has a depth of 2
* a collection of arrays of 4 values
* 1. The columns
* 2. The class to apply to the blocks in the left column
* 3. The class to apply to the blocks in the right column
* 4. The class to apply as a prefix in each column that will be followed by
* the column number
* 5. The class to apply as a prefix in each row that will be followed by the
* row number
* 6. Hybrid class string which potentially combines left, right, column and
* row values
*/
function views_column_class_prepare_advanced($advanced) {
$advanced_parsed = array();
$advanced = trim($advanced);
if ($advanced != '') {
$advanced_array = explode("\n", $advanced);
if (count($advanced_array) > 0) {
foreach ($advanced_array as $id => $adv_row) {
$adv_row_arr = explode('|', $adv_row);
$row_elements = count($adv_row_arr);
$row_values = array();
if ($row_elements >= 1 && (int) $adv_row_arr[0] > 0) {
$advanced_parsed[] = array(
$adv_row_arr[0],
$row_elements >= 2 && trim($adv_row_arr[1]) != '' ? trim($adv_row_arr[1]) : 0,
$row_elements >= 3 && trim($adv_row_arr[2]) != '' ? trim($adv_row_arr[2]) : FALSE,
$row_elements >= 4 && trim($adv_row_arr[3]) != '' ? trim($adv_row_arr[3]) : FALSE,
$row_elements >= 5 && trim($adv_row_arr[4]) != '' ? trim($adv_row_arr[4]) : '',
$row_elements >= 6 && trim($adv_row_arr[5]) != '' ? trim($adv_row_arr[5]) : '',
$row_elements >= 7 && trim($adv_row_arr[6]) != '' ? trim($adv_row_arr[6]) : '',
);
}
}
}
}
return $advanced_parsed;
}
Functions
Name![]() |
Description |
---|---|
template_preprocess_views_view_column_class | Implements template_preprocess_HOOK(). |
views_column_class_help | Implements hook_help(). |
views_column_class_prepare_advanced | Parse the input from the Advanced classes textarea. |
views_column_class_set_row_classes | Prepare array of values that will be applied as classes to a row |
views_column_class_views_api | Implements hook_views_api(). |