summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2018-04-07 07:43:49 (GMT)
committerAlex Pott2018-04-07 07:43:49 (GMT)
commit9f61972e0ac6d30c64c6ab93f5cfe8029536f51a (patch)
treec6ea09b88c2bd25706abe2f155969bc32fd1c35d
parent33519ea3c75edb85199b56e5e5a3579f9bf5da49 (diff)
Issue #2896535 by timmillwood, tedbow: Create a helper trait for Forms in ajax dialogs
-rw-r--r--core/lib/Drupal/Core/Ajax/AjaxFormHelperTrait.php (renamed from core/modules/layout_builder/src/Form/AjaxFormHelperTrait.php)7
-rw-r--r--core/lib/Drupal/Core/Ajax/AjaxHelperTrait.php (renamed from core/modules/layout_builder/src/Controller/AjaxHelperTrait.php)16
-rw-r--r--core/modules/layout_builder/src/Controller/AddSectionController.php1
-rw-r--r--core/modules/layout_builder/src/Controller/ChooseBlockController.php1
-rw-r--r--core/modules/layout_builder/src/Controller/ChooseSectionController.php1
-rw-r--r--core/modules/layout_builder/src/Form/ConfigureBlockFormBase.php1
-rw-r--r--core/modules/layout_builder/src/Form/ConfigureSectionForm.php1
-rw-r--r--core/modules/layout_builder/src/Form/LayoutRebuildConfirmFormBase.php1
-rw-r--r--core/modules/settings_tray/src/Block/BlockEntitySettingTrayForm.php55
9 files changed, 32 insertions, 52 deletions
diff --git a/core/modules/layout_builder/src/Form/AjaxFormHelperTrait.php b/core/lib/Drupal/Core/Ajax/AjaxFormHelperTrait.php
index a5a3870..49932df 100644
--- a/core/modules/layout_builder/src/Form/AjaxFormHelperTrait.php
+++ b/core/lib/Drupal/Core/Ajax/AjaxFormHelperTrait.php
@@ -1,18 +1,13 @@
<?php
-namespace Drupal\layout_builder\Form;
+namespace Drupal\Core\Ajax;
-use Drupal\Core\Ajax\AjaxResponse;
-use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Form\FormStateInterface;
-use Drupal\layout_builder\Controller\AjaxHelperTrait;
/**
* Provides a helper to for submitting an AJAX form.
*
* @internal
- *
- * @todo Move to \Drupal\Core in https://www.drupal.org/node/2896535.
*/
trait AjaxFormHelperTrait {
diff --git a/core/modules/layout_builder/src/Controller/AjaxHelperTrait.php b/core/lib/Drupal/Core/Ajax/AjaxHelperTrait.php
index 072ecca..287abae 100644
--- a/core/modules/layout_builder/src/Controller/AjaxHelperTrait.php
+++ b/core/lib/Drupal/Core/Ajax/AjaxHelperTrait.php
@@ -1,6 +1,6 @@
<?php
-namespace Drupal\layout_builder\Controller;
+namespace Drupal\Core\Ajax;
use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
@@ -8,8 +8,6 @@ use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
* Provides a helper to determine if the current request is via AJAX.
*
* @internal
- *
- * @todo Move to \Drupal\Core in https://www.drupal.org/node/2896535.
*/
trait AjaxHelperTrait {
@@ -20,12 +18,12 @@ trait AjaxHelperTrait {
* TRUE if the current request is via AJAX, FALSE otherwise.
*/
protected function isAjax() {
- return in_array($this->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 9478959..0ea4237 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 5287be2..85ea7d8 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 a04cdbb..4884808 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 e1103e9..ccef122 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 2bfcdca..94c2a4e 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 511a119..2a44a29 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 fd44b0c..243e907 100644
--- a/core/modules/settings_tray/src/Block/BlockEntitySettingTrayForm.php
+++ b/core/modules/settings_tray/src/Block/BlockEntitySettingTrayForm.php
@@ -5,9 +5,9 @@ namespace Drupal\settings_tray\Block;
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 @@ use Drupal\Core\Url;
*/
class BlockEntitySettingTrayForm extends BlockForm {
+ use AjaxFormHelperTrait;
+
/**
* Provides a title callback to get the block's admin label.
*
@@ -122,17 +124,17 @@ class BlockEntitySettingTrayForm extends BlockForm {
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 @@ class BlockEntitySettingTrayForm extends BlockForm {
}
/**
- * 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);
}