Newer
Older
<?php
/**
* @file
* Hansel taxonomy integration
*
* This modules provides switches and breadcrumb actions for taxonomies.
*/
/**
* Implements hook_hansel_action_types().
*/
function hansel_taxonomy_hansel_action_types() {
return array(
'add term path' => array(
'get crumbs' => 'hansel_taxonomy_action_add_term_path_get_crumbs',
'info' => 'hansel_taxonomy_action_add_term_path_info',
'config form' => 'hansel_taxonomy_action_add_term_path_config_form',
),
);
}
/**
* Implements hook_hansel_switch_types().
*/
function hansel_taxonomy_hansel_switch_types() {
return array(
'vocabulary name' => array(
'compare' => 'hansel_taxonomy_switch_vocabulary_compare',
),
);
}
/**
* Callback for "add term path" breadcrumb action
*
* @param array $arguments Values from the configuration form.
* @return array
*/
function hansel_taxonomy_action_add_term_path_get_crumbs($arguments) {
$links = array();
if (drupal_strtolower(hansel_arg(0)) == 'taxonomy' && drupal_strtolower(hansel_arg(1)) == 'term' && is_numeric(hansel_arg(2))) {
$tid = hansel_arg(2);
if ($data = hansel_cache_get("taxonomy:t$tid")) {
return $data;
}
if ($term = taxonomy_term_load($tid)) {
$parents = taxonomy_get_parents_all($term->tid);
foreach ($parents as $term) {
$links[] = array(
'title' => $term->name,
'href' => 'taxonomy/term/' . $term->tid,
);
}
$links = array_reverse($links);
}
hansel_cache_set("taxonomy:t$tid", $links);
}
elseif (drupal_strtolower(hansel_arg(0)) == 'node' && is_numeric(hansel_arg(1))) {
$nid = hansel_arg(1);
if ($data = hansel_cache_get("taxonomy:n$nid")) {
return $data;
}
if ($node = node_load($nid)) {
$terms = array();
foreach (field_info_instances('node', $node->type) as $field => $info) {
if (isset($info['widget']['module']) && ($info['widget']['module'] == 'taxonomy' || $info['widget']['module'] == 'options')) {
$term_field = $node->$field;
if (isset($term_field[$node->language]) || isset($term_field['und'])) {
$localized_terms = isset($term_field[$node->language]) ? $term_field[$node->language] : $term_field['und'];
foreach ($localized_terms as $term) {
if (!isset($term['tid'])) {
// Some items may not be taxonomy terms, like Commerce products (see #1365428).
continue;
}
if (!isset($term['taxonomy_term'])) {
$term['taxonomy_term'] = taxonomy_term_load($term['tid']);
}
if ((empty($arguments['vid'])) || (!empty($arguments['vid']) && $term['taxonomy_term']->vid == $arguments['vid'])) {
$terms[$term['tid']] = $term['taxonomy_term'];
if ($term = reset($terms)) {
$link = array();
$link[$term->tid] = array(
'title' => $term->name,
'href' => 'taxonomy/term/' . $term->tid,
);
$parents = taxonomy_get_parents_all($term->tid);
foreach ($parents as $parent) {
$link[$parent->tid] = array(
'title' => $parent->name,
'href' => 'taxonomy/term/' . $parent->tid,
foreach (array_reverse($link) as $tid => $value) {
$links[$tid] = $value;
}
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
}
}
hansel_cache_set("taxonomy:n$nid", $links);
}
return $links;
}
/**
* Callback for "add term path" action to generate the information line
*
* @param array $arguments Values from the configuration form.
* @return string
*/
function hansel_taxonomy_action_add_term_path_info($arguments) {
if (empty($arguments['vid'])) {
return t('Add term path');
}
else {
$vocab = taxonomy_vocabulary_load($arguments['vid']);
return t('Add term path using the vocabulary %vocab', array('%vocab' => $vocab->name));
}
}
/**
* Callback to generate the configuration form for the "add term path" action
*
* @param array $arguments
* @return array
*/
function hansel_taxonomy_action_add_term_path_config_form($arguments) {
$form = array();
$res = db_query("SELECT vid, name FROM {taxonomy_vocabulary} ORDER BY name");
$options = array(0 => t('All'));
while ($rec = $res->fetchAssoc()) {
$options[$rec['vid']] = $rec['name'];
}
$form['vid'] = array(
'#type' => 'select',
'#title' => t('Vocabulary'),
'#options' => $options,
'#description' => t('Use only terms from this vocabulary. This applies only to node pages.'),
'#default_value' => isset($arguments['vid']) ? $arguments['vid'] : 0,
);
return $form;
}
/**
* Callback for "taxonomy vocabulary" switch to compare a given value.
*
* @param array $arguments
* @param string $value
* @return boolean
*/
function hansel_taxonomy_switch_vocabulary_compare($arguments, $value) {
if (drupal_strtolower(hansel_arg(0)) == 'taxonomy' && drupal_strtolower(hansel_arg(1)) == 'term' && is_numeric(hansel_arg(2))) {
if (($term = taxonomy_term_load(hansel_arg(2))) && ($vocabulary = taxonomy_vocabulary_load($term->vid))) {
return drupal_strtolower($vocabulary->machine_name) == drupal_strtolower($value);
}
}
return FALSE;
}