View source
<?php
function content_taxonomy_hierarchical_select_form_alter($form_id, &$form) {
if (isset($form['type']) && $form['type']['#value'] . '_node_form' == $form_id) {
$content_type = $form['type']['#value'];
$fields = content_fields(NULL, $content_type);
$hs_fields = array();
foreach ($fields as $field_name => $field_properties) {
if ($field_properties['type_name'] == $content_type) {
$vid = $field_properties['vid'];
$depth = $field_properties['depth'];
if ($depth != 1) {
if (variable_get("hierarchical_select_status_{$vid}", FALSE)) {
$vocabulary = taxonomy_get_vocabulary($vid);
if ($vocabulary->hierarchy > 0) {
$hs_fields[$field_name]['vid'] = $vid;
$hs_fields[$field_name]['depth'] = $depth;
}
}
}
}
}
foreach ($hs_fields as $field_name => $field_properties) {
$vid = $field_properties['vid'];
$depth = $field_properties['depth'];
if (isset($form[$field_name]) && $form[$field_name]['tids']['#type'] == 'select') {
$form[$field_name]['tids']['#type'] = 'hierarchical_select';
$form[$field_name]['tids']['#hierarchical_select_settings'] = array(
'module' => 'content_taxonomy',
'params' => array(
'vid' => $vid,
'depth' => is_numeric($depth) ? $depth : 999,
),
);
taxonomy_hierarchical_select_update_form_item($form[$field_name]['tids'], $vid);
}
elseif (module_exists('fieldgroup')) {
$field_group = fieldgroup_get_group($content_type, $field_name);
if (isset($form[$field_group][$field_name]) && $form[$field_group][$field_name]['tids']['#type'] == 'select') {
$form[$field_group][$field_name]['tids']['#type'] = 'hierarchical_select';
$form[$field_group][$field_name]['tids']['#hierarchical_select_settings'] = array(
'module' => 'content_taxonomy',
'params' => array(
'vid' => $vid,
'depth' => is_numeric($depth) ? $depth : 999,
),
);
taxonomy_hierarchical_select_update_form_item($form[$field_group][$field_name]['tids'], $vid);
}
}
}
}
}
function content_taxonomy_hierarchical_select_params() {
$params = array(
'vid',
'depth',
);
return $params;
}
function content_taxonomy_hierarchical_select_root_level($params) {
return taxonomy_hierarchical_select_root_level($params);
}
function content_taxonomy_hierarchical_select_children($parent, $params) {
static $tree;
if (!isset($tree)) {
$tree = taxonomy_get_tree($params['vid'], 0);
}
$terms = $tree[$parent]->depth >= $params['depth'] ? array() : taxonomy_get_tree($params['vid'], $parent, -1, 1);
return _taxonomy_hierarchical_select_terms_to_options($terms);
}
function content_taxonomy_hierarchical_select_lineage($item, $params) {
return taxonomy_hierarchical_select_lineage($item, $params);
}
function content_taxonomy_hierarchical_select_valid_item($item, $params) {
if (!is_numeric($item) || $item < 1) {
return FALSE;
}
$term = taxonomy_get_term($item);
return $term->vid == $params['vid'] && $term->depth < $params['depth'];
}
function content_taxonomy_hierarchical_select_item_get_label($item, $params) {
return taxonomy_hierarchical_select_item_get_label($item, $params);
}