You are here

function term_level_field_widget_form in Term Level Field 7

Implements hook_field_widget_form().

File

./term_level.module, line 278
Field type for referencing terms with a level to an entity.

Code

function term_level_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $field_settings = $field['settings'];
  $widget_settings = $instance['widget']['settings'];
  $voc = taxonomy_vocabulary_machine_name_load($field_settings['voc']);
  $levels = term_level_options_list($field);
  $default_values = _term_level_extract_default_values($items);

  // Submitted values through AJAX.
  if (isset($form_state['values']['default_terms'])) {
    foreach ($form_state['values']['default_terms'] as $tid => $level) {
      $default_values[$tid] = $level;
    }
  }

  // Term added through AJAX.
  $added_term = 0;
  if (isset($form_state['values']['added_term']) && $form_state['values']['added_term']) {
    $added_term = $form_state['values']['added_term'];
  }
  $groups = _term_level_get_groups($voc->vid, $widget_settings['term_entries_limit_per_group'], $default_values, $added_term);
  $element['#element_validate'] = array(
    'term_level_widget_validate',
  );
  $element['#required'] = $instance['required'];
  $label = filter_xss_admin($instance['label']);
  $label = $instance['required'] ? $label . ' ' . theme('form_required_marker', array()) : $label;
  $element['label'] = array(
    '#markup' => '<label class="term-level-widget-label">' . $label . '</label>',
  );
  if (!empty($instance['description'])) {
    $element['description'] = array(
      '#markup' => '<div class="term-level-widget-description description">' . filter_xss_admin($instance['description'] . '</div>'),
    );
  }
  foreach ($groups['parents'] as $parent => $parent_name) {
    $element[] = array(
      '#type' => 'term_level',
      '#title' => $parent_name,
      '#terms' => isset($groups['terms'][$parent]) ? $groups['terms'][$parent] : array(),
      '#levels' => $levels,
      '#parent' => $parent,
      '#vid' => $voc->vid,
      '#tag_cloud_terms' => $widget_settings['tag_cloud'] && isset($groups['tag_cloud'][$parent]) ? $groups['tag_cloud'][$parent] : array(),
    );
  }
  return $element;
}