You are here

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

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

AJAX callback handler for the term data form.

File

src/Form/TaxonomyManagerForm.php, line 360

Class

TaxonomyManagerForm
Taxonomy manager class.

Namespace

Drupal\taxonomy_manager\Form

Code

public function termDataCallback($form, FormStateInterface $form_state) {
  $taxonomy_term = $this->taxonomyTypeManager
    ->load($form_state
    ->getValue('load-term-data'));
  $term_form = $this->entityFormBuilder
    ->getForm($taxonomy_term, 'default');

  // Move the term data form into a fieldset.
  $term_form['fieldset']['#type'] = 'fieldset';
  $term_form['fieldset']['#title'] = Html::escape($taxonomy_term
    ->getName()) . ' (' . $taxonomy_term
    ->id() . ')';
  $term_form['fieldset']['#attributes'] = [];
  foreach (Element::children($term_form) as $key) {
    if ($key != 'fieldset') {
      $term_form['fieldset'][$key] = $term_form[$key];
      unset($term_form[$key]);
    }
  }
  $term_form['#prefix'] = '<div id="taxonomy-term-data-form">';
  $term_form['#suffix'] = '</div>';
  $current_path = $this->currentPath
    ->getPath();

  // Change the form action url form the current site to the add form.
  $term_form['#action'] = $this->urlGenerator
    ->generateFromRoute('entity.taxonomy_term.edit_form', [
    'taxonomy_term' => $taxonomy_term
      ->id(),
  ], [
    'query' => [
      'destination' => $current_path,
    ],
  ]);
  $response = new AjaxResponse();
  $response
    ->addCommand(new ReplaceCommand('#taxonomy-term-data-form', $term_form));
  return $response;
}