I was just developing a Block View in Drupal for a freelance project, where the markup on the first row was different to the subsequent rows. I needed to change the markup to display an image and teaser, while the subsequent rows just has the title (which links to the node) and published date.

Due to the fact that the loop is found in the “views-view-unformatted” template, while the row markup is found in “views-view-fields” it is not possible to do a simple test to see if $i = 1 for instance.

Drupal does not implement Object Oriented PHP very well, so the only way I could figure out how to do this was using a Global variable. So at the top of my “view-view-fields” template, I added the following:

              
<?php
    global $is_first;

    if(!isset($is_first)) $is_first = true; 
    else $is_first = false;
?>

            

then, in the template you can just test if $is_first, eg.

              
if ($is_first) {
    print '<p>Markup for row 1</p>';
} 
else {
    print '<p>Markup for subsequent rows</p>';
}

            

Remember this is a global variable, so be sure to name it uniquely if you’re going to use it elsewhere.

« back

Related posts

Wordpress Widget Context on localhost

I often develop sites on my local machine before committing and transferring to a staging/live server, so during the development phase most of my sites reside on a URL similar to http://localhost:8800. …

17th May 2011

read more »

Sort a view using arguments in Drupal

Being able to sort a view using URL parameters seems like the most basic functionality, but it seems this is not possible using using views without some kind of module. I have to say Symphony handles this much better than Drupal. …

10th March 2011

read more »

Get the image path of a CCK file upload field in Drupal

I often like to use template files to alter the HTML of a view to cut down on the masses of redundant markup that Drupal creates. But I just came across a problem where I was unable to get the file path of an image in my content type, despite the fact that the image was output in the view settings. …

8th March 2011

read more »