You are here

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

Same name and namespace in other branches
  1. 8 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 156

Class

TaxonomyManagerForm
Taxonomy manager class.

Namespace

Drupal\taxonomy_manager\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, VocabularyInterface $taxonomy_vocabulary = NULL) {
  $form['voc'] = [
    '#type' => 'value',
    "#value" => $taxonomy_vocabulary,
  ];
  $form['#attached']['library'][] = 'taxonomy_manager/form';
  if ($this->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',
    ],
  ];
  $form['toolbar']['miniexport'] = [
    '#type' => 'submit',
    '#name' => 'export',
    '#value' => $this
      ->t('Export all'),
    '#ajax' => [
      'callback' => '::exportListFormCallback',
    ],
  ];

  /* Vocabulary switcher */
  $vocabularies = \Drupal::entityTypeManager()
    ->getStorage('taxonomy_vocabulary')
    ->loadMultiple();
  foreach ($vocabularies as $voc) {
    $voc_list[$voc
      ->id()] = $voc
      ->label();
  }
  $current_path = \Drupal::service('path.current')
    ->getPath();
  $url_parts = explode('/', $current_path);
  $voc_id = end($url_parts);
  $form['toolbar']['vocabulary_switcher'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Vocabulary switcher'),
    '#options' => $voc_list,
    '#attributes' => [
      'onchange' => "form.submit('taxonomy-manager-vocabulary-terms-form')",
    ],
    '#default_value' => $voc_id,
  ];

  /* Autocomplete function redirecting to taxonomy term details page */
  $form['toolbar']['search_terms'] = [
    '#title' => $this
      ->t('Search all terms in this vocabulary'),
    '#type' => 'entity_autocomplete',
    '#target_type' => 'taxonomy_term',
    '#selection_settings' => [
      'target_bundles' => [
        $voc_id,
      ],
    ],
  ];
  $form['toolbar']['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Submit'),
    '#submit' => [
      '::taxonomy_term_submit_handler',
    ],
    '#attributes' => [
      'class' => [
        'taxonomy-manager-hidden-button',
      ],
    ],
  ];

  /* 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;
}