You are here

function term_merge_form_base in Term Merge 7

Base of the term merge action form.

It is extracted into a separate function to better internal code reuse.

Parameters

object $vocabulary: Fully loaded Taxonomy vocabulary where the merge should occur

array $term_branch_value: Array of Taxonomy term IDs that are nominated as branch terms.

2 calls to term_merge_form_base()
term_merge_action_form in ./term_merge.module
Generate the configuration form for action "Term merge".
term_merge_form in ./term_merge.pages.inc
Menu callback.

File

./term_merge.pages.inc, line 18
Menu page callbacks for Term Merge module.

Code

function term_merge_form_base(&$form, &$form_state, $vocabulary, $term_branch_value) {
  $form['#vocabulary'] = $vocabulary;
  $form['#term_merge_term_branch'] = $term_branch_value;
  $tree = taxonomy_get_tree($vocabulary->vid);
  $form['term_trunk'] = array(
    '#type' => 'fieldset',
    '#title' => t('Merge Into'),
    '#prefix' => '<div id="term-merge-form-term-trunk">',
    '#suffix' => '</div>',
    '#tree' => TRUE,
    '#element_validate' => array(
      'term_merge_form_base_term_trunk_validate',
    ),
  );

  // Array of currently available widgets for choosing term trunk.
  $term_trunk_widget_options = array(
    'autocomplete' => 'Autocomplete',
  );
  if (variable_get('taxonomy_override_selector', FALSE) && module_exists('hs_taxonomy')) {
    $term_trunk_widget_options['hs_taxonomy'] = t('Hierarchical Select');
    $term_trunk_widget = 'hs_taxonomy';
  }
  else {
    $term_trunk_widget_options['select'] = t('Select');
    $term_trunk_widget = 'select';
  }

  // If the vocabulary is too big, by default we want the trunk term widget to
  // be autocomplete instead of select or hs_taxonomy.
  if (count($tree) > variable_get('term_merge_select_limit', 200)) {
    $term_trunk_widget = 'autocomplete';
  }

  // Override the term trunk widget if settings are found in $form_state.
  if (isset($form_state['values']['term_trunk']['widget']) && in_array($form_state['values']['term_trunk']['widget'], array_keys($term_trunk_widget_options))) {
    $term_trunk_widget = $form_state['values']['term_trunk']['widget'];
  }

  // TODO: the trunk term widgets should be implemented as cTools plugins.
  $form['term_trunk']['widget'] = array(
    '#type' => 'radios',
    '#title' => t('Widget'),
    '#required' => TRUE,
    '#options' => $term_trunk_widget_options,
    '#default_value' => $term_trunk_widget,
    '#description' => t('Choose what widget you prefer for entering the term trunk.'),
    '#ajax' => array(
      'callback' => 'term_merge_form_term_trunk',
      'wrapper' => 'term-merge-form-term-trunk',
      'method' => 'replace',
      'effect' => 'fade',
    ),
  );

  // @todo:
  // There is a known bug, if user has selected something in one widget, and
  // then changes the widget, $form_states['values'] will hold the value for
  // term trunk form element in the format that is used in one widget, while
  // this value will be passed to another widget. This triggers different
  // unpleasant effects like showing tid instead of term's name or vice-versa.
  // I think we should just empty $form_state['values'] for the term trunk
  // form element when widget changes. Better ideas are welcome!
  $function = 'term_merge_form_term_trunk_widget_' . $term_trunk_widget;
  $function($form, $form_state, $vocabulary, $term_branch_value);

  // Ensuring the Merge Into form element has the same title no matter what
  // widget has been used.
  $form['term_trunk']['tid']['#title'] = t('Merge into');

  // Adding necessary options of merging.
  $form += term_merge_merge_options_elements($vocabulary);
}