1

Inside views-view-list.tpl.php there is a loop like this:

<?php foreach ($rows as $id => $row): ?>
  <li class="<?php print $classes_array[$id]; ?>"><?php print $row; ?></li>
<?php endforeach; ?>

I want to be able to access the actual values of the fields inside each item, rather than passing off the rendering of the html to views-view-fields.tpl.php.

In pseudocode, I would like to do this (THIS CODE DOES NOT WORK):

<?php foreach ($rows as $id => $row): ?>
  <?php print $row->$fields['field_my_field_name']; ?>
<?php endforeach; ?>

<?php foreach ($rows as $id => $row): ?>
  <?php print $row->$fields['field_my_second_field_name']; ?>
<?php endforeach; ?>

Would I actually use the $row object, or would I use something else?

4 Answers 4

2

views-view-list.tpl.php file it's a view template to display a list of rows, not fields.

Theming only fields:

<?php foreach ($fields as $id => $field): ?>
    <li><?php print $field->content; ?></li>
<?php endforeach; ?>

* file: views-view-fields--[view name]--[display].tpl.php

Theming the whole output:

<div class="<?php print $classes; ?>">
    <?php foreach ($view->style_plugin->rendered_fields as $delta => $item): ?>
        <li><?php print $item['field_my_field_name']; ?></li>
    <?php endforeach; ?>
</div>

* file: views-view--[view name]--[display].tpl.php

3

took me a while to work this out so sharing the solution i used.

<?php foreach ($rows as $id => $row): ?>
    <?php print $view->render_field('field_my_field_name', $id);?>
<?php endforeach; ?>

<?php foreach ($rows as $id => $row): ?>
    <?php print $view->render_field('field_my_second_field_name', $id);?>
<?php endforeach; ?>
1
  • Why is this not working! I am wondering how it got three votes Dec 28, 2016 at 7:36
1

There is a $view variable inside most Views templates that contains the entire View Object. Inside of it, you can access the raw results via $view->result which is basically an array of the rows. Throwing in a drupal_set_message('<pre>' . print_r($view->result, TRUE) . '</pre>') or equivalent will give you an idea of what you have to work with.

0

The simplest way is : $fields["field_name"]->content; Where field_name -> cck field name.

Hope that will help !!

Cheers ! Raj

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.