Closed (fixed)
Project:
Views (for Drupal 7)
Version:
6.x-2.x-dev
Component:
Code
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Reporter:
Created:
31 Jul 2009 at 21:53 UTC
Updated:
6 Oct 2009 at 07:20 UTC
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.
| Comment | File | Size | Author |
|---|---|---|---|
| views-HEAD-table_preprocess_row_store.patch | 796 bytes | dboulet |
Comments
Comment #1
dboulet commentedComment #2
merlinofchaos commentedAre you sure this is needed? The rows are always available in $view->result
Comment #3
dboulet commentedThis 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?
Comment #4
dboulet commentedAre 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.
Comment #5
Oleksa-1 commentedI 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 :
let say field_myfield is $fields['created']->content , how I should put it in this code ?
Comment #6
dboulet commented@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:
Comment #7
Oleksa-1 commentedThx,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:
Comment #8
merlinofchaos commentedOk, it's an easy enough patch, so committed.
Comment #9
dawehnerWhy is it not commited to DRUPAL-6--2?
Happy birthday earl!