public function TermFormController::form

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

Overrides EntityFormControllerNG::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) {
  $term = $this->entity;
  $vocabulary = taxonomy_vocabulary_load($term
    ->bundle());
  $parent = array_keys(taxonomy_term_load_parents($term
    ->id()));
  $form_state['taxonomy']['parent'] = $parent;
  $form_state['taxonomy']['vocabulary'] = $vocabulary;
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
    '#default_value' => $term->name->value,
    '#maxlength' => 255,
    '#required' => TRUE,
    '#weight' => -5,
  );
  $form['description'] = array(
    '#type' => 'text_format',
    '#title' => t('Description'),
    '#default_value' => $term->description->value,
    '#format' => $term->format->value,
    '#weight' => 0,
  );
  $language_configuration = module_invoke('language', 'get_default_configuration', 'taxonomy_term', $vocabulary
    ->id());
  $form['langcode'] = array(
    '#type' => 'language_select',
    '#title' => t('Language'),
    '#languages' => Language::STATE_ALL,
    '#default_value' => $term->langcode->value,
    '#access' => !is_null($language_configuration['language_show']) && $language_configuration['language_show'],
  );
  $form['relations'] = array(
    '#type' => 'details',
    '#title' => t('Relations'),
    '#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.settings:override_selector
  // before loading the full vocabulary. Contrib modules can then intercept
  // before hook_form_alter to provide scalable alternatives.
  if (!config('taxonomy.settings')
    ->get('override_selector')) {
    $parent = array_keys(taxonomy_term_load_parents($term
      ->id()));
    $children = taxonomy_get_tree($vocabulary
      ->id(), $term
      ->id());

    // A term can't be the child of itself, nor of its children.
    foreach ($children as $child) {
      $exclude[] = $child->tid;
    }
    $exclude[] = $term
      ->id();
    $tree = taxonomy_get_tree($vocabulary
      ->id());
    $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->value,
    '#description' => t('Terms are displayed in ascending order by weight.'),
    '#required' => TRUE,
  );
  $form['vid'] = array(
    '#type' => 'value',
    '#value' => $vocabulary
      ->id(),
  );
  $form['tid'] = array(
    '#type' => 'value',
    '#value' => $term
      ->id(),
  );
  if ($term
    ->isNew()) {
    $form_state['redirect'] = current_path();
  }
  return parent::form($form, $form_state, $term);
}