Re-implement with hook_link_alter()
David Lesieur - September 14, 2008 - 00:29
| Project: | Taxonomy hide |
| Version: | HEAD |
| Component: | Code |
| Category: | task |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | active |
Jump to:
Description
Taxonomy hide currently works by removing data from a node's taxonomy through hook_nodeapi(). Thus, the data becomes unavailable to modules and themes that may depend on it, causing issues such as #266885: Taxonomy is also hidden from Contemplate and #224287: Taxonomy Hide and Taxonomy Image.
A better approach would be to implement hook_preprocess_node() and simply replace the 'terms' variable.

#1
David,
i like it very much. As soon i get some time, i will submit such feature. thanks.
Can you create a patch?
regards,
massa
#2
I'd love to code it but won't be able to get to it anytime soon, so I've posted this either for anyone interested, or as a reminder for myself. ;-)
#3
I've found the simplest method is to use hook_link_alter, since you don't have to worry about deleting variables in hook_preprocess_node() that some other module might be relying on.
This example re-uses some of the code from _taxonomy_hide_nodeapi. Sorting will take a little more work thought,
<?php
/**
* Implementation of HOOK_link_alter
*
* removes links to taxonomies we are hiding
*
*/
function taxonomy_hide_link_alter(&$links, $node) {
// This module only cares with nodes that have taxonomy terms
// assigned to them.
if (!empty($node->taxonomy)) {
// Get all hidden vocabularies; keys of $hidden are the vocabulary ids.
// Apply either the node type filter or the global filter, not both
// First, apply filter for current node type, if it exists, else apply global filter
// In this way, the setting for the node type overrides the global setting
// If a filter for this type is not set, the global filter will be used.
if ($hidden = array_filter(variable_get('taxonomy_hide_vocabularies_'. $node->type, array())) or
$hidden = array_filter(variable_get('taxonomy_hide_vocabularies', array()))) {
// Hide terms by removing them from the link array
foreach ($links AS $module => $link) {
if(sscanf($module, 'taxonomy_term_%d', $tid)) {
if($hidden[$node->taxonomy[$tid]->vid]) {
// Remove all taxonomy links
unset($links[$module]);
}
}
}
}
}
}
?>
#4
Adam,
nice! i will take a better look soon.
regards,
massa
#5
Subscribing.
I have a site I'm working where I'm using one vocabulary to organize content into the sections it belongs to, and a separate vocabulary to provide author-entered content tags. The second vocabulary is what I'd like to show, and the first vocabulary I'd like to hide, but still have access to in the node object so I can use it's values elsewhere.
Cheers!
#6