Note: Requires content.module.'); } } /** * Implementation of hook_widget_info(). */ function content_taxonomy_select_widget_info() { return array( 'content_taxonomy_select' => array( 'label' => 'Select List', 'field types' => array('content_taxonomy'), ), ); return $items; } /** * Implementation of hook_widget_settings */ function content_taxonomy_select_widget_settings($op, $widget) { switch ($op) { case 'form': $form = array(); $options_term = array(); $options_term[0] = '---'; foreach (taxonomy_get_vocabularies() as $voc) { foreach (taxonomy_get_tree($voc->vid) as $term) { $options_term[$voc->name][$term->tid] = $term->name; } } $form['optgroup'] = array( '#type' => 'fieldset', '#title' => t('OptGroups'), '#collapsible' => TRUE, '#description' => t(''), '#weight' => 10, ); $form['optgroup']['group_tid'] = array( '#title' => t('Parent for grouping in first bar'), '#type' => 'select', '#default_value' => isset($widget['group_tid']) ? $widget['group_tid'] : 0, '#options' => $options_term, ); return $form; case 'save': return array('group_tid'); } } /** * Implementation of hook_widget(). */ function content_taxonomy_select_widget($op, &$node, $field, &$node_field) { global $fields; $vid = $field['vid']; $tid = $field['tid']; $depth = (!empty($field['depth'])) ? $field['depth'] : NULL; switch ($op) { case 'prepare form values': break; case 'form': $form = array(); $form[$field['field_name']] = array( '#tree' => TRUE, '#weight' => $field['widget']['weight'], ); $options = array(); if ($field['widget']['group_tid']) { $options = content_taxonomy_select_optgroups($field); } else { $options = content_taxonomy_build_options($vid,$tid,$depth,!$field['multiple']); } $form[$field['field_name']]['tids'] = array( '#type' => 'select', '#title' => t($field['widget']['label']), '#default_value' => is_array($node_field[$field['tid']]) ? array_keys($node_field[$field['tid']]) : $field['widget']['default_value']['tids'], '#multiple' => ($field['multiple']) ? TRUE : FALSE, '#options' => $options, '#required' => $field['required'], '#description' => t($field['widget']['description']), ); return $form; case 'validate': if ($field['required'] && $node_field['tids'] == 0){ form_set_error($field['field_name'] , t('The field %d is required',array('%d'=>$field['widget']['label']))); } break; case 'process form values': if (isset($field['save']) && $field['save'] != 'tag') { if ($field['multiple']) { foreach ($node_field['tids'] as $key => $tid) { if ($tid != 0) $keys[$key] = $tid; } } else { $keys[$node_field['tids']] = $node_field['tids']; } $node_field = content_transpose_array_rows_cols(array('value' => $keys)); } break; } } /** * helper function to generate opt groups in selects */ function content_taxonomy_select_optgroups($field) { $parent = $field['tid']; $group_parent = $field['widget']['group_tid']; if (!$field['multiple']) { $options[0] = t('---'); } //if children in no group $default_terms = taxonomy_get_children($parent); foreach ($default_terms as $default_term) { $default_tids[$default_term->tid] = $default_term->name; } foreach (taxonomy_get_children($group_parent) as $group) { foreach (taxonomy_get_children($group->tid) as $term) { $options[$group->name][$term->tid] = $term->name; unset($default_tids[$term->tid]); } } if (is_array($default_tids)) { foreach ($default_tids as $tid => $name) { $options[$tid] = $name; } } return $options; } ?>