View source
<?php
function taxonomy_hierarchical_select_form_alter($form_id, &$form) {
if ($form_id == 'taxonomy_form_vocabulary') {
$vid = $form['vid']['#value'];
$first_part = array_slice($form, 0, 9);
$second_part = array_slice($form, 10);
$form = $first_part;
$form['hierarchical_select'] = array(
'#tree' => TRUE,
'#type' => 'fieldset',
'#title' => t('Hierarchical Select settings'),
);
$form['hierarchical_select']['hierarchical_select_status'] = array(
'#type' => 'checkbox',
'#title' => t('Use the Hierarchical Select form element for this vocabulary'),
'#default_value' => variable_get("hierarchical_select_status_{$vid}", FALSE),
);
$form['hierarchical_select']['hierarchical_select_lineage'] = array(
'#type' => 'select',
'#title' => t('Lineage'),
'#options' => array(
TRUE => t('Save term lineage'),
FALSE => t('Save only the deepest term'),
),
'#default_value' => variable_get("hierarchical_select_lineage_{$vid}", FALSE),
'#description' => t('Saving the term lineage means saving the entire the term itself and
all its ancestors. This is recommended if the vocabulary has the
multiple parents options enabled.'),
);
$form['#submit']['taxonomy_form_vocabulary_submit'] = array();
$form['#submit']['hierarchical_select_taxonomy_form_vocabulary_submit'] = array();
$form += $second_part;
}
if ($form_id == 'views_filters') {
$filters = array();
foreach ($form['view']['#value']->exposed_filter as $id => $filter) {
if (preg_match("/term_node_(\\d+)\\.tid/", $filter['field'], $matches)) {
$vid = $matches[1];
if (variable_get("hierarchical_select_status_{$vid}", FALSE)) {
$vocabulary = taxonomy_get_vocabulary($vid);
if ($vocabulary->hierarchy > 0) {
unset($form["op{$id}"]);
$form['view']['#value']->exposed_filter[$id]['single'] = TRUE;
$form['#parameters']->exposed_filter[$id]['single'] = TRUE;
$form["filter{$id}"]['#type'] = 'hierarchical_select';
$form["filter{$id}"]['#hierarchical_select_settings'] = array(
'lineage' => variable_get("hierarchical_select_lineage_{$vid}", FALSE),
'module' => 'taxonomy',
'params' => array(
'vid' => $vid,
),
);
_hierarchical_select_views_exposed_filters_reposition();
}
}
}
}
}
if (isset($form['type']) && $form['type']['#value'] . '_node_form' == $form_id && is_array($form['taxonomy'])) {
foreach ($form['taxonomy'] as $vid => $form_item) {
if (variable_get("hierarchical_select_status_{$vid}", FALSE)) {
$vocabulary = taxonomy_get_vocabulary($vid);
if ($vocabulary->hierarchy > 0) {
$form['taxonomy'][$vid]['#type'] = 'hierarchical_select';
$form['taxonomy'][$vid]['#hierarchical_select_settings'] = array(
'lineage' => variable_get("hierarchical_select_lineage_{$vid}", FALSE),
'module' => 'taxonomy',
'params' => array(
'vid' => $vid,
),
);
}
}
}
}
}
function taxonomy_hierarchical_select_params() {
$params = array(
'vid',
);
return $params;
}
function taxonomy_hierarchical_select_root_level($params) {
$terms = taxonomy_get_tree($params['vid'], 0, -1, 1);
return _taxonomy_hierarchical_select_terms_to_options($terms);
}
function taxonomy_hierarchical_select_children($parent, $params) {
$terms = taxonomy_get_children($parent, $params['vid']);
return _taxonomy_hierarchical_select_terms_to_options($terms);
}
function taxonomy_hierarchical_select_lineage($item) {
if (!is_numeric($item) || $item == -1) {
$lineage = array();
}
else {
$terms = array_reverse(taxonomy_get_parents_all($item));
foreach ($terms as $term) {
$lineage[] = $term->tid;
}
}
return $lineage;
}
function taxonomy_hierarchical_select_valid_item($item, $params) {
$term = taxonomy_get_term($item);
return $term->vid == $params['vid'];
}
function hierarchical_select_taxonomy_form_vocabulary_submit($form_id, $form_values) {
$vid = $form_values['vid'];
foreach ($form_values['hierarchical_select'] as $setting => $value) {
variable_set("{$setting}_{$vid}", $value);
}
}
function _taxonomy_hierarchical_select_terms_to_options($terms) {
$options = array();
foreach ($terms as $key => $term) {
$options[$term->tid] = $term->name;
}
return $options;
}