Oh, this is driving me insane. Really.
I'm using the vote up/down module and need to display the number of points a node has, but NOT in the links section ($links), but within the content($content) section (using Contemplate).
If I insert this code in my node template:
print $node->links['vote_up_down_points']['title']
It gets printed correctly, but it also gets printed on the links section. If I disable the link on the vote up/down settings page ("Do not display link"), the variable $node->links['vote_up_down_points']['title'] is also gone in contemplate.
So, I'm trying to override the links ($links) code using http://drupal.org/node/44708 and http://drupal.org/node/134442 as a guide, but they refer to 4.7 only, and I'm too much of a newbie to be able to adapt this code to 5.2. Anyway, here's what my code looks like in template.php:
function phptemplate_links($links, $attributes = array()) {
return _phptemplate_callback('links', array('links' => $links, 'attributes' => $attributes));
}
and in links.tpl.php:
$link_count = count($links);
print $links;
However, with this code no link section is printed out to the screen andI have no idea what I'm doing wrong... I've been reading Drupal.org for 9 hours trying to get around this problem, but no luck...
I'd appreciate any kind of help and I'm willing to update code (to 5.x) on the links referenced above if I can get this to work. Thanks!
Comments
Some things to try
You are going to discover that theming links in Drupal 5,0 is more complex than in 4.7 since the array of links contains more than just pre-formatted links. As such I think you will discover it is easy to do the override in code and not use the links.tpl.php.
So, first try
You see "Hello World" where the links belong.
If that works, try
First of all, thanks Nevets
First of all, thanks Nevets for the reply! I noticed your comments in a lot of support threads, so thanks a lot for taking the time!!
It does return the constant (Hello World), so the function call is right. However, it is not my intention to destroy the variable from $links; I simply want to omit it in the links section so that I can use it in contemplate. If I destroy it, I don't think I'm going to be able to use it in contemplate.
Here's the code that I want to emulate:
I've tried returning a constant in links.tpl.php but it didn't work, so I guess the problem is with the arguments in the callback function. BTW, why is it easier to override in code instead of using links.tpl.php? I think it'll probably be easier to implement the change directly in theme_links instead of using _callback, right?
Thanks!
Some concepts here
We are dealing with both $node->links and $links. $links is the themed version of $node->links after passing through theme_links (by default). Contemplate only has access to $node->links (it themes the node content, generally not the additional information) so changing $links in your override of theme_links will not change what contemplate sees.
In this case using the override function (phptemplate_links) is easier than links.tpl.php because most of the work is in PHP code. It seems silly to me to invoke a callback that parse a file (links.tpl.php) that is mostly PHP code, it can be done, I just do not see the benefit. Now files like page.tpl.php, node.tpl.php etc are mostly HTML with some PHP so that is different.
So back to my original suggestion, the call to unset() removes the link you do not want to see as part of the "standard" links. We then call theme_links() directly which will perform the default theming for a list of links (minus the one we unset()). This all happens after contemplate does it thing.
One should avoid modify core file because it makes updating a challange. So it is better to write the override function (phptemplate_links) than changing the original.
Hope this helps.
It works!! THANK YOU!!! So
It works!! THANK YOU!!!
So Contemplate has access to node variables before they are themed by theme() ?
I had made a little hack just to get me going:
but your code works like a charm (plus I don't have to copy & paste theme_links() anymore). Working with overrides and callbacks for tpl.php files is hard for those just getting started, plus there isn't much documentation about contemplate, so it's hard to know those nuances (like which methods get invoked befor which). Thank God there's great people in this forum.
Anyways, I'll add this info to the support info on http://drupal.org/node/157969 and thanks again for the perfect explanation!