/[drupal]/contributions/modules/taxonomy_hide/taxonomy_hide.module
ViewVC logotype

Contents of /contributions/modules/taxonomy_hide/taxonomy_hide.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.9 - (show annotations) (download) (as text)
Fri Aug 3 16:56:58 2007 UTC (2 years, 3 months ago) by davidlesieur
Branch: MAIN
CVS Tags: DRUPAL-5--1-00, HEAD
Branch point for: DRUPAL-5, DRUPAL-6--1
Changes since 1.8: +6 -6 lines
File MIME type: text/x-php
Moved settings to Site configuration rather than Site building. Fixed use of link within t() as recommended by t()'s documentation.
1 <?php
2 // $Id: taxonomy_hide.module,v 1.8 2007/08/03 16:35:01 davidlesieur Exp $
3
4 /**
5 * Implementation of hook_menu().
6 */
7 function taxonomy_hide_menu($may_cache) {
8 $items = array();
9 if ($may_cache) {
10 $items[] = array(
11 'path' => 'admin/settings/taxonomy_hide',
12 'title' => t('Taxonomy hide'),
13 'description' => t('Hide and group vocabulary terms in node views.'),
14 'callback' => 'drupal_get_form',
15 'callback arguments' => 'taxonomy_hide_admin_settings',
16 'access' => user_access('administer site configuration'), // or 'administer taxonomy'
17 );
18 }
19
20 return $items;
21 }
22
23 /**
24 * Implementation of hook_help().
25 */
26 function taxonomy_hide_help($section) {
27 $output = '';
28 switch ($section) {
29 case 'admin/settings/taxonomy_hide':
30 $output = t('The taxonomy_hide module allows you to hide and group vocabulary terms in node views.');
31 break;
32
33 case 'admin/help#taxonomy_hide':
34 $output = t(
35 '<p>The taxonomy_hide module allows you to hide and group vocabulary terms in node views.</p>'.
36 '<p>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.</p>'.
37 '<p>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.</p>'.
38 '<p>You can</p>'.
39 '<ul><li>Specify vocabularies whose terms will be hidden at <a href="@admin">Administer &gt; Site configuration &gt; Taxonomy hide</a>.</li>'.
40 '<li>Enable grouping of vocabulary terms at <a href="@admin">Administer &gt; Site configuration &gt; Taxonomy hide</a>.</li></ul>',
41 array('@admin' => url('admin/settings/taxonomy_hide')));
42 break;
43 }
44
45 return $output;
46 }
47
48 /**
49 * Menu callback for administration settings.
50 */
51 function taxonomy_hide_admin_settings() {
52 $form = array();
53 $form['taxonomy_hide'] = array(
54 '#type' => 'fieldset',
55 '#title' => t('Vocabulary settings'),
56 );
57 $form['taxonomy_hide_group_by_vocabulary'] = array(
58 '#type' => 'checkbox',
59 '#title' => t('Group by vocabulary'),
60 '#return_value' => 1,
61 '#default_value' => variable_get('taxonomy_hide_group_by_vocabulary', 0),
62 '#description' => t('Check this box to group terms by vocabulary in node views.'),
63 '#weight' => -1,
64 );
65
66 // Note that the settings will be saved as an array whose keys are the
67 // vocabulary ids ($vid) and whose values are 0 (disabled) or $vid (enabled).
68 //
69 // The default_value should be an array whose values are the enabled
70 // vocabulary ids.
71 //
72 // The options should be an array whose keys are the vocabulary ids, and whose
73 // values are the names of the vocabularies.
74 $select = array();
75 $vocabularies = taxonomy_get_vocabularies();
76 foreach ($vocabularies as $vocabulary) {
77 $select[$vocabulary->vid] = $vocabulary->name;
78 }
79 $form['taxonomy_hide']['taxonomy_hide_vocabularies'] = array(
80 '#type' => 'checkboxes',
81 '#title' => t('Hide vocabularies'),
82 '#default_value' => array_keys(array_filter(variable_get('taxonomy_hide_vocabularies', array()))),
83 '#options' => $select,
84 '#description' => t('Select the vocabularies whose terms should disappear from node views'),
85 '#extra' => '',
86 '#multiple' => true,
87 );
88
89 return system_settings_form($form);
90 }
91
92 /**
93 * Implementation of hook_nodeapi().
94 */
95 function taxonomy_hide_nodeapi(&$node, $op, $arg = 0, $arg2 = 0) {
96 switch ($op) {
97 case 'view':
98 // Get all hidden vocabularies; keys of $hidden are the vocabulary ids.
99 $hidden = array_filter(variable_get('taxonomy_hide_vocabularies', array()));
100 if (count($hidden) && !empty($node->taxonomy)) {
101 // Hide terms by removing them from the taxonomy field
102 foreach ($node->taxonomy as $key => $value) {
103 if (array_key_exists($value->vid, $hidden)) {
104 unset($node->taxonomy[$key]);
105 }
106 }
107 }
108 if (variable_get('taxonomy_hide_group_by_vocabulary', 0)) {
109 // Sort terms by sorting the taxonomy field
110 usort($node->taxonomy, "_taxonomy_hide_sort");
111 }
112 break;
113 }
114 return;
115 }
116
117 /**
118 * Comparison function for the usort of vocabulary terms.
119 */
120 function _taxonomy_hide_sort($a, $b) {
121 // Cache the extra vocabulary information (we need the vocabulary weight)
122 static $vocs = array();
123 if (!array_key_exists($a->vid, $vocs)) {
124 $vocs[$a->vid] = taxonomy_get_vocabulary($a->vid);
125 }
126 if (!array_key_exists($b->vid, $vocs)) {
127 $vocs[$b->vid] = taxonomy_get_vocabulary($b->vid);
128 }
129
130 // Compare first by vocabulary weight, next by vocabulary id, next by term
131 // weight, next by term name, and finally by term id. This is the same order
132 // as used by taxonomy_node_get_terms, except that we group by vocabulary too.
133 if ($vocs[$a->vid]->weight < $vocs[$b->vid]->weight) {
134 return -1;
135 }
136 elseif ($vocs[$a->vid]->weight > $vocs[$b->vid]->weight) {
137 return 1;
138 }
139 elseif ($a->vid < $b->vid) {
140 return -1;
141 }
142 elseif ($a->vid > $b->vid) {
143 return 1;
144 }
145 elseif ($a->weight < $b->weight) {
146 return -1;
147 }
148 elseif ($a->weight > $b->weight) {
149 return 1;
150 }
151 elseif (strcasecmp($a->name, $b->name)) {
152 return strcasecmp($a->name, $b->name);
153 }
154 elseif ($a->tid < $b->tid) {
155 return -1;
156 }
157 elseif ($a->tid > $b->tid) {
158 return 1;
159 }
160 else {
161 return 0;
162 }
163 }

  ViewVC Help
Powered by ViewVC 1.1.2