diff --git a/core/modules/layout_builder/src/Form/AjaxFormHelperTrait.php b/core/lib/Drupal/Core/Ajax/AjaxFormHelperTrait.php similarity index 86% rename from core/modules/layout_builder/src/Form/AjaxFormHelperTrait.php rename to core/lib/Drupal/Core/Ajax/AjaxFormHelperTrait.php index a5a387020f9d6c383295ca037a4da8380f1d05e3..49932df548acb90ed7f9f315a475207fd18f560e 100644 --- a/core/modules/layout_builder/src/Form/AjaxFormHelperTrait.php +++ b/core/lib/Drupal/Core/Ajax/AjaxFormHelperTrait.php @@ -1,18 +1,13 @@ getRequestWrapperFormat(), [ - 'drupal_ajax', - 'drupal_dialog', - 'drupal_dialog.off_canvas', - 'drupal_modal', - ]); + foreach (['drupal_ajax', 'drupal_modal', 'drupal_dialog'] as $wrapper) { + if (strpos($this->getRequestWrapperFormat(), $wrapper) !== FALSE) { + return TRUE; + } + } + return FALSE; } /** diff --git a/core/modules/layout_builder/src/Controller/AddSectionController.php b/core/modules/layout_builder/src/Controller/AddSectionController.php index 947895985cf16bafd67c413bd81c4fcedb29a619..0ea42379a6be523a15adc5c459ff25f3e42fbc34 100644 --- a/core/modules/layout_builder/src/Controller/AddSectionController.php +++ b/core/modules/layout_builder/src/Controller/AddSectionController.php @@ -2,6 +2,7 @@ namespace Drupal\layout_builder\Controller; +use Drupal\Core\Ajax\AjaxHelperTrait; use Drupal\Core\DependencyInjection\ClassResolverInterface; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Drupal\layout_builder\LayoutTempstoreRepositoryInterface; diff --git a/core/modules/layout_builder/src/Controller/ChooseBlockController.php b/core/modules/layout_builder/src/Controller/ChooseBlockController.php index 5287be25fcff10b8908ebf93a5c9b3538bab84a2..85ea7d83709a8b6d840599a755d9045f953cb324 100644 --- a/core/modules/layout_builder/src/Controller/ChooseBlockController.php +++ b/core/modules/layout_builder/src/Controller/ChooseBlockController.php @@ -2,6 +2,7 @@ namespace Drupal\layout_builder\Controller; +use Drupal\Core\Ajax\AjaxHelperTrait; use Drupal\Core\Block\BlockManagerInterface; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Drupal\Core\Url; diff --git a/core/modules/layout_builder/src/Controller/ChooseSectionController.php b/core/modules/layout_builder/src/Controller/ChooseSectionController.php index a04cdbb1c1169baa827505005121aec1e2e12740..488480883d57bff665265a9e04c2a457758c6ac9 100644 --- a/core/modules/layout_builder/src/Controller/ChooseSectionController.php +++ b/core/modules/layout_builder/src/Controller/ChooseSectionController.php @@ -2,6 +2,7 @@ namespace Drupal\layout_builder\Controller; +use Drupal\Core\Ajax\AjaxHelperTrait; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Drupal\Core\Layout\LayoutPluginManagerInterface; use Drupal\Core\Plugin\PluginFormInterface; diff --git a/core/modules/layout_builder/src/Form/ConfigureBlockFormBase.php b/core/modules/layout_builder/src/Form/ConfigureBlockFormBase.php index e1103e9e44bbce830144475fad98074f789604c6..ccef12287db93c68aa79b6a1b1c740ca8f299255 100644 --- a/core/modules/layout_builder/src/Form/ConfigureBlockFormBase.php +++ b/core/modules/layout_builder/src/Form/ConfigureBlockFormBase.php @@ -3,6 +3,7 @@ namespace Drupal\layout_builder\Form; use Drupal\Component\Uuid\UuidInterface; +use Drupal\Core\Ajax\AjaxFormHelperTrait; use Drupal\Core\Block\BlockManagerInterface; use Drupal\Core\Block\BlockPluginInterface; use Drupal\Core\DependencyInjection\ClassResolverInterface; diff --git a/core/modules/layout_builder/src/Form/ConfigureSectionForm.php b/core/modules/layout_builder/src/Form/ConfigureSectionForm.php index 2bfcdca7c2df3501867f271ebc6bbef80a6c6598..94c2a4eb4c2b35d0b81d8dce2ad6af2bb2f7bd6c 100644 --- a/core/modules/layout_builder/src/Form/ConfigureSectionForm.php +++ b/core/modules/layout_builder/src/Form/ConfigureSectionForm.php @@ -2,6 +2,7 @@ namespace Drupal\layout_builder\Form; +use Drupal\Core\Ajax\AjaxFormHelperTrait; use Drupal\Core\DependencyInjection\ClassResolverInterface; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; diff --git a/core/modules/layout_builder/src/Form/LayoutRebuildConfirmFormBase.php b/core/modules/layout_builder/src/Form/LayoutRebuildConfirmFormBase.php index 511a11968e3e778726e8f805671d4afa6d005624..2a44a290d0a59aa4884c69fcdd244fc2f1194030 100644 --- a/core/modules/layout_builder/src/Form/LayoutRebuildConfirmFormBase.php +++ b/core/modules/layout_builder/src/Form/LayoutRebuildConfirmFormBase.php @@ -2,6 +2,7 @@ namespace Drupal\layout_builder\Form; +use Drupal\Core\Ajax\AjaxFormHelperTrait; use Drupal\Core\DependencyInjection\ClassResolverInterface; use Drupal\Core\Form\ConfirmFormBase; use Drupal\Core\Form\FormStateInterface; diff --git a/core/modules/settings_tray/src/Block/BlockEntitySettingTrayForm.php b/core/modules/settings_tray/src/Block/BlockEntitySettingTrayForm.php index fd44b0c86aa8546264a4f6c795df473e4902b0e2..243e907ccd3ffe0663c96235dfb4cab42cb9b25b 100644 --- a/core/modules/settings_tray/src/Block/BlockEntitySettingTrayForm.php +++ b/core/modules/settings_tray/src/Block/BlockEntitySettingTrayForm.php @@ -5,9 +5,9 @@ use Drupal\block\BlockForm; use Drupal\block\BlockInterface; use Drupal\Component\Utility\Html; +use Drupal\Core\Ajax\AjaxFormHelperTrait; use Drupal\Core\Ajax\AjaxResponse; use Drupal\Core\Ajax\RedirectCommand; -use Drupal\Core\Ajax\ReplaceCommand; use Drupal\Core\Block\BlockPluginInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\PluginWithFormsInterface; @@ -23,6 +23,8 @@ */ class BlockEntitySettingTrayForm extends BlockForm { + use AjaxFormHelperTrait; + /** * Provides a title callback to get the block's admin label. * @@ -122,17 +124,17 @@ protected function getPluginForm(BlockPluginInterface $block) { public function buildForm(array $form, FormStateInterface $form_state) { $form = parent::buildForm($form, $form_state); $form['actions']['submit']['#ajax'] = [ - 'callback' => '::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 - // \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 - // the data-drupal-selector bug. This is okay so long as we assume that this - // form only ever occurs once on a page. + // 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 the data-drupal-selector bug. + // This is okay so long as we assume that this form only ever occurs once on + // a page. // @todo Remove this workaround once https://www.drupal.org/node/2897377 is // fixed. $form['#id'] = Html::getId($form_state->getBuildInfo()['form_id']); @@ -141,38 +143,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); }