I have a node--type and view--type templates. In view--type I cannot access $node, for example with $node->nid.

How can I inject $node inside view type templates?

I have a views-view-field--mypage--page--body.tpl.php file; this is a field template of the body of teasers where I inserted a Read more link. I need the URL for that link.

  • 2
    If the node->object isn't loaded in a specific template, it probably shouldn't be there. I think what you really should consider is if something is wrong with your approach to the problem. – Letharion Jan 8 '12 at 17:22

Views doesn't load the complete $node object for performance reason. Instead, it build, or attempt to build, an optimized query to only load the fields configured in the Views.

When editing the views, you should have a Fields block. In this block, use its Add fields links to add the additional fields you want to use in your template. If you don't want to display these fields using Views itself, be sure to configure them to be excluded from display.

|improve this answer|||||
  • I am pretty close, but I have already added a field nid which is available within teasers. The only problem I am in a field template. I found however the nid in view results: 'node/'.$view->result[$id-1]->_field_data['node_taxonomy_term_data_nid']['entity']->nid;..but is terrible ugly – drupal_stuff_alter Nov 9 '11 at 14:41
  • Ugly doesn't really matter. If that's where it is it is fine to get it from there. – rooby Oct 13 '13 at 12:34

You can add the properties you need in the view and exclude them from display. This will make them available in templates, but won't effect the markup.

Alternatively you can load the node with node_load, but to do this, you need to nid (node id) from somewhere. One way would be to use the above technique.

|improve this answer|||||
  • Hello @googletorp, thank you for taking time to read my problem. How can I add properties from views panel? – drupal_stuff_alter Nov 9 '11 at 12:40
  • Using node_load() in a views template is a discouraged practice. At least, do it in a (pre)process function, but not in the template itself. But loading each node individually, or even using Drupal 7 node_load_multiple(), could lead to performance issues as more data is loaded from the database and more code executed (ie. every node load hooks implementations). – Pierre Buyle Nov 9 '11 at 14:05
  • @Pierre Buyle I didn't say that he should load the node in a template. If you are using views it will be quite complex to load all nodes, and since node_load uses static cache, it might not generate a query anyways. – googletorp Nov 9 '11 at 15:12
  • @Pierre Buyle, I would be just curious to learn why we should avoid node_load in a template... Just curious to know the proper ways of doing Drupal stuff. – Artur Feb 7 '12 at 17:02
  • 1
    @Arthur, following the good practice of "Separation of Concern" (goo.gl/qfwfk) a template (the .tpl.php file) should be about presentation not data retrieval or processing. To properly separate things, the data retrieval and processing should happens before the presentation layer is used, in Drupal that would be before calling theme() (either directly or indirectly by returning data to be presented by the calling code (for instance, before returning something in a page callback). When not possible, the (pre)process of the template can be used to keep things separated. – Pierre Buyle Feb 7 '12 at 18:10

What kind of information do you need from the $node object that you can't get from views standard options?

If you enable views_customfield you can add a PHP code field into the Views UI. Just add the node NID as a normal field and then add a custom PHP field, then use:

print var_export($data, TRUE);

in this new PHP code field to show you what items you can call from the views result.

One of them will be the nid so you could use node_load($data->nid);

Remember you need to add the PHP opening and closing tags around your code in the Views customfield.

You could do the same in a template or preprocess function too and if you have some complex logic that is what I would recommend.

|improve this answer|||||
  • I actually need to place a $node->nid in the Read more url. And read more is in a views-view-field template – drupal_stuff_alter Nov 9 '11 at 13:54
  • Just use the field "Node: Link, Provide a simple link to the node.", as this already links to the node and then set the text to Read more. – soulston Nov 9 '11 at 14:07
  • I am actualy in the body field in the teaser view. So I am already in a field. I can make nid available but a leverl above – drupal_stuff_alter Nov 9 '11 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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