This patch modifies the template_preprocess_views_view_table() function slightly to have the raw row data stored for future use by other preprocess functions. This information can be useful for preprocess functions, for example, when using row data to add classes.

See http://drupal.org/node/438138#comment-1591658 and http://drupal.org/node/261171#comment-1822682.

Comments

dboulet’s picture

Title: Store rows » Store row data for future use in views table preprocess functions
merlinofchaos’s picture

Are you sure this is needed? The rows are always available in $view->result

dboulet’s picture

This is needed to solve a problem I encountered while trying to access the data that corresponds to each table row in a preprocess function. Matching the row key with the $view->result key proved to be unreliable. In the case where the view table uses a grouping field, groups are split into their own tables, and $vars['rows'] only contains the data for that specific group, while $view->result always contains the data for all groups.

In my specific case, I was trying to use row data to add classes to my table rows, but couldn't find a way to find the specific data for each row—the data in $view->result[$key] did not always match $vars['rows'][$key], which meant that I couldn't accurately add classes to $vars['row_classes'][$key].

If, for example, my view looks like this:

Group 1
----------
|Result0|
----------
|Result1|
----------
Group 2
----------
|Result2|
----------
|Result3|
----------

Each group is rendered as its own table. When processing the first group, $vars['rows'][0] will correspond to that data found in $view->result[0], but when processing the second group, $vars['rows'][0] will correspond to $view->result[2]. Hope that makes sense.

I thought that storing the original $vars['rows'] array would be a simple way to solve this, but maybe there is a better way?

dboulet’s picture

Are the results in $view->result always in sorted in the order they are presented, even when using a grouping field? My tests show this to be the case, but I don't know if it's necessarily always true. If it is the case, I could count the rows using a static variable, and then reference the results in $view->result using that variable as the key.

Oleksa-1’s picture

I read this post and privios one? and have no clue how to put my class (depending of value of field) to row (

)

Should I make any changes in views-view-table.tpl.php ?

Or it is enough to put this function (as example from project module) to template.php :

function chrysalis_preprocess_views_view_table($variables) {
  $view = $variables['view'];
  if ($view->plugin_name == 'table') {
    foreach ($view->result as $num => $result) {
      $variables['row_classes'][$num][] = "state-$result->field_myfield ";
      $variables['row_classes'][$num][] = "priority-$result->field_myfield ";
    }
  }
  $variables['class'] .= " table";
}

let say field_myfield is $fields['created']->content , how I should put it in this code ?

dboulet’s picture

@Oleksa It is enough to use the preprocess function in your template.php file. As in the example you posted, you can loop through $view->result and add classes to the $variables['row_classes'][$num] array, with $num being the number of the row. Be aware however that this will not work correctly if you use a grouping field, which is the issue that I was trying to solve in this thread.

I was able to work around the problem by using a global variable to count the number of rows, and use that count to match up the data in $view->result to each row. Here is some sample code:

<?php
/**
 * Preprocess views-view.tpl.php.
 */
function mytheme_preprocess_views_view(&$vars) {
  // Reset the result count
  global $result_count;
  $result_count = 0;
}

/**
 * Display a view as a table style.
 */
function mytheme_preprocess_views_view_table(&$vars) {
  global $result_count;
  if (!is_int($result_count)) {
    $result_count = 0;
  }
  $view = $vars['view'];
  $result = $view->result;
  foreach ($vars['rows'] as $index => $data) {
    // Add classes
    $classes = is_array($vars['row_classes'][$index]) ? $vars['row_classes'][$index] : array();
    // Node language class
    if (isset($result[$result_count]->node_language) && !empty($result[$result_count]->node_language)) { $classes[] = 'node-lang-'. check_plain($result[$result_count]->node_language); }
    // Node status class
    switch ($result[$result_count]->node_status) {
      case "1":
        $classes[] = 'node-published';
        break;
      case "0":
        $classes[] = 'node-unpublished';
        break;
    }
    $vars['row_classes'][$index] = $classes;
    $result_count++;
  }
}
?>
Oleksa-1’s picture

Thx,dboulet, but code doesnt work for me
warning: Invalid argument supplied for foreach() in /home/mysite/public_html/sites/all/themes/ninesixtyrobots/template.php on line 142.

on line 142 , i have
foreach ($vars['rows'] as $index => $data) {

And why it is so complicated with style:table? With other styles (unformated for example) it is enough to put something like this in views-view-fields.tpl.php
<ul class="<?php print $fields['created']->content" ?>">
And it works.

And i expected the same in views-view-table.tpl.php:

<?php foreach ($rows as $count => $row): ?>
      <tr class="<?php print implode(' ', $row_classes[$count]); ?>" class="<?php print $fields['created']->content" ?>">
merlinofchaos’s picture

Status: Needs review » Fixed

Ok, it's an easy enough patch, so committed.

dawehner’s picture

Why is it not commited to DRUPAL-6--2?

Happy birthday earl!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.