You are here

public function TaxonomyManagerForm::buildForm in Taxonomy Manager 8

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

Form constructor.

Display a tree of all the terms in a vocabulary, with options to edit each one. The form implements the Taxonomy Manager intefrace.

Parameters

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

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

\Drupal\taxonomy\VocabularyInterface $taxonomy_vocabulary: The vocabulary being with worked with.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/TaxonomyManagerForm.php, line 155

Class

TaxonomyManagerForm
Taxonomy manager class.

Namespace

Drupal\taxonomy_manager\Form

Code

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

  // Advagg clash warning.
  if ($this->moduleHandler
    ->moduleExists('advagg')) {
    $this
      ->messenger()
      ->addWarning($this
      ->t('<em>Advanced CSS/JS Aggregation</em> module is enabled. Make sure that <em>%settings_link</em> setting is switched off.', [
      '%settings_link' => $this->linkGenerator
        ->generate($this
        ->t('Move all external scripts to the top of the execution order'), new Url('advagg_mod.settings')),
    ]));
  }

  // Build the form.
  $form['voc'] = [
    '#type' => 'value',
    "#value" => $taxonomy_vocabulary,
  ];
  $form['#attached']['library'][] = 'taxonomy_manager/form';
  if (TaxonomyManagerHelper::vocabularyIsEmpty($taxonomy_vocabulary
    ->id())) {
    $form['text'] = [
      '#markup' => $this
        ->t('No terms available'),
    ];
    $form[] = $this->formBuilder
      ->getForm('Drupal\\taxonomy_manager\\Form\\AddTermsToVocabularyForm', $taxonomy_vocabulary);
    return $form;
  }
  $form['toolbar'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Toolbar'),
  ];
  $form['toolbar']['add'] = [
    '#type' => 'submit',
    '#name' => 'add',
    '#value' => $this
      ->t('Add'),
    '#ajax' => [
      'callback' => '::addFormCallback',
    ],
  ];
  $form['toolbar']['delete'] = [
    '#type' => 'submit',
    '#name' => 'delete',
    '#value' => $this
      ->t('Delete'),
    '#attributes' => [
      'disabled' => TRUE,
    ],
    '#ajax' => [
      'callback' => '::deleteFormCallback',
    ],
  ];
  $form['toolbar']['move'] = [
    '#type' => 'submit',
    '#name' => 'move',
    '#value' => $this
      ->t('Move'),
    '#ajax' => [
      'callback' => '::moveFormCallback',
    ],
  ];
  $form['toolbar']['export'] = [
    '#type' => 'submit',
    '#name' => 'export',
    '#value' => $this
      ->t('Export'),
    '#ajax' => [
      'callback' => '::exportFormCallback',
    ],
  ];

  /* Taxonomy manager. */
  $form['taxonomy']['#tree'] = TRUE;
  $form['taxonomy']['manager'] = [
    '#type' => 'fieldset',
    '#title' => Html::escape($taxonomy_vocabulary
      ->label()),
    '#tree' => TRUE,
  ];
  $form['taxonomy']['manager']['top'] = [
    '#markup' => '',
    '#prefix' => '<div class="taxonomy-manager-tree-top">',
    '#suffix' => '</div>',
  ];
  $form['taxonomy']['manager']['tree'] = [
    '#type' => 'taxonomy_manager_tree',
    '#vocabulary' => $taxonomy_vocabulary
      ->id(),
    '#pager_size' => $this->configFactory
      ->get('taxonomy_manager.settings')
      ->get('taxonomy_manager_pager_tree_page_size'),
  ];
  $form['taxonomy']['manager']['pager'] = [
    '#type' => 'pager',
  ];

  // Add placeholder for term data form, the load-term-data field has AJAX
  // events attached and will trigger the load of the term data form. The
  // field is hidden via CSS and the value gets set in termData.js.
  $form['term-data']['#prefix'] = '<div id="taxonomy-term-data-form">';
  $form['term-data']['#suffix'] = '</div>';
  $form['load-term-data'] = [
    '#type' => 'textfield',
    '#ajax' => [
      'callback' => '::termDataCallback',
      'event' => 'change',
    ],
  ];
  return $form;
}