You are here

public function AddTermsToVocabularyForm::buildForm in Taxonomy Manager 2.0.x

Same name and namespace in other branches
  1. 8 src/Form/AddTermsToVocabularyForm.php \Drupal\taxonomy_manager\Form\AddTermsToVocabularyForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/AddTermsToVocabularyForm.php, line 52

Class

AddTermsToVocabularyForm
Form for adding terms to a given vocabulary.

Namespace

Drupal\taxonomy_manager\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, VocabularyInterface $taxonomy_vocabulary = NULL, $parents = []) {

  // Cache form state so that we keep the parents in the modal dialog.
  // For non modals (non POST request), form state caching on is not allowed.
  // @see FormState::setCached()
  if ($this
    ->getRequest()
    ->getMethod() == 'POST') {
    $form_state
      ->setCached(TRUE);
  }
  $form['voc'] = [
    '#type' => 'value',
    '#value' => $taxonomy_vocabulary,
  ];
  $form['parents']['#tree'] = TRUE;
  foreach ($parents as $p) {
    $form['parents'][$p] = [
      '#type' => 'value',
      '#value' => $p,
    ];
  }
  $description = $this
    ->t("If you have selected one or more terms in the tree view, the new terms are automatically children of those.");
  $form['help'] = [
    '#markup' => $description,
  ];
  $form['mass_add'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Terms'),
    '#description' => $this
      ->t("One term per line. Child terms can be prefixed with a\n        dash '-' (one dash per hierarchy level). Terms that should not become\n        child terms and start with a dash need to be wrapped in double quotes.\n        <br />Example:<br />\n        animals<br />\n        -canine<br />\n        --dog<br />\n        --wolf<br />\n        -feline<br />\n        --cat"),
    '#rows' => 10,
    '#required' => TRUE,
  ];
  $form['add'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Add'),
    '#attributes' => array(
      'onclick' => 'javascript:var s=this;setTimeout(function(){s.value="Saving...";s.disabled=true;},1);',
    ),
  ];
  return $form;
}