You are here

function term_merge_merge_options_elements in Term Merge 7

Generate and return form elements that control behavior of merge action.

Output of this function should be used in any form that merges terms, ensuring unified interface. It should be used in conjunction with term_merge_merge_options_submit(), which will process the submitted values for you and return an array of merge settings.

Parameters

object $vocabulary: Fully loaded taxonomy vocabulary object in which merging occurs

Return value

array Array of form elements that allow controlling term merge action

See also

term_merge_merge_options_submit()

2 calls to term_merge_merge_options_elements()
term_merge_duplicates_form in ./term_merge.pages.inc
Generate 'term_merge_duplicates_form'.
term_merge_form_base in ./term_merge.pages.inc
Base of the term merge action form.

File

./term_merge.module, line 703
Provide functionality for merging taxonomy terms one into another.

Code

function term_merge_merge_options_elements($vocabulary) {

  // @todo: it would be nice to provide some ability to supply default values
  // for each setting.
  $form = array();

  // Getting bundle name and a list of fields attached to this bundle for
  // further use down below in the code while generating form elements.
  $bundle = field_extract_bundle('taxonomy_term', $vocabulary);
  $instances = field_info_instances('taxonomy_term', $bundle);
  $form['term_branch_keep'] = array(
    '#type' => 'checkbox',
    '#title' => t('Only merge occurrences'),
    '#description' => t('Check this if you want to only merge the occurrences of the specified terms, i.e. the terms will not be deleted from your vocabulary.'),
  );
  if (!empty($instances)) {
    $options = array();
    foreach ($instances as $instance) {
      $options[$instance['field_name']] = $instance['label'];
    }
    $form['merge_fields'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Merge Term Fields'),
      '#description' => t('Check the fields whose values from branch terms you want to add to the values of corresponding fields of the trunk term. <b>Important note:</b> the values will be added until the cardinality limit for the selected fields is reached and only unique values for each field will be saved.'),
      '#options' => $options,
    );
  }
  $form['keep_only_unique'] = array(
    '#type' => 'checkbox',
    '#title' => t('Keep only unique terms after merging'),
    '#description' => t('Sometimes after merging you may end up having a node (or any other entity) pointing twice to the same taxonomy term, tick this checkbox if want to keep only unique terms in other entities after merging.'),
    '#default_value' => TRUE,
  );
  if (module_exists('redirect')) {
    $form['redirect'] = array(
      // We respect access rights defined in redirect.module here.
      '#access' => user_access('administer redirects'),
      '#type' => 'select',
      '#title' => t('Create Redirect'),
      '#description' => t('If you want to create an HTTP redirect from your branch terms to the trunk term, please, choose the HTTP redirect code here.'),
      '#required' => TRUE,
      '#options' => term_merge_redirect_options(),
      '#default_value' => variable_get('term_merge_default_redirect', TERM_MERGE_NO_REDIRECT),
    );
  }
  else {
    $form['redirect'] = array(
      '#markup' => t('Enable the module ' . l('Redirect', 'http://drupal.org/project/redirect') . ' if you want to do an HTTP redirect from your term branch to the term trunk.'),
    );
  }
  if (module_exists('synonyms')) {
    $options = array(
      '' => t('None'),
    );
    if (function_exists('synonyms_behavior_get')) {

      // We are in Synonyms 7.x-1.5 and above.
      foreach (synonyms_behavior_get('term_merge', 'taxonomy_term', $vocabulary->machine_name, TRUE) as $behavior_implementation) {
        $options[$behavior_implementation['provider']] = $behavior_implementation['label'];
      }
    }
    else {

      // This is how we used to retrieve possible synonyms field prior to
      // Synonyms 7.x-1.5. TODO: this should be removed at some point.
      foreach (synonyms_synonyms_fields($vocabulary) as $field_name) {
        $options[$field_name] = $instances[$field_name]['label'];
      }
    }
    $form['synonyms'] = array(
      '#type' => 'radios',
      '#title' => t('Add as Synonyms'),
      '#description' => t('Synonyms module allows you to add branch terms as synonyms into any of fields, enabled as sources of synonyms in vocabulary. Check the field into which you would like to add branch terms as synonym. <b>Important note:</b> the values will be added until the cardinality limit for the selected field is reached.'),
      '#options' => $options,
      '#default_value' => '',
    );
  }
  else {
    $form['synonyms'] = array(
      '#markup' => t('Enable the module ' . l('Synonyms', 'http://drupal.org/project/synonyms') . ' if you want to be able to add branch terms as synonyms into a field of your trunk term.'),
    );
  }
  $form['step'] = array(
    '#type' => 'textfield',
    '#title' => t('Step'),
    '#description' => t('Please, specify how many terms to process per script run in batch. If you are hitting time or memory limits in your PHP, decrease this number.'),
    '#default_value' => 40,
    '#required' => TRUE,
    '#element_validate' => array(
      'element_validate_integer_positive',
    ),
  );
  return $form;
}