Drupal view rows output

drupaldrupal-views

I'm trying to create a page by using view 2. This page list all the nodes and grouping by their taxonomy term. In this view the Style as Unformatted and Row style as Fields. The fields as following:

Node: Title 
Node: Teaser 
Taxonomy: Term 

The problem is that I want the first row under each term display both Title & Teaser and the rest display only the Title. Example:

-News

  1. Title & Teaser
  2. Title
  3. Title

-Sport

  1. Title & Teaser
  2. Title
  3. Title

-Entertainment

  1. Title & Teaser
  2. Title
  3. Title

I tried to theme by using

  • views-view-unformatted.tpl.php
  • views-view-fields.tpl.php
  • views-view-field.tpl.php

above three files with no luck. I have struggled with this issue for a while now, any help will be appreciated. Thank you.

Best Answer

You'll need to use the 'Row style output' template (e.g. views-view-fields.tpl.php).

To make it unique to your view, you'll want to use a more specific template name than that, such as the last possible template name showed in that section - e.g. views-view-fields--myview--default.tpl.php.

Once there, you'll need you have a global variable to keep track of the current row, something like:

<?php 
  global $is_first_of_term;

  if(!isset($is_first_of_term)){
    $is_first_of_term = TRUE;
  } else {
    $is_first_of_term = FALSE;
  }


  // Then use $is_first_of_term below (unmodified original script)
  // to print whatever you want.
?>
<?php foreach ($fields as $id => $field): ?>
  <?php if (!empty($field->separator)): ?>
    <?php print $field->separator; ?>
  <?php endif; ?>

  <<?php print $field->inline_html;?> class="views-field-<?php print $field->class; ?>">
    <?php if ($field->label): ?>
      <label class="views-label-<?php print $field->class; ?>">
        <?php print $field->label; ?>:
      </label>
    <?php endif; ?>
      <?php
      // $field->element_type is either SPAN or DIV depending upon whether or not
      // the field is a 'block' element type or 'inline' element type.
      ?>
      <<?php print $field->element_type; ?> class="field-content"><?php print $field->content; ?></<?php print $field->element_type; ?>>
  </<?php print $field->inline_html;?>>
<?php endforeach; ?>
Related Topic