| 1 |
<?php
|
| 2 |
// $Id: term_display.module,v 1.11 2009/03/15 17:41:41 flanker Exp $
|
| 3 |
|
| 4 |
define('TERM_DISPLAY_DEFAULT', 'default');
|
| 5 |
define('TERM_DISPLAY_LIST', 'list');
|
| 6 |
define('TERM_DISPLAY_CUSTOM', 'custom');
|
| 7 |
define('TERM_DISPLAY_NONE', 'none');
|
| 8 |
define('TERM_DISPLAY_LOAD', 'load');
|
| 9 |
|
| 10 |
|
| 11 |
/**
|
| 12 |
* Implementation of hook_theme().
|
| 13 |
*/
|
| 14 |
function term_display_theme() {
|
| 15 |
return array(
|
| 16 |
'term_display_list' => array(
|
| 17 |
'file' => 'term_display.module',
|
| 18 |
'arguments' => array(
|
| 19 |
'vocabulary' => NULL,
|
| 20 |
'terms' => NULL,
|
| 21 |
),
|
| 22 |
),
|
| 23 |
'term_display_custom' => array(
|
| 24 |
'file' => 'term_display.module',
|
| 25 |
'arguments' => array(
|
| 26 |
'vocabulary' => NULL,
|
| 27 |
'terms' => NULL,
|
| 28 |
),
|
| 29 |
),
|
| 30 |
);
|
| 31 |
}
|
| 32 |
|
| 33 |
/**
|
| 34 |
* Theme terms as a list.
|
| 35 |
*/
|
| 36 |
function theme_term_display_list($vocabulary, $terms) {
|
| 37 |
$links = array();
|
| 38 |
foreach ($terms as $term) {
|
| 39 |
$links[] = l($term->name, taxonomy_term_path($term));
|
| 40 |
}
|
| 41 |
return theme('item_list', $links, check_plain($vocabulary->name), 'ul', array('class' => 'vocabulary-list'));
|
| 42 |
}
|
| 43 |
|
| 44 |
/**
|
| 45 |
* Theme terms as a comma-separated list.
|
| 46 |
*/
|
| 47 |
function theme_term_display_custom($vocabulary, $terms) {
|
| 48 |
$links = array();
|
| 49 |
foreach ($terms as $term) {
|
| 50 |
$links[] = l($term->name, taxonomy_term_path($term));
|
| 51 |
}
|
| 52 |
return '<h3 class="title">' . check_plain($vocabulary->name) . ': ' . implode(', ', $links) . '</h3>';
|
| 53 |
}
|
| 54 |
|
| 55 |
|
| 56 |
/**
|
| 57 |
* Implementation of hook_form_alter().
|
| 58 |
*/
|
| 59 |
function term_display_form_taxonomy_form_vocabulary_alter(&$form, &$form_state) {
|
| 60 |
$vid = isset($form['vid']) ? $form['vid']['#value'] : NULL;
|
| 61 |
$term_display_data = term_display_data($vid);
|
| 62 |
|
| 63 |
// Lighten the identification fieldset so it's above ours.
|
| 64 |
$form['identification']['#weight'] = -1;
|
| 65 |
|
| 66 |
$form['term_display'] = array(
|
| 67 |
'#type' => 'fieldset',
|
| 68 |
'#title' => t('Display options'),
|
| 69 |
'#collapsible' => TRUE,
|
| 70 |
'#collapsed' => FALSE,
|
| 71 |
'#description' => t('Set display options on content view for terms in this vocabulary.'),
|
| 72 |
'#weight' => -0.5,
|
| 73 |
);
|
| 74 |
|
| 75 |
$options = array(
|
| 76 |
TERM_DISPLAY_DEFAULT => t('Default'),
|
| 77 |
TERM_DISPLAY_LIST => t('List'),
|
| 78 |
TERM_DISPLAY_CUSTOM => t('Custom (defaults to comma separated)'),
|
| 79 |
TERM_DISPLAY_LOAD => t('Load into $node object'),
|
| 80 |
TERM_DISPLAY_NONE => t('None (hidden)'),
|
| 81 |
);
|
| 82 |
|
| 83 |
$form['term_display']['term_display_style'] = array(
|
| 84 |
'#type' => 'select',
|
| 85 |
'#title' => t('Display style'),
|
| 86 |
'#options' => $options,
|
| 87 |
'#default_value' => $term_display_data['style'],
|
| 88 |
'#description' => t('Select if and how you wish terms to appear on content (node) display.'),
|
| 89 |
);
|
| 90 |
|
| 91 |
$form['term_display']['term_display_weight'] = array(
|
| 92 |
'#type' => 'weight',
|
| 93 |
'#title' => t('Display weight'),
|
| 94 |
'#default_value' => $term_display_data['weight'],
|
| 95 |
'#description' => t('Set a weight for term display to control where terms appear in the content body. Set a low weight to move terms to the top of the content or a high one to move them to the bottom. Applies only when display style is set to "list" or "custom". This option is for advanced usage only; usually it\'s best to leave this at 0.'),
|
| 96 |
);
|
| 97 |
|
| 98 |
}
|
| 99 |
|
| 100 |
/**
|
| 101 |
* Implementation of hook_taxonomy().
|
| 102 |
*/
|
| 103 |
function term_display_taxonomy($op, $type, $object = NULL) {
|
| 104 |
if ($type == 'vocabulary') {
|
| 105 |
if (isset($object['term_display_style']) && isset($object['term_display_weight'])) {
|
| 106 |
switch ($op) {
|
| 107 |
case 'update':
|
| 108 |
// First, try an update.
|
| 109 |
db_query("UPDATE {term_display} SET style = '%s', weight = %d WHERE vid = %d", $object['term_display_style'], $object['term_display_weight'], $object['vid']);
|
| 110 |
// There will be no rows if this we are editing for the first time a term that was created before
|
| 111 |
// term_display was enabled. In that case, we need an insert.
|
| 112 |
if (db_affected_rows()) {
|
| 113 |
break;
|
| 114 |
}
|
| 115 |
// Fall through.
|
| 116 |
case 'insert':
|
| 117 |
db_query("INSERT INTO {term_display} (vid, style, weight) VALUES (%d, '%s', %d)", $object['vid'], $object['term_display_style'], $object['term_display_weight']);
|
| 118 |
break;
|
| 119 |
}
|
| 120 |
}
|
| 121 |
if ($op == 'delete') {
|
| 122 |
db_query('DELETE FROM {term_display} WHERE vid = %d', $object['vid']);
|
| 123 |
}
|
| 124 |
}
|
| 125 |
}
|
| 126 |
|
| 127 |
/**
|
| 128 |
* Fetch term display data for a vocabulary or defaults if there are no registered data.
|
| 129 |
*/
|
| 130 |
function term_display_data($vid) {
|
| 131 |
static $term_display_data;
|
| 132 |
|
| 133 |
if (empty($term_display_data)) {
|
| 134 |
$result = db_query('SELECT vid, style, weight FROM {term_display}');
|
| 135 |
while ($vocabulary = db_fetch_array($result)) {
|
| 136 |
$term_display_data[$vocabulary['vid']] = $vocabulary;
|
| 137 |
}
|
| 138 |
}
|
| 139 |
if (isset($term_display_data[$vid])) {
|
| 140 |
return $term_display_data[$vid];
|
| 141 |
}
|
| 142 |
else {
|
| 143 |
return array(
|
| 144 |
'style' => TERM_DISPLAY_DEFAULT,
|
| 145 |
'weight' => 0,
|
| 146 |
);
|
| 147 |
}
|
| 148 |
}
|
| 149 |
|
| 150 |
/**
|
| 151 |
* Implementation of hook_nodeapi().
|
| 152 |
*/
|
| 153 |
function term_display_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
|
| 154 |
switch ($op) {
|
| 155 |
case 'view':
|
| 156 |
if (isset($node->taxonomy)) {
|
| 157 |
|
| 158 |
$vocabularies = taxonomy_get_vocabularies($node->type);
|
| 159 |
foreach ($vocabularies as $vocabulary) {
|
| 160 |
$term_display_data = term_display_data($vocabulary->vid);
|
| 161 |
if ($term_display_data['style'] == TERM_DISPLAY_DEFAULT) {
|
| 162 |
continue;
|
| 163 |
}
|
| 164 |
$terms = array();
|
| 165 |
foreach ($node->taxonomy as $tid => $term) {
|
| 166 |
if ($term->vid == $vocabulary->vid) {
|
| 167 |
switch ($term_display_data['style']) {
|
| 168 |
case TERM_DISPLAY_LIST:
|
| 169 |
case TERM_DISPLAY_LOAD:
|
| 170 |
case TERM_DISPLAY_CUSTOM:
|
| 171 |
$terms[$tid] = $term;
|
| 172 |
// Fall through.
|
| 173 |
case TERM_DISPLAY_NONE:
|
| 174 |
unset($node->taxonomy[$tid]);
|
| 175 |
break;
|
| 176 |
}
|
| 177 |
}
|
| 178 |
}
|
| 179 |
if (!empty($terms)) {
|
| 180 |
if ($term_display_data['style'] != TERM_DISPLAY_LOAD) {
|
| 181 |
$node->content['term_display_'. $vocabulary->vid] = array(
|
| 182 |
'#weight' => $term_display_data['weight'],
|
| 183 |
'#value' => theme('term_display_'. $term_display_data['style'], $vocabulary, $terms),
|
| 184 |
);
|
| 185 |
}
|
| 186 |
else {
|
| 187 |
$node->term_display[$vocabulary->vid]['vocabulary_name'] = check_plain($vocabulary->name);
|
| 188 |
$node->term_display[$vocabulary->vid]['terms'] = $terms;
|
| 189 |
}
|
| 190 |
}
|
| 191 |
}
|
| 192 |
}
|
| 193 |
break;
|
| 194 |
}
|
| 195 |
}
|