3

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.

Augusto
  • 2,125
  • 18
  • 27
mechmsk
  • 129
  • 2
  • 3
  • 8

4 Answers4

4

The variables available are written in the documentation inside the sites/all/modules/views/theme folder's files.

Usually, the variable you need to look at and modify on a views-view-fields.tpl.php template is $fields

I use the devel module (http://drupal.org/project/devel) to view the variables available:

<?php
//after enabling the devel module...
dpm($fields);

// This will print a Kuomo display on the page with the array's vars

?>

Generally, on a view of nodes,

<?php print $fields['title']->content; ?> 

will print the node title. For fields, try

<?php print $fields['field_FIELDNAME']->content; ?>

If you have the memory, you can capture ALL vars available on the template in the Kuomo with

<?php dpm(get_defined_vars()); ?>

Make sure you clear your cache before you try to view the vars.

Joe Hyde
  • 999
  • 7
  • 17
3

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.

lgomezma
  • 1,597
  • 2
  • 15
  • 30
  • Thanks, this works. But I still have a concern about performance. print_r($row) outputs all fields of given content type, even if these fields were not selected in a view. And their structure is huge. Can I disable most of them? – mechmsk Mar 30 '12 at 15:35
  • Interesting question. Not sure about that...I will try to find out. – lgomezma Apr 02 '12 at 08:49
  • Note that you can get the possible template file names from the View's edit page, under Advanced > Theme: Information. – jackocnr Nov 27 '12 at 17:55
0

If you want to change of theme of view then Change views-view-fields.tpl.php like this:

<div class="pagecontent">
    <div class="colleft">
        <?php if($fields['field_file']->content){  ?><div class="views-field-file"><?php print $fields['field_file']->content; ?></div><?php } ?>
    </div>
    <div class="colright">
        <div class="views-field-title"><?php print $fields['title']->content; ?></div>
        <div class="views-field-body"><?php print $fields['body']->content; ?></div>
        <div class="views-field-view-node"><?php print $fields['view_node']->content; ?></div>
    </div>
</div>
Chirag Shah
  • 1,463
  • 2
  • 18
  • 40
0

To sort through your information in a theme use this:

<?php dpm ($rows); ?> // View all the information in the view

<?php foreach ($rows as $row_count => $row): ?>
 <?php print $row['title'];
 <?php print $row['nid'];
<?php endforeach; ?>
chadpeppers
  • 2,057
  • 1
  • 20
  • 27