You are here

function content_taxonomy_form_field_ui_field_edit_form_alter in Content Taxonomy 7

Implements hook_form_ID_alter().

File

./content_taxonomy.module, line 6

Code

function content_taxonomy_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
  $field = $form['#field'];
  $instance = $form['#instance'];

  // Add parent selector to term reference fields, except to the autocomplete
  // widget, as it ignores the parent setting.
  if ($field['type'] == 'taxonomy_term_reference' && $instance['widget']['type'] != 'taxonomy_autocomplete' && $instance['widget']['type'] != 'autocomplete_deluxe_taxonomy' && $instance['widget']['type'] != 'entityreference_autocomplete' && $instance['widget']['type'] != 'entityreference_autocomplete_tags') {

    // Add parent form.
    foreach ($field['settings']['allowed_values'] as $delta => $tree) {
      $options[0] = '---';

      // todo this might break with huge vocs
      $voc = taxonomy_vocabulary_machine_name_load($tree['vocabulary']);
      foreach (taxonomy_get_tree($voc->vid) as $term) {
        $options[$term->tid] = str_repeat('- ', $term->depth) . $term->name;
      }
      $form['field']['settings']['allowed_values'][$delta]['parent'] = array(
        '#type' => 'select',
        '#title' => t('Parent'),
        '#options' => $options,
        '#default_value' => isset($tree['parent']) ? $tree['parent'] : 0,
      );
      $form['field']['settings']['allowed_values'][$delta]['depth'] = array(
        '#type' => 'textfield',
        '#title' => t('Tree depth'),
        '#default_value' => isset($tree['depth']) ? $tree['depth'] : '',
        '#description' => t('Set the depth of the tree. Leave empty to load all terms.'),
        '#element_validate' => array(
          '_element_validate_integer_positive',
        ),
      );
    }
  }

  // Add opt group setting.
  if ($field['type'] == 'taxonomy_term_reference' && $instance['widget']['type'] == 'options_select') {
    $form['instance']['widget']['settings']['content_taxonomy_opt_groups'] = array(
      '#type' => 'checkbox',
      '#title' => t('Render parent terms as opt-groups'),
      '#default_value' => isset($instance['widget']['settings']['content_taxonomy_opt_groups']) ? $instance['widget']['settings']['content_taxonomy_opt_groups'] : FALSE,
      '#description' => t('This option only works if you have a 2-level hierarchy in your vocabulary. Then the parents in the first level get opt-groups and the child terms will be selectable.'),
    );
  }
}