You are here

public function VocabularyForm::form in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/taxonomy/src/VocabularyForm.php \Drupal\taxonomy\VocabularyForm::form()

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

core/modules/taxonomy/src/VocabularyForm.php, line 51
Contains \Drupal\taxonomy\VocabularyForm.

Class

VocabularyForm
Base form for vocabulary edit forms.

Namespace

Drupal\taxonomy

Code

public function form(array $form, FormStateInterface $form_state) {
  $vocabulary = $this->entity;
  if ($vocabulary
    ->isNew()) {
    $form['#title'] = $this
      ->t('Add vocabulary');
  }
  else {
    $form['#title'] = $this
      ->t('Edit vocabulary');
  }
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('Name'),
    '#default_value' => $vocabulary
      ->label(),
    '#maxlength' => 255,
    '#required' => TRUE,
  );
  $form['vid'] = array(
    '#type' => 'machine_name',
    '#default_value' => $vocabulary
      ->id(),
    '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
    '#machine_name' => array(
      'exists' => array(
        $this,
        'exists',
      ),
      'source' => array(
        'name',
      ),
    ),
  );
  $form['description'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('Description'),
    '#default_value' => $vocabulary
      ->getDescription(),
  );

  // $form['langcode'] is not wrapped in an
  // if ($this->moduleHandler->moduleExists('language')) check because the
  // language_select form element works also without the language module being
  // installed. https://www.drupal.org/node/1749954 documents the new element.
  $form['langcode'] = array(
    '#type' => 'language_select',
    '#title' => $this
      ->t('Vocabulary language'),
    '#languages' => LanguageInterface::STATE_ALL,
    '#default_value' => $vocabulary
      ->language()
      ->getId(),
  );
  if ($this->moduleHandler
    ->moduleExists('language')) {
    $form['default_terms_language'] = array(
      '#type' => 'details',
      '#title' => $this
        ->t('Terms language'),
      '#open' => TRUE,
    );
    $form['default_terms_language']['default_language'] = array(
      '#type' => 'language_configuration',
      '#entity_information' => array(
        'entity_type' => 'taxonomy_term',
        'bundle' => $vocabulary
          ->id(),
      ),
      '#default_value' => ContentLanguageSettings::loadByEntityTypeBundle('taxonomy_term', $vocabulary
        ->id()),
    );
  }

  // Set the hierarchy to "multiple parents" by default. This simplifies the
  // vocabulary form and standardizes the term form.
  $form['hierarchy'] = array(
    '#type' => 'value',
    '#value' => '0',
  );
  $form = parent::form($form, $form_state);
  return $this
    ->protectBundleIdElement($form);
}