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