I have a useful snippet I found recently on the forum which prints out the free-tags (from my "Tags" free-tagging vocab) on a node, without printing out other terms the node may also have from any other vocabulary.
This snippet works great so long as it is in my node.tpl.php file:
<?php
// Display the current node's free-tags (but not other vocabs/terms)
$freetags = array();
foreach ($node->taxonomy as $term) {
if ( $term->vid == 2 ) {
$freetags[] = l($term->name, taxonomy_term_path($term));
}
}
print t("Tags") . ": " . implode(', ', $freetags);
?>However I'd like to get this code logic out of the template and include it in template.php with phptemplate_preprocess_node() so I can provide a simple variable such as <?php print $freetags; ?> in node.tpl.php.
This is my attempt in template.php, however I get the following error: "warning: Invalid argument supplied for foreach()":
<?php
function phptemplate_preprocess_node(&$variables) {
// Display the current node's free-tags (but not other vocabs/terms)
$freetags = array();
foreach ($node->taxonomy as $term) {
if ( $term->vid == 2 ) {
$freetags[] = l($term->name, taxonomy_term_path($term));
}
}
$variables['freetags'] = t("Tags") . ": " . implode(', ', $freetags);
}
?>I've made sure my caches and theme registry have been cleared. I tried using print_r($node->taxonomy) in both cases, and on the node.tpl.php (where the snippet works) it prints an array, while from template.php it returns just the number "1" and nothing else. So it seems for some reason the taxonomy isn't available when the code is run in template.php (though I'm not sure how to cause it to be available).
Can someone help me get this code tweaked so that it will work successfully as a phptemplate_preprocess_node() variable?
Thanks in advance :)
-- David
davidnewkerk.com | absolutecross.com
View my Drupal lessons & guides
Comments
Untested...
<?php
function phptemplate_preprocess_node(&$variables) {
$node = $variables['node'];
// Display the current node's free-tags (but not other vocabs/terms)
$freetags = array();
foreach ($node->taxonomy as $term) {
if ( $term->vid == 2 ) {
$freetags[] = l($term->name, taxonomy_term_path($term));
}
}
$variables['freetags'] = t("Tags") . ": " . implode(', ', $freetags);
}
?>
Michelle
--------------------------------------
See my Drupal articles and tutorials or come check out life in the Coulee Region.
-----
Shell Multimedia - My sporadically updated mostly Drupal blog.
Yay!
Everyone who agrees that Michelle is the best, raise your hand (*Keyz raises hand*)
:D
Thanks Michelle.
-- David
davidnewkerk.com | absolutecross.com
View my Drupal lessons & guides
-- David
davidnewkerk.com | absolutecross.com
View my Drupal lessons & guides
LOL
You're funny. :) I just happened to see your plea on IRC when I peeked in, that's all.
Michelle
--------------------------------------
See my Drupal articles and tutorials or come check out life in the Coulee Region.
-----
Shell Multimedia - My sporadically updated mostly Drupal blog.
One more thing, if anyone
One more thing, if anyone knows... would it be possible to test whether there "are" any free-tags available for the node and only print anything out if there are free-tags? At the moment it returns "Tags:" if there are no tags to display.
-- David
davidnewkerk.com | absolutecross.com
View my Drupal lessons & guides
-- David
davidnewkerk.com | absolutecross.com
View my Drupal lessons & guides
Sure,
Sure, change
$variables['freetags'] = t("Tags") . ": " . implode(', ', $freetags);to
if ( !empty($freetags) ) {$variables['freetags'] = t("Tags") . ": " . implode(', ', $freetags);
}
Spectacular, thanks :)
Spectacular, thanks :) I was "almost" there with the !empty (had a hunch that was what I needed to do, since I learned about that when looking at the core tpl.php files) but hadn't yet realized to just wrap it around the $variables part.
Thanks Michelle and nevets :D
-- David
davidnewkerk.com | absolutecross.com
View my Drupal lessons & guides
-- David
davidnewkerk.com | absolutecross.com
View my Drupal lessons & guides