You are here

function term_merge_form in Term Merge 7

Menu callback.

Allow user to specify which terms to be merged into which term and any other settings needed for the term merge action.

Parameters

object $vocabulary: Fully loaded taxonomy vocabulary object

object $term: Fully loaded taxonomy term object that should be selected as the default merge term in the form. If the $vocabulary is omitted, the vocabulary of $term is considered

Return value

array Array of the form in Form API format

TODO: accidentally this function happens to implement hook_form() which is definitely not my intention. This function must be renamed to a safer name.

1 string reference to 'term_merge_form'
term_merge_menu in ./term_merge.module
Implements hook_menu().

File

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

Code

function term_merge_form($form, $form_state, $vocabulary = NULL, $term = NULL) {
  if (is_null($vocabulary)) {
    $vocabulary = taxonomy_vocabulary_load($term->vid);
  }
  if (!isset($form_state['storage']['confirm'])) {

    // We are at the set up step.
    $tree = taxonomy_get_tree($vocabulary->vid);
    $term_branch_value = is_null($term) ? array() : array(
      $term->tid,
    );
    if (variable_get('taxonomy_override_selector', FALSE) && module_exists('hs_taxonomy')) {

      // We use Hierarchical Select module if it's available and configured to
      // be used for taxonomy selects.
      $form['term_branch'] = array(
        '#type' => 'hierarchical_select',
        // @todo: figure out why #required => TRUE doesn't work.
        // As a matter of fact, this issue seems to cover our case.
        // http://drupal.org/node/1275862.

        //'#required' => TRUE,
        '#config' => array(
          'module' => 'hs_taxonomy',
          'params' => array(
            'vid' => $vocabulary->vid,
            'exclude_tid' => NULL,
            'root_term' => FALSE,
          ),
          'enforce_deepest' => 0,
          'entity_count' => 0,
          'require_entity' => 0,
          'save_lineage' => 0,
          'level_labels' => array(
            'status' => 0,
          ),
          'dropbox' => array(
            'status' => 1,
            'limit' => 0,
          ),
          'editability' => array(
            'status' => 0,
          ),
          'resizable' => TRUE,
          'render_flat_select' => 0,
        ),
      );
    }
    else {

      // Falling back on a simple <select>.
      $options = array();
      foreach ($tree as $v) {
        $options[$v->tid] = str_repeat('-', $v->depth) . $v->name . ' [tid: ' . $v->tid . ']';
      }
      $form['term_branch'] = array(
        '#type' => 'select',
        '#required' => TRUE,
        '#multiple' => TRUE,
        '#options' => $options,
        '#size' => 8,
      );
    }
    $form['term_branch'] = array(
      '#title' => t('Terms to Merge'),
      '#description' => t('Please, choose the terms you want to merge into another term.'),
      '#ajax' => array(
        'callback' => 'term_merge_form_term_trunk',
        'wrapper' => 'term-merge-form-term-trunk',
        'method' => 'replace',
        'effect' => 'fade',
      ),
      '#default_value' => $term_branch_value,
    ) + $form['term_branch'];

    // We overwrite term branch value with the one from $form_state, if there is
    // something there.
    if (isset($form_state['values']['term_branch']) && is_array($form_state['values']['term_branch'])) {
      $term_branch_value = $form_state['values']['term_branch'];
    }
    term_merge_form_base($form, $form_state, $vocabulary, $term_branch_value);
    $form['actions'] = array(
      '#type' => 'actions',
    );
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Submit'),
    );
  }
  else {

    // We are at the confirmation step.
    $count = count($form_state['values']['term_branch']);
    $question = format_plural($count, 'Are you sure you want to merge 1 term?', 'Are you sure you want to merge @count terms?');
    $form = confirm_form($form, $question, 'admin/structure/taxonomy/' . $vocabulary->machine_name);
  }
  return $form;
}