views_preprocess_node() isn't always available when theme registry builds

Drupalmannen - July 8, 2008 - 15:11
Project:Views
Version:6.x-2.0-rc1
Component:Code
Category:support request
Priority:normal
Assigned:Unassigned
Status:active (needs more info)
Description

I've created a view which simply retrieves all nodes(atleast that is what I want) of a specific content type.
The view is a block type so I can move it where I want it on the Page using the Block-module.

All I want after that is to get these nodes to work in my own custom tpl.php file.

If you don't know how that works look in Views->Add/Edit exisitng then there is a block/div called Basic Settings.
At the end/bottom of that list you shall see 'Theme: Information'. Click on that and you understand how I created my custom tpl.php.

The problem is I cannot get anything out from this file. I want some nodes so I can do like $node->title etc.
But all I get is the $view which I don't know how I should get the nodes from. I've googled and found a function called
views_build_view but it doesn't exist in Drupal 6. Otherwise I think that's exactly what I needed.

Hopefully there is something basic I've missed cause what I want to do isn't any complicated. Just get the nodes from the view.

The thing is, if I add some fields to the view, like the titles, those will only be displayed with the default template view, but when the view uses my
own tpl.php file nothing appears, and yes, I pasted the same code that was in the default file... still nothing there. Ain't that weird?

What have I missed?

Just to remind you; I don't want the view to build the view and display it's contents, that's what's my custom tpl.php file is for.
I just want the view to retrieve what I want, a query PLUS it provides a block (which I can move around wtih the Block-module functionality).
That's why it needs to be a view, cause it provides me a block aswell.

I'm totaly stuck and about to give up. All I want is a list of nodes to work with.

#1

merlinofchaos - July 8, 2008 - 16:15
Version:6.x-1.0» 6.x-2.0-rc1
Status:active» active (needs more info)

Did you follow the instructions in the release notes and update your site to Drupal 6.x-dev? This is the bug that exists in Drupal 6.2.

#2

Drupalmannen - July 9, 2008 - 09:30

Hmm... ok. Hope that's it.

I have changed from 6.2 stable to the latest development release.

I have a view which is a block and is of node type isntead of field type cause I don't want any fields to be displayed automatically).

I have my custom tpl.php file.

I write "hello" and it displays "hello hello" as there are 2 nodes of this type.
But how do I get the node data?

echo $title doesn't work, then I get:
notice: Undefined variable: $title in (path to file)

echo $node->title doesn't work, then I get:
notice: Trying to get property of non-object in (path to file)

echo $node WORKS though which prints all contents of the node.
(well, that was just beacuse node was a string, because i had choosen row style: node)

Now I use row style: field but it still doesn't work.

Only variable I seem to be able to start from is $view.
But I know the function views_build_view($view, 'items',...) doesn't exist in Drupal 6.

What should I write to get specific node data?

Would be nice if it was just something like $view->node->title;

#3

mooffie - July 9, 2008 - 10:15

i had choosen row style: node

Return to using this style. Then copy 'node.tpl.php' into a new file, 'node-view-VIEWNAME.tpl.php', in your theme folder. Replace 'VIEWNAME' with the "machine readable" name of your view. This 'node-view-VIEWNAME.tpl.php' will now be used to show the nodes in that specific view. Edit that template to your liking.

If things doesn't seem to work, type "blah blah" into that template: you should see it in your view output. That little test is actually the first thing you should do anyway.

#4

Drupalmannen - July 9, 2008 - 10:45

Thank you very much! :)
It works now.

I must say the Theme: Information part in Basic settings in views is missleading, cause I thought those names were the only names you could have.

This section lists all possible templates for the display plugin and for the style plugins, ordered roughly from the least specific to the most specific. The active template for each plugin -- which is the most specific template found on the system -- is highlighted in bold.

Display output: views-view.tpl.php, views-view--typeOneViewer.tpl.php, views-view--default.tpl.php, views-view--typeOneViewer--default.tpl.php
Style output: views-view-unformatted.tpl.php, views-view-unformatted--typeOneViewer.tpl.php, views-view-unformatted--default.tpl.php, views-view-unformatted--typeOneViewer--default.tpl.php
Row style output: views-view-row-node.tpl.php, views-view-row-node--typeOneViewer.tpl.php, views-view-row-node--default.tpl.php, views-view-row-node--typeOneViewer--default.tpl.php

So I was naming my template php after that pattern. Which didn't have node-... first.
The template was used and I could see "bla bla" but no node objects to access.

Problem solved
You have to name the tpl.php file to 'node-view-VIEWNAME.tpl.php' to be able to access the node object.

#5

Drupalmannen - July 9, 2008 - 10:48
Status:active (needs more info)» fixed

and Fixed :)

#6

mooffie - July 9, 2008 - 11:16

I must say the Theme: Information part in Basic settings in views is missleading,
[...]
This section lists all possible templates for the display plugin and for the style plugins,

This "Theme: Information" page does what it says. A node template isn't a "display plugin template" nor a "style plugin template".

But I see your point. The problem is that it's probably not possible to "fix" this: the template a node eventually uses is determined by runtime code and each node may end up using a different template. A story node may use 'node-story.tpl.php, a page node may use 'node-page.tpl.php', etc.

There's a comment in 'views-view-row-node.tpl.php' explaining to use the node template instead, and it doesn't neglect to mention "This isn't actually a views template". I think this is sufficient.

#7

merlinofchaos - July 9, 2008 - 15:10

If you have ideas on how to make this more clear, I'm all ears, too.

#8

STNyborg - July 14, 2008 - 10:51

Having read this thread I tend to agree with Drupalmannen - I too have difficulty finding proper documentation on how to theme my 6.x views using "node-view-[viewname].tpl.php".

If I have understood the thread correct - the only thing I need is create a file called "node-view-[viewname].tpl.php" with the output code for the rows of data for my listed view and add it to my theme directory. Am I correct so far?

My problem is that I cannot get my view to change according to the themed node-pages.

What am I doing wrong?

Regards,

Svend

#9

STNyborg - July 14, 2008 - 10:51
Status:fixed» active (needs more info)

... need more info :)

#10

lx-88 - July 19, 2008 - 05:32

I think I'm having the same issue as STNyborg above. I created a view that displays events. The view is called "pmills_event" with Defaults, "events overview," and "event detail." "events overview" and "event detail" are both Page types that I added using the Add Display button.

Everything functions as I would expect and I'm happy with the query but I can't seem to style it. I read above and got excited but it didn't behave as I expected.

I created a file in the root template directory (along with page.tpl.php) called node-view-pmills_event.tpl.php, not recognized.

I want control over how each field's style. Basically I want to add

around one field and

around another. What should I name my tpl file and where should I put it. I reset cache and did everything I know how to do.

I agree that the template information box could be a bit clearer for views beginners, but cant offer any suggestions until I understand how the templating system actually works. Thank you in advance for any help you can offer.

#11

cryozot - July 20, 2008 - 05:30

I had the same issue with Views 6.x-2.0-rc1 as STNyborg and lx-88 both describe. Having made some investigations I found that node-view-VIEWNAME template suggestion is added by views_preprocess_node() function in /modules/node.views.inc file. It seems that sometimes views_preprocess_node() sometimes gets "lost" and does not appear in 'node' theming hook as preprocessing function (according to Theme Registry screen in Devel module).

Now it works on my site, and I don't know what I've did to fix it :(

Do you, STNyborg and lx-88, see views_preprocess_node function in Theme Registry?

#12

merlinofchaos - July 20, 2008 - 06:26
Title:Basic template customizing of a view in Drupal 6» views_preprocess_node() isn't always available when theme registry builds
Category:support request» bug report
Status:active (needs more info)» active

It seems that sometimes views_preprocess_node() sometimes gets "lost" and does not appear in 'node' theming hook as preprocessing function

Hm. Crap. That could be a problem.

#13

heather - July 20, 2008 - 23:51

been struggling with this for hours... i am suffering from similar symptoms...

@cryozot you asked "Do you, STNyborg and lx-88, see views_preprocess_node function in Theme Registry?" ... how do i check my theme registry to see if the function is there?

edit: you install the devel module... and got to admin/build/blocks and enable the "devel" block.
then you can select "theme registry" which will take you to http://yoursite.com/devel/theme/registry - and you can see a list there.

#14

claudiur - July 22, 2008 - 06:14

a quick hack and dirty hack...

I moved views_preprocess_node()'s contents to a preprocess_node function in my template.php and seems to be working well all the time. Hope someone finds a real fix for this

#15

lentreprenaute - July 22, 2008 - 15:41

Hi guys,

I met the same problem, i can't use " tpl " to display views with Drupal 6.3.

#16

lx-88 - July 25, 2008 - 06:25

I got tpl files recognized!

go to sites/all/modules/views/theme and copy the unformatted ones to your root theme folder (the one where page.tpl.php is for your theme). Next append --VIEWNAME on the end with .tpl.php at the end of that. (Examples Below) Clear the cache on admin/settings/performance and clear the Views cache under the "Tools" tab of your view's config panel.

The Rescan button under theme information did not change anything for me, nor did it bold the filenames of my active templates. The Theme Developer (devel) module isn't playing nice with my CSS and has some odd behavior with other parts of my site. I'm still trying to figure that out but it isn't a priority for me right now. I would suggest turning it off if your having issues and turn it on when you need it. It just takes one variable out.

My Views Name is pmills_event. My filenames are below and placed in sites/all/themes/MYTHEMENAME
views-view-fields--pmills-event.tpl.php
views-view-unformatted--pmills-event.tpl.php

Hope that helps someone.

#17

merlinofchaos - July 25, 2008 - 16:45

The Rescan button under theme information did not change anything for me, nor did it bold the filenames of my active templates.

This is a concern. Can someone else check this behavior as well?

#18

dreamlabs - August 10, 2008 - 11:56

Very big concern, have the same problem. My views templates keep getting ignored, And after emptying the cache, the node-view template is gone and overruled by the regular node template

#19

mooffie - August 10, 2008 - 15:24

The Rescan button under theme information did not change anything for me

This is a concern. Can someone [...]

Very big concern, have the same problem. [...]

People are mixing here two totaly different issues:

  1. 'node-view-VIEWNAME.tpl.php' not being recognized. (It shouldn't, because views_preprocess_node(), which is in 'node.views.inc', isn't loaded when the registry is built. One solution is to move it to 'views.module'. (Incidentally, we won't have this problem in Drupal 7, as it has code registry.))
  2. Any other template not being recognized. This may well be a false report.

#20

STNyborg - August 12, 2008 - 09:51

@mooffie - you write that 'node-view-VIEWNAME.tpl.php' should not be recognized. Are you implying that it is NOT possible to create view templates in 6.x?

I have tried to move the views_preprocess_node() to views.module, but without luck. I would really like to be able to theme my views in 6.x and thus look forward to any form of feasible possibilities.

#21

mooffie - August 12, 2008 - 13:25

I have tried to move the views_preprocess_node() to views.module, but without luck.

After you relocate this function you must clear Drupal's cache (or only the registry cache), or else Drupal won't notice this function. (I didn't mention this detail because my comment was directed mainly to Merlin.)

Did you clear the cache?

#22

mooffie - August 12, 2008 - 13:59
Status:active» patch (code needs review)

Here's patch that moves that preprocess function to 'views.module'. It'll make it possible to use 'node-view-VIEWNAME.tpl.php'.

I added some comments.

(Note: users who clear the theme registry by clicking Views' "Rescan template files" button don't see this bug because in this case 'node.views.inc' __is__ loaded. However, sooner or later the registry will be rebuilt and these users too will fall victim to the bug.)

AttachmentSize
node_tmplt_279899.diff2.44 KB

#23

nonsie - August 12, 2008 - 23:52

Patch in #22 works.
I've added the option to use node-view-viewname-displayname.tpl.php style templates in addition to node-view-viewname.tpl.php templates.
Note that attached patch contains code from #22

AttachmentSize
views.module-node-view-viewname-displayname.patch1.25 KB

#24

STNyborg - August 13, 2008 - 07:32

Thank you mooffie for your patch. I thought I had cleared the cache, but I will give your patch a try. One small problem however - I do NOT have access to the root command on my site (hostet externaly). I guess I have to alter the two files by hand, but am not sure how to "read" the patch - what to add, remove and replace. Would you be able to attach the updated two files?

As to the last patch #23 - I am not sure what to use this feature for. What is the advantage adding displayname to the template?

#25

STNyborg - August 13, 2008 - 07:37

Just out of curiosity - knowing this comment IS out of place - would it be possible to create a functional drupal module that could handle patches directly online? This way people like myself who do not have access to the root command will still be able to apply patches to their systems.

#26

merlinofchaos - August 13, 2008 - 17:09
Status:patch (code needs review)» fixed

It would not be possible to have a Drupal-side patch applier; it is a security risk to allow Drupal to modify its own code. It means that a very small breakin could result in a very major, long-term problem. The Drupal security team's attitude is that the code needs to be inviolate by Drupal.

Patch committed to CVS.

#27

STNyborg - August 20, 2008 - 12:18
Status:fixed» active (needs more info)

I have altered the two files as stated in the patch file; cleared the cache; but still cannot get my node-view-[viewname].tpl.php to work. Should I upgrade to the available dev version?

Svend

#28

mooffie - August 20, 2008 - 13:55

Svend,

In the "Basic settings" box there's "Row style" (just under "Style"). Is it set to "Node"?

#29

STNyborg - August 21, 2008 - 08:22

Mooffie,

I cannot find row style anywhere. We are talking about editing the specific view, right? I cannot find row settings anywhere. I have attached two screen dumps of the current settings.

Svend

AttachmentSize
screendump02.png23.88 KB
screendump01.png22.09 KB

#30

mooffie - August 21, 2008 - 13:34
Status:active (needs more info)» fixed

Svend,

There are various ways to show data. You're showing it as a table. You have "Style: Table" there.

A different way to show nodes is in the way they are shown on your front page: as a list of nodes. To switch to this mode, change "Style: Table" to "Style: Unformatted". Then you'll have a new "Row style" option. Makes sure it shows "Row style: Node".

Only in this mode there's a meaning to "node-view-[viewname].tpl.php". This specific template won't be used otherewise.

But why are you intereseted in this specific template? Perhaps it's not the template you should be looking for. There are quite a few templates involved in theming a view. We don't know what you're trying to achieve, so we can't help much.

#31

STNyborg - August 28, 2008 - 08:09
Status:fixed» active (needs more info)

Mooffie,

I found the row style setting and also read the info about how to call the node template. However, does still not work.

The reason why I would like to use node templates is that I would like to CSS style the node output in various ways, e.g. adding specific CCK fields to the view list.

#32

mooffie - August 28, 2008 - 12:46
Category:bug report» support request

First, upgrade to the 'dev' version, and clear the cache.

- What's the name of the view?
- What's the name of your template file?
- What's the "Row Style"?
- Into your template file type "blah blah blah". Do you see this text in the view output?

"I would like to CSS style the node output in various ways, e.g. adding specific CCK fields to the view list."

"to CSS style the node" is very different than "adding CCK fields".

If CSS tweaking is all you want, you don't really have to change the template. Views wraps all the output with <div class="... view-VIEWNAME ...">...</div>

So, for example, in you style sheet you can add...

.view-VIEWNAME .field {
  color: red;
}

...to paint all CCK fields in red. No template editing is needed.

 
 

Drupal is a registered trademark of Dries Buytaert.