1 taxonomy.admin.inc taxonomy_form_term($form, &$form_state, TaxonomyTerm $term = NULL, TaxonomyVocabulary $vocabulary = NULL)

Form function for the term edit form.

Parameters

TaxonomyTerm|null $term: (optional) The taxonomy term entity to edit. If NULL or omitted, the form creates a new term.

TaxonomyVocabulary|null $vocabulary: (optional) A taxonomy vocabulary entity to create the term in. Required if the term is omitted.

See also

taxonomy_form_term_validate()

taxonomy_form_term_submit()

Related topics

File

core/modules/taxonomy/taxonomy.admin.inc, line 734
Administrative page callbacks for the taxonomy module.

Code

function taxonomy_form_term($form, &$form_state, TaxonomyTerm $term = NULL, TaxonomyVocabulary $vocabulary = NULL) {
  // During initial form build, add the term entity to the form state for use
  // during form building and processing. During a rebuild, use what is in the
  // form state.
  if (!isset($form_state['term'])) {
    // Create a new TaxonomyTerm entity for the add form.
    if (!isset($term)) {
      backdrop_set_title(t('Add term'));
      $term = entity_create('taxonomy_term', array(
        'vocabulary' => $vocabulary->machine_name,
      ));
    }
    else {
      backdrop_set_title(t('Edit term %term_name', array('%term_name' => $term->name)), PASS_THROUGH);
    }
    if (!isset($vocabulary) && isset($term->vocabulary)) {
      $vocabulary = taxonomy_vocabulary_load($term->vocabulary);
    }
    $form_state['term'] = $term;
  }
  else {
    $term = $form_state['term'];
    if (!isset($vocabulary) && isset($term->vocabulary)) {
      $vocabulary = taxonomy_vocabulary_load($term->vocabulary);
    }
  }

  $parent = array_keys(taxonomy_term_load_parents($term->tid));
  $form['#term'] = (array) $term;
  $form['#term']['parent'] = $parent;
  $form['#vocabulary'] = $vocabulary;

  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
    '#default_value' => $term->name,
    '#maxlength' => 255,
    '#required' => TRUE,
    '#weight' => -5,
    '#attributes' => array(
      'autofocus' => TRUE,
    ),
  );
  if (module_exists('language') && $vocabulary->language == TAXONOMY_LANGUAGE_ENABLED) {
    $form['langcode'] = array(
      '#type' => 'select',
      '#title' => t('Language'),
      '#default_value' => isset($_GET['langcode_term']) ? $_GET['langcode_term'] : $term->langcode,
      '#options' => language_list(TRUE, TRUE),
      '#empty_value' => LANGUAGE_NONE,
      '#empty_option' => t('- All (always shown) -'),
      '#description' => t('Set a language for this term. The term will only be visible in that language.')
    );
  }
  else {
    // Maintain langcode settings if Language module is not present.
    $form['langcode'] = array(
      '#type' => 'value',
      '#value' => $term->langcode,
    );
  }

  $form['description'] = array(
    '#type' => 'text_format',
    '#title' => t('Description'),
    '#default_value' => $term->description,
    '#format' => $term->format,
    '#weight' => 0,
  );

  $form['vocabulary'] = array(
    '#type' => 'value',
    '#value' => isset($term->vocabulary) ? $term->vocabulary : $vocabulary->machine_name,
  );

  $form['additional_settings'] = array(
    '#type' => 'vertical_tabs',
    '#attached' => array(
      'js' => array(backdrop_get_path('module', 'taxonomy') . '/js/taxonomy.term.js'),
    ),
    '#weight' => 99,
  );

  field_attach_form('taxonomy_term', $term, $form, $form_state);

  $form['relations'] = array(
    '#type' => 'fieldset',
    '#title' => t('Relations'),
    '#collapsible' => TRUE,
    '#collapsed' => ($vocabulary->hierarchy != TAXONOMY_HIERARCHY_MULTIPLE),
    '#weight' => -10,
    '#group' => 'additional_settings',
  );

  $parent = array_keys(taxonomy_term_load_parents($term->tid));
  $children = taxonomy_get_tree($vocabulary->machine_name, $term->tid);

  // A term can't be the child of itself, nor of its children.
  foreach ($children as $child) {
    $exclude[] = $child->tid;
  }
  $exclude[] = $term->tid;

  $tree = taxonomy_get_tree($vocabulary->machine_name);
  $options = array('<' . t('root') . '>');
  if (empty($parent)) {
    $parent = array(0);
  }
  foreach ($tree as $item) {
    if (!in_array($item->tid, $exclude)) {
      $options[$item->tid] = str_repeat('-', $item->depth) . $item->name;
    }
  }
  $form['relations']['parent'] = array(
    '#type' => 'select',
    '#title' => t('Parent terms'),
    '#options' => $options,
    '#default_value' => $parent,
    '#multiple' => TRUE,
  );

  $form['relations']['weight'] = array(
    '#type' => 'number',
    '#title' => t('Weight'),
    '#default_value' => $term->weight,
    '#description' => t('Terms are displayed in ascending order by weight.'),
    '#required' => TRUE,
  );

  // Prepare cancel link.
  if (isset($_GET['destination'])) {
    $path = $_GET['destination'];
  }
  elseif (isset($_SERVER['HTTP_REFERER'])) {
    $path = $_SERVER['HTTP_REFERER'];
  }
  elseif (user_access('administer taxonomy')) {
    $path = 'admin/structure/taxonomy/' . $vocabulary->machine_name;
  }
  elseif (isset($term->tid)) {
    $path = 'taxonomy/term/' . $term->tid;
  }
  else {
    $path = '<front>';
  }
  $options = backdrop_parse_url($path);
  $options['attributes']['class'][] = 'form-cancel';

  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
    '#weight' => 5,
  );

  if ($term->tid) {
    $form['actions']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete'),
      '#access' => taxonomy_term_access('delete', $term),
      '#weight' => 10,
      '#submit' => array('taxonomy_form_term_delete_submit'),
    );
  }
  else {
    $form_state['redirect'] = $_GET['q'];
  }

  $form['actions']['cancel'] = array(
    '#type' => 'link',
    '#title' => t('Cancel'),
    '#href' => $options['path'],
    '#options' => $options,
    '#weight' => 15,
  );

  return $form;
}