View source
<?php
function content_taxonomy_tree_theme() {
return array(
'content_taxonomy_tree' => array(
'arguments' => array(
'element' => NULL,
),
),
);
}
function content_taxonomy_tree_widget_info() {
return array(
'content_taxonomy_tree' => array(
'label' => t('Tree'),
'field types' => array(
'content_taxonomy',
),
'multiple values' => CONTENT_HANDLE_MODULE,
'callbacks' => array(
'default value' => CONTENT_CALLBACK_DEFAULT,
),
),
);
return $items;
}
function content_taxonomy_tree_widget_settings($op, $widget) {
switch ($op) {
case 'form':
$form['settings'] = array(
'#type' => 'fieldset',
'#title' => t('Settings for Trees'),
'#collapsible' => TRUE,
'#weight' => 10,
);
$form['settings']['expand_all'] = array(
'#type' => 'checkbox',
'#title' => t('Expand whole tree by default'),
'#default_value' => isset($widget['expand_all']) ? $widget['expand_all'] : 0,
'#description' => t('Otherwise only branches, where a term is selected get expanded by default'),
);
return $form;
case 'save':
return array(
'expand_all',
);
}
}
function content_taxonomy_tree_elements() {
return array(
'content_taxonomy_tree' => array(
'#input' => TRUE,
'#columns' => array(
'value',
),
'#delta' => 0,
'#process' => array(
'content_taxonomy_tree_process',
),
'#element_validate' => array(
'content_taxonomy_tree_validate',
),
'#autocomplete_path' => FALSE,
),
);
}
function content_taxonomy_tree_widget(&$form, &$form_state, $field, $items, $delta = NULL) {
$element = array(
'#type' => 'content_taxonomy_tree',
'#default_value' => !empty($items) ? $items : array(),
);
return $element;
}
function content_taxonomy_tree_process($element, $edit, &$form_state, $form) {
$field_name = $element['#field_name'];
$field = $form['#field_info'][$field_name];
$field_key = $element['#columns'][0];
if (is_array($element['#value'])) {
if (array_key_exists($field_key, $element['#value'])) {
if ($field['multiple']) {
$selected_terms = _taxonomy_manager_tree_get_selected_terms($element['#value']['value']);
$element['#value'] = content_transpose_array_rows_cols(array(
$field_key => array_values($selected_terms),
));
}
}
$element['#value'] = content_taxonomy_tree_data2form($element, $element['#value'], $field);
}
$element[$field_key] = array(
'#type' => 'taxonomy_manager_tree',
'#default_value' => isset($element['#value']) ? $element['#value'] : '',
'#vid' => $field['vid'],
'#parent' => content_taxonomy_field_get_parent($field),
'#default_value' => is_array($element['#value']) ? $element['#value'] : array(),
'#render_whole_tree' => TRUE,
'#pager' => FALSE,
'#add_term_info' => FALSE,
'#expand_all' => $field['widget']['expand_all'],
'#tree_is_required' => $field['required'],
'#required' => FALSE,
'#multiple' => isset($field['multiple']) ? $field['multiple'] : FALSE,
'#field_name' => $element['#field_name'],
'#delta' => $element['#delta'],
'#columns' => $element['#columns'],
'#title' => $element['#title'],
'#description' => $element['#description'],
);
$form_state['#field_info'][$field['field_name']] = $field;
return $element;
}
function content_taxonomy_tree_validate($element, &$form_state) {
$field = $form_state['#field_info'][$element['#field_name']];
$field_key = $element['#columns'][0];
$values = content_taxonomy_tree_form2data($element, $form_state, $field);
if ($field['required'] && is_null($values[0]['value']) && !empty($element['#value'])) {
form_error($element[$field_key], t('!name field is required.', array(
'!name' => $element['#title'],
)));
}
if ($field['multiple'] >= 2 && count($element['#value']) > $field['multiple']) {
form_error($element[$field_key], t('%name: this field cannot hold more than @count values.', array(
'%name' => t($field['widget']['label']),
'@count' => $field['multiple'],
)));
}
form_set_value($element, $values, $form_state);
}
function content_taxonomy_tree_data2form($element, $values, $field) {
$terms = array();
foreach ($values as $delta => $entry) {
if ($entry['value']) {
$terms[] = $entry['value'];
}
}
return $terms;
}
function content_taxonomy_tree_form2data($element, &$form_state, $field) {
$field_key = $element['#columns'][0];
$selected_terms = $form_state['values'][$element['#field_name']][$field_key]['selected_terms'];
if (!is_array($selected_terms) || !count($selected_terms)) {
$selected_terms[] = NULL;
}
$form_state['#field_info'][$field['field_name']] = $field;
return content_transpose_array_rows_cols(array(
$element['#columns'][0] => array_values($selected_terms),
));
}
function theme_content_taxonomy_tree($element) {
return $element['#children'];
}