By l2u on
I've been having headaches figuring out how to get node object when theming views.
How is it possible to get node object (like node id and other information) inside views .tpl.php files?
I want to have access and control over this data when theming drupal views.
Is it maybe still possible to override phptemplate_views_view_list_..() functions inside template.php instead of using template files?
I tried doing that with my latest drupal (6.x) and views version but it didnt work..
Many thanks for help!
Comments
...
The node objects aren't loaded for you because of efficiency: Views loads only the fields you asked for.
The node ID is there in the record fetched from the databse. The record is $row, so the node ID is $row->nid, and you can load this node yourself.
You can, but in the best case it gives you nothing. In most cases you'll lose: you lose all the juicy variables the 'preprocess' functions prepare for you (since 'preprocess' functions are executed for template-files, not for functions).
Thank you very much with
Thank you very much with helping me out with this information.
How can I get/make link/path to $node with the code above? Should I use l() ?
And one more thing: how should I define globals so that every item could be enumerated? I think I should define global $count in views-view-list.tpl.php so I would be able to print it (current $count) in views-view-fields.tpl.php .
Many thanks for help!
...
You can use l(), as in
print l($node->title, "node/$node->nid");You should paste your code here so we can fix it.But why would you want to do that? There's a 'Node: Link' field that does links for you. You don't need to program.
You can define a global, as in
global $MYVIEW_counter; ... print ++$MYVIEW_counter;.But why would you want to do that? If you pick the 'List' style, then you can configure it to show the list numbered.
You can define the global right in views-view-fields.tpl.php. But note that you should name it views-view-fields--YOURVIEWNAME.tpl.php or else your template will affect all views. Remember to clear Drupal's cache after you create this file or Drupal won't pick it up (but you don't need to clear the cache if you just modify this file).
I want to wrap all fields in
I want to wrap all fields in one a (link) tag because of css design.
I put this at the beginning of fields.tpl.php:
and by the end:
It seems to work okay, but I'm unsure if thats a proper/good way of doing it..
Thanks for help!
...
It's best, and simpler, to use the url() function, which deals with various scenarios for us (clean urls or not, language prefixes, rewrites, etc.):
(Btw, I don't see that you're using the $node object. It's better not to load it if you don't need it.)
(Note that according to the HTML standard, {A} tag may only contain inline elements. So you can't have DIV inside it, because it isn't an 'inline' element. If you care about standards, and if you do have DIVs there, you can move that enveloping {A} into 'views-view-field--xyz.tpl.php' --this will make every field separately enveloped by the {A}.)
Mooffie, I'm having the same
Mooffie,
I'm having the same issue that l2u initially described. I can't figure out of to get a node id from the $row. I found your example above quite helpful and encouraging, but for some reason, $row->nid returns nothing for me. Any thoughts on why that might be?
My code is below. It's supposed to detect the first and last nodes in the "frontpage" view and set a "first" or "last" attribute so that node.tpl.php can add an extra class to the node, if appropriate, so that it can be styled differently. Of course, it doesn't work because of the issue I described.
Many thanks in advance for any insight you can provide!
...
Views uses two different components to theme your data:
- A 'row style', that themes an individual piece of data (a row).
- A 'style', that themes the whole pieces together.
l2u's was dealing with a 'row style', and in his case a $row corresponds to a database row.
But in your case you're overriding the theme of a 'style'; and a 'style', unlike a 'row style', doesn't care much about the particulars of the individual pieces. In your case, the $row variable holds the HTML of each piece. The nodes are already rendered at this stage.
There are different ways to do this.
If you don't mind having a DIV with that extra class wrap your nodes, instead of putting that class directly on the node, then you can change your existing code: e.g., output a "{div class='node-{$view->name}-first'}" if this is the first item, etc.
But if you want the class directly on your nodes, you can use a "preprocess" function to touch the $node object before it arrives to 'node.tpl.php'. One nice thing Views does for us is to attach a "view" property to the node, and we can inspect it in our preprocess function. So, for example, `if ($vars['node']->view->result[0]->nid == $vars['node']->nid)` we know it's the first node. (I won't write the complete code because then I'll have to test it.)
Mooffie, I tend to obsess
Mooffie,
I tend to obsess about having nice clean markup, so your second suggestion seemed more desirable to me than the first.
And I can't thank you enough for your suggestion! It worked perfectly and you've ended quite a bit of frustration for me. Here's what I ended up with:
Thanks again! I really appreciate your assistance.
...
You're welcome. Improvements to your code: