Hi everyone -
What I have is a site where the site's main sections are identified by a jpg banner at the top of each page. I want to set up a vocabulary called "section" so that I can tag certain pages with a term from the section vocabulary and have a banner display on that page.
One way of doing this would be to create a different page.tpl.php file for each taxonomy term like:
page-taxonomy-term-101.tpl.php
True, that would work - but I have 20 different banners. That would mean that if I ever wanted to make substantial changes to my page.tpl.php files, I'd have to do it in every single version.
Is there a way to do this kind of simple image switching within the main page.tpl.php file? I've looked all through the handbooks and on the forums, but everything keeps pointing me back to this idea of setting up separate page.tpl.php files. From a maintenance standpoint, it seems like an extremely messy way of doing things, especially as our website grows.
Thanks very much for your help!
Matt
Comments
Wrong thinking
Banner should probably be created as a block.
Yeah, beginners can do it in page.tpl, but eventually you figure out the right way.
So you make a block called section_banner. Place it in the page header (or a custom zone, I have had to put some above content)
enter php code as the block body. Something like:
That's pseudo-code. Experiment with your own needs.
Of course the above code COULD just go into page.tpl, but it could be in a better place.
.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/
.dan. is the New Zealand Drupal Developer working on Government Web Standards
That makes sense...
I'm totally with you on moving this operation to a block. Makes total sense. Plus, it keeps my page.tpl.php file nice and clean.
Unfortnately, I can't get it to work. The block is set up to run as part of the header. And I have it weighted below my top-most nav bar. But the PHP block isn't outputting any code.
You can see the page, which should be showing a banner that spans the length of the page, here:
www.thenestedtest.com/drupal/about
Here's what I have in the block:
As you can see, I've changed little from the code you suggested; the id of my "Sections" vocabulary is 7, so I've used that in the first line. Also, I've added the correct path for my test site banner images.
Am I just missing something really obvious that should have also been changed?
Thanks for your help; this is starting to make some sense!
Matt
A couple of thoughs
First, you have a typo, '$term->tid should be '$term->vid' (vid not tid). Also did you set the input format for the block to 'PHP code'? If that is note the problem, try prepending some text/html before the php and see if the block is printing anything at all.
A couple of minor points, if you always want a banner this code will not output anything for non-node pages and pages that do not have a term from the selected vocabulary. It also it not portable as it assumes you have the site installed in a sub-directory called drupal. The following would be one way to handle both of these.
Getting closer...
Thanks, Nevets!
I did have the input format set to render php. After fixing the typo you mentioned and implementing the other changes, I now have something printing out - but not quite the right something. Here's what it's outputting:
For some reason, that variable isn't printing the way it should. So close!
Matt
Update
So here's where I'm at.
My code currently looks like this:
The first problem was that I needed to concatenate some output strings. Now I'm at least getting the default banner image printing to the page.
That means that I've narrowed down the problem. It has to be in the foreach block. Does anyone have any thoughts? This is the part that I'm least likely to be able to troubleshoot, being a Drupal noob. Any ideas?
Matt
Check to see if the information matches
Since you can get the default image to print it means there are no parse errors in the PHP so we have a logic error. Just before the 'for' loop, add
then visit a page that should have a taxonomy related banner. It should lists the terms in the banner area. Do you find a term with the vid you expect? If yes and still no image, look at the source, what is the src path form the img tag generated. Does it point to a valid image?
No terms are being output...
For some reason, that print statement you just suggested isn't ouputting any terms. I just get
TERMS<pre></pre>in the rendered source.
That's pretty bizarre, right? This is on a page that's definitely marked with a taxonomy term in my Section vocabulary.
This will only work when viewing a single node
In page.tpl.phph, $node is only sent when viewing a single node (i.e. when the un-aliased path is of the form "node/{nid}"). If you try printing $node is it set?
It doesn't appear to be
When I run print $node; I don't get a return value. Is that what you mean?
Yes, though I would use print_r
So, to double check, you are using Drupal 5, modifying page.tpl.php and the path you are using is of the form "node/{nid}" (unaliased)? If that is true (even if using Drupal 4.7) $node should be set and if you do a print_r on it you should see something. Since you are seeing nothing that is why you loop does nothing (no $node then no $node->taxonomy). When printing out $node it helps to surround it with some text (I often use '|') to be sure the print statement is doing something.
Ahh...wait...
I'm accessing the page at an aliased URL. Meaning, I'm typing http://thenestedtest.com/drupal/some_alias into the browser. Could that be why I'm not outputting a value for $node?
(I really have to thank you guys for continuing to offer up ideas. Clearly, I'm in over my head on this one. The good thing is I've already learned some lessons today I wouldn't have learned otherwise.)
Oh, also
I should have mentioned that yes, I am using drupal 5.x and yes, I am altering page.tpl.php
IT WORKS!
After much debugging, I finally got it to work. So that's great!
But now I'm getting a warning on my admin page saying that there's an invalid argument passed to foreach in page.tpl.php. Here's the code that finally ended up working:
Any ideas as to why I'm being told that the foreach parameter isn't valid? Seems strange, especially since the whole snippet is fiinally working.
Matt
It will produce an error on non-node pages
When $node is not defined, $node->taxonomy is not an array so it complains, so you can change the foreach to
which should get rid of the error. It casts $node->taxonomy to an array and in the case where it is empyt gives you an empty array.
Nevets, I owe you a beer, or a coke, or whatever.
I'm gonna go home, curl up with my apress php book, and celebrate.
I can't thank you enough for your help!
My one last question:
why doesn't this same code work in a php block?
Because $node is not defined
To have the code or similiar work in a block for the cases where the page is a single node being viewed you would need to do this to get the node
The last part,
&& ! arg(2)will restrict the call to node_load() to when viewing a node . If you want the code to run for example when editing a node simply remove that part.And thanks for the beer, it will taste great with dinner :)
See for yourself
I'm still riffing off the top of my head here (sorry for the typo or two or three earlier, but I'm not actually testing these lines myself) and I THINK it should have been:
$term['vid'] not $term->vid and $term['tid'] not $term->tid.
It may be an array, not an object.
A quick print_r() before you did anything should have shown that up. Never just believe code, check what it should be doing - that means saying "what the hell is this $node->taxonomy anyway then?". That way progress is made.
Go figure.
.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/
.dan. is the New Zealand Drupal Developer working on Government Web Standards
Interesting...
Dman - Unfortunately, the changes you suggest didn't work. I appreciate the guidance; I'll dig further into it in the docs and see if something doesn't turn up.
After playing around, I noticed this. Running this:
print_r($node->taxonomy)Returns 1 - a boolean to let me know that yes, it does exist. However, running this:
print_r($node->taxonomy, TRUE)does not return a list of the values, as you would expect.
I'm stumped.
Double check your content filter
Pls double check your content filter, sometime we put in the PHP code but we forgot to using PHP filter.
Will this work for
Will this work for page.taxonomy.tpl.php?
greetings,
Martijn