By dnguyen on
So I'm overriding a table theme for a particular view in template.php...Basically, this view calls in the images belonging to 8 nodes. I wrote a table theme function in template.php to print it in a 2x4 format, like so:
1. 2.
3. 4.
5. 6.
7. 8.
What I'm getting is this:
1.
2.
3.
4.
5.
6.
7.
8.
Here's my code..with each node, it increments $ccount, and at the beginning of the for loop, if $ccount is even, it prints a new
Here's the code:
function mytheme_views_view_table_myview($view, $nodes, $type) {
$output ="";
$ccount = 0;
$fields = _views_get_fields();
$output .="<table><tbody>\n";
foreach ($nodes as $node) {
/*********TEST CONDITION ****/
$odd = $ccount&1;
if($odd == 0 ){
$output .="<tr>\n";
}/*********TEST CONDITION ****/
$output.="<td>";
foreach ($view->field as $field) {
$cell['data'] = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
$cell['class']='view-field-'.$field['queryname'];
$output .= $cell['data'];
}
$output .="</td>";
/*********TEST CONDITION ****/
if($odd == 0){
$output .= "</tr>\n\n";
}
$ccount++;
/*********TEST CONDITION ****/
}
$output .="</tbody></table>";
return $output;
}
$ccount is correctly incrementing and the test condition is correct...but it seems like Drupal is totally ignoring the test condition and writing
Comments
Some thing to try
Try changing
to
The question is how many different nid's do you see.
As written you have two loops, the outer covering the nodes, the inner the fields (images) which as written can be more than one. I would think your count and odd logic should be inside the inner loop.
I changed the code as you
I changed the code as you suggested and it behaves as expected: it prints out 8 node ids. I'm pretty sure my loop logic is fine, as there is only one field in this view.
I just don't get the behavior of the theming function...if I understand correctly, it will only return the output as I want it to, right? For example, if I set $output equal to "TEST"...I will only get "TEST" (wrapped in some view div tags). But the table tags behave differently...I could not include any
A link would help
The code looks ok and looks like it should do what you want. I would suggest looking at the generate html, which tags do you see? (Drupal should not be filtering them at this point).
heh...yeah I know a link
heh...yeah I know a link would help, but it's running locally right now. Here's the code that it generates (I've left it so that it just shows the node-id:
After each
tag, two newlines is generated. So you can see that there are
I think I figured out
Currently $ccount and $odd are the same value before and after printing a table cell.
Change
to
Then change the second test condition so only
$ccount++;remains (no 'if' and no printing of closing table cellThen if you are sure there are always an even number of cells, change
An alternatve approach would be
I just woke up, and your
I just woke up, and your correction to the code didn't make sense to me, so I just pasted in your alternative approach and it works perfectly...Thanks! I'm going to have some coffee before I figure out what I did wrong.
edit: ok, I get where I was incrementing incorrectly. Good to know I'm just an idiot rather than something difficult with Drupal.