I have figured out I need to override two templates:

  1. views-view-fields.tpl.php
  2. views-view-unformatted.tpl.php

Basically my node has an "align" property which I need to access in both templates to properly generate the expected markup.

<?php echo '<pre>'; print_r(get_defined_vars()); exit; ?>

Using this I can see I have a monster of a structure at my disposal but the I am nervous about traversing such a structure as the slightest change to it's arrangment means my template(s) would break!!!

Ideally I would like to implement a preprocessor such as:

How can I add variables from a preprocessor function to be used in an unformatted view template?

Basically add the "align" value from the node to a simple variable which I can then access and check inside both templates listed above...


1 Answer 1


You can use hook_preprocess(&$variables, $hook)to find out which hook you have to react on, in your case it should be hook_preprocess_views_view_fields() and hook_preprocess_views_view_unformatted().

Next you have to find a way to access the node object. If your view is node based, then there should be an nid somewhere in the variables array. Don't be afraid to dif into the $variables array. You can use dpm (devel module) or print_ror whatever you're comfortable with. You can't really break anything without manipulating the array.

  • but if a module changes the structure of the array: $row->_field_data['nid']['entity']->field_align['und'][0]['value'] is what I currently have it is so deeply nested that the slightest change would break...no fear you say, eh? :) Commented Jan 30, 2014 at 16:18
  • You should of course prepare for the case that the expected data is not there, using isset()will do. Where does $row->_field_data come from? That's in $variables?
    – berliner
    Commented Jan 30, 2014 at 16:23
  • not variables no sorry (views-view-fields.tpl.php) Commented Jan 30, 2014 at 16:37

Your Answer

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

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