You are here

function cshs_field_widget_form in Client-side Hierarchical Select 7

Implements hook_field_widget_form().

Provide a widget form.

File

./cshs.module, line 135
A simple clientside hierarchical select widget for taxonomy terms.

Code

function cshs_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $value_key = key($field['columns']);
  $instance_settings = $instance['widget']['settings']['cshs'];
  $settings = $field['settings'];
  $labels = array();
  if (isset($instance_settings['level_labels']) && strlen($instance_settings['level_labels'])) {
    $labels = drupal_explode_tags($instance_settings['level_labels']);
  }

  // Run each level label through t().
  foreach ($labels as &$label) {
    $label = t($label);
  }
  $parent = isset($instance_settings['parent']) ? $instance_settings['parent'] : 0;
  $vocab_name = isset($settings['allowed_values'][0]['vocabulary']) ? $settings['allowed_values'][0]['vocabulary'] : '';

  // Prepare the list of options.
  $options = _cshs_get_options($vocab_name, $parent);

  // Set some properties specific to field-widgets.
  $widget = $element;
  $widget += array(
    '#type' => 'cshs',
    '#label' => $instance['label'],
    '#default_value' => isset($items[$delta][$value_key]) ? $items[$delta][$value_key] : NULL,
    '#options' => $options,
    '#value_key' => $value_key,
    //'#properties' => $properties,
    '#labels' => $labels,
    '#force_deepest' => isset($instance_settings['force_deepest']) ? $instance_settings['force_deepest'] : FALSE,
    '#parent' => isset($instance_settings['parent']) ? $instance_settings['parent'] : 0,
  );
  $element[$value_key] = $widget;
  return $element;
}