diff --git a/core/lib/Drupal/Core/Ajax/AjaxFormHelperTrait.php b/core/lib/Drupal/Core/Ajax/AjaxFormHelperTrait.php index 9f6e695b80..49932df548 100644 --- a/core/lib/Drupal/Core/Ajax/AjaxFormHelperTrait.php +++ b/core/lib/Drupal/Core/Ajax/AjaxFormHelperTrait.php @@ -1,18 +1,13 @@ '::submitFormDialog', + 'callback' => '::ajaxSubmit', ]; $form['#attached']['library'][] = 'core/drupal.dialog.ajax'; - // static::submitFormDialog() requires data-drupal-selector to be the same - // between the various Ajax requests. A bug in + // static::ajaxSubmit() requires data-drupal-selector to be the same between + // the various Ajax requests. A bug in // \Drupal\Core\Form\FormBuilder prevents that from happening unless // $form['#id'] is also the same. Normally, #id is set to a unique HTML ID // via Html::getUniqueId(), but here we bypass that in order to work around @@ -141,38 +144,17 @@ public function buildForm(array $form, FormStateInterface $form_state) { } /** - * Submit form dialog #ajax callback. - * - * @param array $form - * An associative array containing the structure of the form. - * @param \Drupal\Core\Form\FormStateInterface $form_state - * The current state of the form. - * - * @return \Drupal\Core\Ajax\AjaxResponse - * An AJAX response that display validation error messages or redirects - * to a URL - * - * @todo Repalce this callback with generic trait in - * https://www.drupal.org/node/2896535. + * {@inheritdoc} */ - public function submitFormDialog(array &$form, FormStateInterface $form_state) { - $response = new AjaxResponse(); - if ($form_state->hasAnyErrors()) { - $form['status_messages'] = [ - '#type' => 'status_messages', - '#weight' => -1000, - ]; - $command = new ReplaceCommand('[data-drupal-selector="' . $form['#attributes']['data-drupal-selector'] . '"]', $form); + protected function successfulAjaxSubmit(array $form, FormStateInterface $form_state) { + if ($redirect_url = $this->getRedirectUrl()) { + $command = new RedirectCommand($redirect_url->setAbsolute()->toString()); } else { - if ($redirect_url = $this->getRedirectUrl()) { - $command = new RedirectCommand($redirect_url->setAbsolute()->toString()); - } - else { - // Settings Tray always provides a destination. - throw new \Exception("No destination provided by Settings Tray form"); - } + // Settings Tray always provides a destination. + throw new \Exception("No destination provided by Settings Tray form"); } + $response = new AjaxResponse(); return $response->addCommand($command); }