public function TermFormController::form

Overrides Drupal\Core\Entity\EntityFormController::form().

Overrides EntityFormController::form

File

drupal/core/modules/taxonomy/lib/Drupal/taxonomy/TermFormController.php, line 21
Definition of Drupal\taxonomy\TermFormController.

Class

TermFormController
Base for controller for taxonomy term edit forms.

Namespace

Drupal\taxonomy

Code

public function form(array $form, array &$form_state, EntityInterface $term) {
  $vocabulary = taxonomy_vocabulary_load($term->vid);
  $parent = array_keys(taxonomy_term_load_parents($term->tid));
  $form_state['taxonomy']['parent'] = $parent;
  $form_state['taxonomy']['vocabulary'] = $vocabulary;
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
    '#default_value' => $term->name,
    '#maxlength' => 255,
    '#required' => TRUE,
    '#weight' => -5,
  );
  $form['description'] = array(
    '#type' => 'text_format',
    '#title' => t('Description'),
    '#default_value' => $term->description,
    '#format' => $term->format,
    '#weight' => 0,
  );
  $language_configuration = module_invoke('language', 'get_default_configuration', 'taxonomy_term', $vocabulary->machine_name);
  $form['langcode'] = array(
    '#type' => 'language_select',
    '#title' => t('Language'),
    '#languages' => LANGUAGE_ALL,
    '#default_value' => $term->langcode,
    '#access' => !is_null($language_configuration['language_hidden']) && !$language_configuration['language_hidden'],
  );
  $form['vocabulary_machine_name'] = array(
    '#type' => 'value',
    '#value' => isset($term->vocabulary_machine_name) ? $term->vocabulary_machine_name : $vocabulary->name,
  );
  $form['relations'] = array(
    '#type' => 'details',
    '#title' => t('Relations'),
    '#collapsible' => TRUE,
    '#collapsed' => $vocabulary->hierarchy != TAXONOMY_HIERARCHY_MULTIPLE,
    '#weight' => 10,
  );

  // taxonomy_get_tree and taxonomy_term_load_parents may contain large numbers of
  // items so we check for taxonomy_override_selector before loading the
  // full vocabulary. Contrib modules can then intercept before
  // hook_form_alter to provide scalable alternatives.
  if (!variable_get('taxonomy_override_selector', FALSE)) {
    $parent = array_keys(taxonomy_term_load_parents($term->tid));
    $children = taxonomy_get_tree($vocabulary->vid, $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->vid);
    $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' => 'textfield',
    '#title' => t('Weight'),
    '#size' => 6,
    '#default_value' => $term->weight,
    '#description' => t('Terms are displayed in ascending order by weight.'),
    '#required' => TRUE,
  );
  $form['vid'] = array(
    '#type' => 'value',
    '#value' => $vocabulary->vid,
  );
  $form['tid'] = array(
    '#type' => 'value',
    '#value' => $term->tid,
  );
  if (empty($term->tid)) {
    $form_state['redirect'] = current_path();
  }
  return parent::form($form, $form_state, $term);
}