Search code examples
performancedrupalviewdrupal-7drupal-theming

How to theme views fields in Drupal 7 correctly


I need to theme views in Drupal 7. There is a content type 'Book' and I need to list 5 books and theme them in special manner(preview image, title and author).

When I override views-view-field.tpl.php and print raw SQL result, I see that all fields are displayed. This code

echo "<pre>";
print_r($row);
echo "</pre>";

gives

[entity] => stdClass Object
 (
  [title] => ...
  ....
  [nid] => 34
  ...
  [body] => Array
  ...

But I don't want pass [body] from database to php side, because it can be huge and cause a performance issue. I haven't selected [body] in view settings.

Is there a way to pass only certain fields to views-view-field.tpl.php?

Thanks in advance.


Solution

  • If what you want to do is theme a certain field you can create a template for that specific field like this one: views-view-field--field-nameofmyfield.tpl.php place it in your theme folder and rescan the templates in the Theme:information part of the View configuration.

    For that to work you have to have the field added to Fields in the View.