2
votes

I am new to drupal and I am trying to figure out how to theme Views. I currently have a content type called Category with the following fields:Title, Image and Body. I created a view for the above mentioned content type so that I would list view of all the categories I have created. To custom theme the view I created a folder called views in my theme folder, and created the following view files:

  1. views-view-fields--plugin-categories.tpl.php
  2. views-view--plugin-categories.tpl.php
  3. views-view-unformatted--plugin-categories.tpl.php

This is what I currently have in my first file:

<div class="<?php print $classes; ?>">


  <?php if ($rows): ?>
    <div class="view-content">
      <?php print $rows; ?>
    </div>
  <?php elseif ($empty): ?>
    <div class="view-empty">
      <?php print $empty; ?>
    </div>
  <?php endif; ?>


  <?php if ($more): ?>
    <?php print $more; ?>
  <?php endif; ?>


</div><?php /* class view */ ?>

Instead of $rows, I tired to use print $field['image'] and print $field['body'] but this method does seem to work. Could you kindly advise on how I could theme the three fields, within categories, displayed using view?

1

1 Answers

2
votes

You should name your template like this

views-view-fields--<machine-name-of-your-view>.tpl.php

So I'm assuming from the above that your view is called 'plugin-categories'. An easy way to check is to go to edit the view and look at the URL while you're on the edit page. It should have the format /admin/structure/views/view/YOUR-VIEW'S-MACHINE-NAME/edit, so you can get it from there.

Once you're sure it has the right name, clear your cache to make sure Drupal is picking up your new template. You just need the one above, not all three to modify the output of the three fields in question.

Once you've cleared cache, Drupal should be picking up the new template. You didn't mention exactly what isn't working, just that it's not working, so I wanted to cover the naming and caching, just in case. Now, to output particular fields in this view template, call them like this:

$fields['your-field-machine-name']

So $fields['body'] (I think you're missing an 's')

You should have nothing about $rows in this template! If you have anything about $rows, you haven't copied and pasted from the correct views template. Simply output the fields as you want them to appear in your view, in whatever order you want with the syntax above and put in whatever css classes, etc you want.

Let us know if that works!