'admin/settings/taxonomy_hide',
'title' => t('Taxonomy hide'),
'description' => t('Hide and group vocabulary terms in node views.'),
'callback' => 'drupal_get_form',
'callback arguments' => 'taxonomy_hide_admin_settings',
'access' => user_access('administer site configuration'), // or 'administer taxonomy'
);
}
return $items;
}
/**
* Implementation of hook_help().
*/
function taxonomy_hide_help($section) {
$output = '';
switch ($section) {
case 'admin/settings/taxonomy_hide':
$output = t('The taxonomy_hide module allows you to hide and group vocabulary terms in node views.');
break;
case 'admin/help#taxonomy_hide':
$output = t(
'
The taxonomy_hide module allows you to hide and group vocabulary terms in node views.
'.
'When you view a node, you usually see all vocabulary terms it is associated with. Sometimes, you might want to hide terms of a specific vocabulary. This module allows you to specify the vocabularies whose terms are never displayed in node views.
'.
'The list of vocabulary terms is usually sorted first by vocabulary weight, and next alphabetically. So terms of different vocabularies with the same weight are mixed. This module allows you to group terms by vocabulary in node views, which means that all terms of one vocabulary are always next to each other.
'.
'You can
'.
'',
array('@admin' => url('admin/settings/taxonomy_hide')));
break;
}
return $output;
}
/**
* Menu callback for administration settings.
*/
function taxonomy_hide_admin_settings() {
$form = array();
$form['taxonomy_hide'] = array(
'#type' => 'fieldset',
'#title' => t('Vocabulary settings'),
);
$form['taxonomy_hide_group_by_vocabulary'] = array(
'#type' => 'checkbox',
'#title' => t('Group by vocabulary'),
'#return_value' => 1,
'#default_value' => variable_get('taxonomy_hide_group_by_vocabulary', 0),
'#description' => t('Check this box to group terms by vocabulary in node views.'),
'#weight' => -1,
);
// Note that the settings will be saved as an array whose keys are the
// vocabulary ids ($vid) and whose values are 0 (disabled) or $vid (enabled).
//
// The default_value should be an array whose values are the enabled
// vocabulary ids.
//
// The options should be an array whose keys are the vocabulary ids, and whose
// values are the names of the vocabularies.
$select = array();
$vocabularies = taxonomy_get_vocabularies();
foreach ($vocabularies as $vocabulary) {
$select[$vocabulary->vid] = $vocabulary->name;
}
$form['taxonomy_hide']['taxonomy_hide_vocabularies'] = array(
'#type' => 'checkboxes',
'#title' => t('Hide vocabularies'),
'#default_value' => array_keys(array_filter(variable_get('taxonomy_hide_vocabularies', array()))),
'#options' => $select,
'#description' => t('Select the vocabularies whose terms should disappear from node views'),
'#extra' => '',
'#multiple' => true,
);
return system_settings_form($form);
}
/**
* Implementation of hook_nodeapi().
*/
function taxonomy_hide_nodeapi(&$node, $op, $arg = 0, $arg2 = 0) {
switch ($op) {
case 'view':
// Get all hidden vocabularies; keys of $hidden are the vocabulary ids.
$hidden = array_filter(variable_get('taxonomy_hide_vocabularies', array()));
if (count($hidden) && !empty($node->taxonomy)) {
// Hide terms by removing them from the taxonomy field
foreach ($node->taxonomy as $key => $value) {
if (array_key_exists($value->vid, $hidden)) {
unset($node->taxonomy[$key]);
}
}
}
if (variable_get('taxonomy_hide_group_by_vocabulary', 0)) {
// Sort terms by sorting the taxonomy field
usort($node->taxonomy, "_taxonomy_hide_sort");
}
break;
}
return;
}
/**
* Comparison function for the usort of vocabulary terms.
*/
function _taxonomy_hide_sort($a, $b) {
// Cache the extra vocabulary information (we need the vocabulary weight)
static $vocs = array();
if (!array_key_exists($a->vid, $vocs)) {
$vocs[$a->vid] = taxonomy_get_vocabulary($a->vid);
}
if (!array_key_exists($b->vid, $vocs)) {
$vocs[$b->vid] = taxonomy_get_vocabulary($b->vid);
}
// Compare first by vocabulary weight, next by vocabulary id, next by term
// weight, next by term name, and finally by term id. This is the same order
// as used by taxonomy_node_get_terms, except that we group by vocabulary too.
if ($vocs[$a->vid]->weight < $vocs[$b->vid]->weight) {
return -1;
}
elseif ($vocs[$a->vid]->weight > $vocs[$b->vid]->weight) {
return 1;
}
elseif ($a->vid < $b->vid) {
return -1;
}
elseif ($a->vid > $b->vid) {
return 1;
}
elseif ($a->weight < $b->weight) {
return -1;
}
elseif ($a->weight > $b->weight) {
return 1;
}
elseif (strcasecmp($a->name, $b->name)) {
return strcasecmp($a->name, $b->name);
}
elseif ($a->tid < $b->tid) {
return -1;
}
elseif ($a->tid > $b->tid) {
return 1;
}
else {
return 0;
}
}