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

tag (and closes with a
tag)...For some reason, Drupal insists on creating a new row with each node.

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

tags whenever it feels like it. Am I missing something in the theming system?

Comments

nevets’s picture

Try changing

$output .=  $cell['data']; 

to

$output .=  $node->nid; 

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.

dnguyen’s picture

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

tags in the output, period, and yet at least one set of
tags will be generated around the tags...Is this typical html behavior, or does drupal do some kind of checking to make sure your table tags are in order before printing them?
nevets’s picture

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).

dnguyen’s picture

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, as designated in my code, a newline is generated. After each

tag, two newlines is generated. So you can see that there are

tags in which no newlines are generated, so those tr tags are not being generated by the code.
<div class="front-page-block">
<div id="front-block-title"><h2>New artwork</h2></div>
<div class="content"><div class="view view-frontpage-new-artwork"><div class="view-content view-content-frontpage-new-artwork"><table><tbody>
<tr>
<td>25</td></tr>

<tr><td>24</td></tr><tr>
<td>22</td></tr>

<tr><td>21</td></tr><tr>
<td>20</td></tr>

<tr><td>19</td></tr><tr>
<td>18</td></tr>

<tr><td>17</td></tr></tbody></table></div></div>
</div>
</div>

nevets’s picture

Currently $ccount and $odd are the same value before and after printing a table cell.

Change

/*********TEST CONDITION ****/
$odd = $ccount&1;
if($odd == 0 ){
$output .="<tr>\n";

}/*********TEST CONDITION ****/

to

/*********TEST CONDITION ****/
$odd = $ccount&1;
if($odd == 0 ){
  if ( $ccount ) {
    // if not the first time through the loop need to close the table row
    $output .= "</tr>\n\n";
  }
$output .="<tr>\n";

}/*********TEST CONDITION ****/

Then change the second test condition so only $ccount++; remains (no 'if' and no printing of closing table cell

Then if you are sure there are always an even number of cells, change

$output .="</tbody></table>";
<code>
to
<code>
$output .="</tr></tbody></table>";

An alternatve approach would be

function mytheme_views_view_table_myview($view, $nodes, $type) {
	$cells = array();
	$fields = _views_get_fields();   

	foreach ($nodes as $node) {
		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'];
			$cells[] = $cell;
		}
	}
	// Make sure we have an even number of cells
	$count = count($cells);
	if ( $count % 2 == 1 ) {
		// Odd number of cells, add one to make the number even
		$cells[] = array('data' => '');
	}
	
	// Now place in table
	$rows = array();
	$row = array();
	foreach ( $cells as $index => $cell ) {
		$row[] = $cell;
		if ( $index % 2 == 1 ) {
			$rows[] = $row;
			$row = array();
		}
	}
	
  return theme('table', array(), $rows);
}
dnguyen’s picture

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.