first phptemplate variables function not working
Hi There -
This is the first time I've written a phptemplate variables function and I think I'm missing something fundamental. I suspected that I wasn't using the correct argument in the switch function. Have tried putting something in for a single node type into the case argument, in the case below, I used 'image'. I've also tried 'page'. Neither gets variables to the image node.
So my first question is, if I want to send to all the pages, regardless of node type, do I use case page, or is that reserved for the page content type? Do I need to iterate the same function for each node type?
I found this code on the issue queue of Views Bookmark and it looked sweet. It wasn't obvious to me where this code belonged, so because I want to send it to a heap of different node types, and also to put logic into the output, that it belonged in the template.php file. I'm not sure if my thinking is right on this.
You can find the discussion here: http://drupal.org/node/174604
and the code right here:
<?php
$counts = module_invoke('view_bookmark', 'get_counts', $nid);
print $counts[vbid_here]['count'];
?>So then I went and took this sweet innocent code and did the following to try to get it to go into phptemplate and get it to display nicely depending on how many people had voted and it got a little more complicated, but lost its two line elegance. In the end, no output to the image node. I got nothin. On the other hand, I'm the kind of beginner who's elated that I didn't get white screen of death.
So I'm wondering whether that code is meant to go into the pages directly, or into template.php. If it's supposed to go into the page, what do I need to do to it to make it work in template.php? If I'm supposed to put it in the page template and if I want to customize the output with the logic below, should I still be putting it in the page template? (I've been operating under the assumption that we're supposed to send clean variables to the page templates.)
<?php
/**
* This is to send to nodes the variables that display
* the number of times something's been bookmarked
* helpful or funny. Uses Views Bookmark module.
*
* Node types affected are: blog, book, forum, image,
* journal, news
*/
function _phptemplate_variables($hook, $vars) {
switch($hook) {
case 'image' :
if (module_exists('views_bookmark')) {
$counts = module_invoke('views_bookmark', 'get_counts', $nid);
$funny_count = $counts[3]['count'];
$helpful_count = $counts[4]['count'];
if ($funny_count > 0) {
if ($funny_count == 1) {
$funny = '$funny_count member found this funny.';
} elseif ($funny_count > 1) {
$funny = '$funny_count members found this funny.';
}
}
if ($helpful_count > 0) {
if ($helpful_count == 1) {
$helpful = '$helpful_count member found this helpful.';
} elseif ($funny_count > 1) {
$helpful = '$helpful_count members found this helpful.';
}
}
$vars['funny'] = '$funny';
$vars['helpful'] = '$helpful';
break;
}
}
return $vars;
}
?>
By pages in the last paragraph above, I mean page templates
There are several custom page-contenttype.tpl.php files and node-contenttype.tpl.php files. My apologies for being unclear above. I meant directly into the template files and not the pages.
OK ...
I'm guessing your trying to poke stuff into your node.tpl files? In which case you would want something like this
function _phptemplate_variables($hook, $vars) {
switch($hook) {
case 'node' :
//if we are about to load a node-XXX.tpl file
$node = $vars['node']; //grab the node in question
if ($node->type=='image' || $node->type=='blog'){
//if the node is of type image or blog (add the rest of your types to the above
if (module_exists('views_bookmark')) {
$counts = module_invoke('views_bookmark', 'get_counts', $nid);
$funny_count = $counts[3]['count'];
$helpful_count = $counts[4]['count'];
$vars['funny'] = $funny_count;
$vars['helpful'] = $helpful_count;
}
}
break;
}
return $vars;
}
?>
Apologies if the above has any syntax errors - I hate writing code in the browser window!
The $hook variable contains the name of the template you are about to 'go into' - in your case I think you are looking to poke stuff into the node template, so we check for that. We then grab the $node object out of the vars array and check its type, if its an image or a blog we do our stuff with the counts and poke the values back into the vars array ready to be sent to the node template file.
Hope this helps
Cheers,
Mike,
Computerminds offer Drupal development, consulting and training
Wonderful! Thanks. I'll try that right now.
So you'd put the conditional logic into the node template file and leave the numbers as the variables that get sent to the page. That's really interesting.
Which leads me to two questions:
1. if I
print_r($page), will that bring up the variables available to the page template file?2. Another question is from a function I found on a wayward template.php file. It called the array that it was adding to '$variables'. (And it worked.) Are there two arrays: the $vars array and the $variables array? Did the $variables array just get added to whatever variable would flow through the phptemplate_variables function? Does it matter what the array or variables are called?
Thanks so much for your reply. I'm going to go try it right now.
Nothing showed up on devload
No errors. Just no variables either. My IDE likes the code fine. The only thing I changed was to add the content types in the 'or' statement. Tried taking out the or statement and corresponding curly brackets. Nada. (This is on dev load. If I should try something else, please let me know.)
I don't have a big enough toolkit to know what to try next. In the absence of any feedback from the site, what do good Drupalistas do? How do you think about this? What are likely things to check next?
If I'm going to create a test of some sort when I'm not getting any feedback from the site, would I put the same code into the page or node template, excluding the $vars array and phptemplate_variables accessories? If I want to check the array part of the function, should I create something simple like
$testvariable = 42;, add that to the array and see if it gets to the node?Am I overthinking this? Is there a simpler way to power through this problem?
Got the following to work on the node templates:
Now that I've got this working in the node template files (node.tpl.php et al) from what I understand it's considered good Drupal practice to move the big messy stuff over to the template.php. Is this true, or does logic like this belong in the node template files? What's common practice?
In general, if you get something working in a page or node template file, what are the considerations when you want to move the messy function stuff over to template.php?
This works. Put the code below at the top of your file:
<?php
$counts = module_invoke('views_bookmark', 'get_counts', $nid);
$funny_count = $counts[3]['count'];
$helpful_count = $counts[4]['count'];
if ($funny_count > 0) {
if ($funny_count == 1) {
$funny = $funny_count .' member found this funny.';
} elseif ($funny_count > 1) {
$funny = $funny_count .' members found this funny.';
}
}
if ($helpful_count > 0) {
if ($helpful_count == 1) {
$helpful = $helpful_count .' member found this helpful.';
} elseif ($helpful_count > 1) {
$helpful = $helpful_count .' members found this helpful.';
}
}
?>
And this within the html:
<?php if ($helpful): print $helpful; endif; ?><?php if ($funny): print $funny; endif; ?>
My opinion
I would create a function in template.php like this:
<?php
function _bookmark_counts($vars)
$counts = module_invoke('views_bookmark', 'get_counts', $nid);
$funny_count = $counts[3]['count'];
$helpful_count = $counts[4]['count'];
if ($funny_count > 0) {
if ($funny_count == 1) {
$vars['funny'] = $funny_count .' member found this funny.';
} elseif ($funny_count > 1) {
$vars['funny'] = $funny_count .' members found this funny.';
}
}
if ($helpful_count > 0) {
if ($helpful_count == 1) {
$vars['helpful'] = $helpful_count .' member found this helpful.';
} elseif ($helpful_count > 1) {
$vars['helpful'] = $helpful_count .' members found this helpful.';
}
}
return $vars;
}
?>
Then in the 'node' hook of _phptemplate_variables put:
$vars = _bookmark_counts($vars);
This will make the $funny and $helpful variables show up in any node-TYPE.tpl.php as well as node.tpl.php.
This bit of docs from advprofile might help you a bit: http://drupal.org/node/207841
Michelle
--------------------------------------
See my Drupal articles and tutorials or come check out life in the Coulee Region.
Michelle, I really like that.
Am working on quite a large site right now and am merging several _phptemplate_variables functions and this way is sooo much nicer than the one I've been using! I appreciate your answering me with an approach that helps me think broadly about how to work with template.php and these functions. Very nice! (other keywords so people can find the coolness that Michelle did: multiple _phptemplate_variables functions, views bookmark node count)