Unverified Commit 9f61972e authored by alexpott's avatar alexpott

Issue #2896535 by timmillwood, tedbow: Create a helper trait for Forms in ajax dialogs

parent 33519ea3
<?php <?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\Core\Form\FormStateInterface;
use Drupal\layout_builder\Controller\AjaxHelperTrait;
/** /**
* Provides a helper to for submitting an AJAX form. * Provides a helper to for submitting an AJAX form.
* *
* @internal * @internal
*
* @todo Move to \Drupal\Core in https://www.drupal.org/node/2896535.
*/ */
trait AjaxFormHelperTrait { trait AjaxFormHelperTrait {
......
<?php <?php
namespace Drupal\layout_builder\Controller; namespace Drupal\Core\Ajax;
use Drupal\Core\EventSubscriber\MainContentViewSubscriber; use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
...@@ -8,8 +8,6 @@ ...@@ -8,8 +8,6 @@
* Provides a helper to determine if the current request is via AJAX. * Provides a helper to determine if the current request is via AJAX.
* *
* @internal * @internal
*
* @todo Move to \Drupal\Core in https://www.drupal.org/node/2896535.
*/ */
trait AjaxHelperTrait { trait AjaxHelperTrait {
...@@ -20,12 +18,12 @@ trait AjaxHelperTrait { ...@@ -20,12 +18,12 @@ trait AjaxHelperTrait {
* TRUE if the current request is via AJAX, FALSE otherwise. * TRUE if the current request is via AJAX, FALSE otherwise.
*/ */
protected function isAjax() { protected function isAjax() {
return in_array($this->getRequestWrapperFormat(), [ foreach (['drupal_ajax', 'drupal_modal', 'drupal_dialog'] as $wrapper) {
'drupal_ajax', if (strpos($this->getRequestWrapperFormat(), $wrapper) !== FALSE) {
'drupal_dialog', return TRUE;
'drupal_dialog.off_canvas', }
'drupal_modal', }
]); return FALSE;
} }
/** /**
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace Drupal\layout_builder\Controller; namespace Drupal\layout_builder\Controller;
use Drupal\Core\Ajax\AjaxHelperTrait;
use Drupal\Core\DependencyInjection\ClassResolverInterface; use Drupal\Core\DependencyInjection\ClassResolverInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\layout_builder\LayoutTempstoreRepositoryInterface; use Drupal\layout_builder\LayoutTempstoreRepositoryInterface;
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace Drupal\layout_builder\Controller; namespace Drupal\layout_builder\Controller;
use Drupal\Core\Ajax\AjaxHelperTrait;
use Drupal\Core\Block\BlockManagerInterface; use Drupal\Core\Block\BlockManagerInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Url; use Drupal\Core\Url;
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace Drupal\layout_builder\Controller; namespace Drupal\layout_builder\Controller;
use Drupal\Core\Ajax\AjaxHelperTrait;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Layout\LayoutPluginManagerInterface; use Drupal\Core\Layout\LayoutPluginManagerInterface;
use Drupal\Core\Plugin\PluginFormInterface; use Drupal\Core\Plugin\PluginFormInterface;
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace Drupal\layout_builder\Form; namespace Drupal\layout_builder\Form;
use Drupal\Component\Uuid\UuidInterface; use Drupal\Component\Uuid\UuidInterface;
use Drupal\Core\Ajax\AjaxFormHelperTrait;
use Drupal\Core\Block\BlockManagerInterface; use Drupal\Core\Block\BlockManagerInterface;
use Drupal\Core\Block\BlockPluginInterface; use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\DependencyInjection\ClassResolverInterface; use Drupal\Core\DependencyInjection\ClassResolverInterface;
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace Drupal\layout_builder\Form; namespace Drupal\layout_builder\Form;
use Drupal\Core\Ajax\AjaxFormHelperTrait;
use Drupal\Core\DependencyInjection\ClassResolverInterface; use Drupal\Core\DependencyInjection\ClassResolverInterface;
use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace Drupal\layout_builder\Form; namespace Drupal\layout_builder\Form;
use Drupal\Core\Ajax\AjaxFormHelperTrait;
use Drupal\Core\DependencyInjection\ClassResolverInterface; use Drupal\Core\DependencyInjection\ClassResolverInterface;
use Drupal\Core\Form\ConfirmFormBase; use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
......
...@@ -5,9 +5,9 @@ ...@@ -5,9 +5,9 @@
use Drupal\block\BlockForm; use Drupal\block\BlockForm;
use Drupal\block\BlockInterface; use Drupal\block\BlockInterface;
use Drupal\Component\Utility\Html; use Drupal\Component\Utility\Html;
use Drupal\Core\Ajax\AjaxFormHelperTrait;
use Drupal\Core\Ajax\AjaxResponse; use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\RedirectCommand; use Drupal\Core\Ajax\RedirectCommand;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Block\BlockPluginInterface; use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginWithFormsInterface; use Drupal\Core\Plugin\PluginWithFormsInterface;
...@@ -23,6 +23,8 @@ ...@@ -23,6 +23,8 @@
*/ */
class BlockEntitySettingTrayForm extends BlockForm { class BlockEntitySettingTrayForm extends BlockForm {
use AjaxFormHelperTrait;
/** /**
* Provides a title callback to get the block's admin label. * Provides a title callback to get the block's admin label.
* *
...@@ -122,17 +124,17 @@ protected function getPluginForm(BlockPluginInterface $block) { ...@@ -122,17 +124,17 @@ protected function getPluginForm(BlockPluginInterface $block) {
public function buildForm(array $form, FormStateInterface $form_state) { public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state); $form = parent::buildForm($form, $form_state);
$form['actions']['submit']['#ajax'] = [ $form['actions']['submit']['#ajax'] = [
'callback' => '::submitFormDialog', 'callback' => '::ajaxSubmit',
]; ];
$form['#attached']['library'][] = 'core/drupal.dialog.ajax'; $form['#attached']['library'][] = 'core/drupal.dialog.ajax';
// static::submitFormDialog() requires data-drupal-selector to be the same // static::ajaxSubmit() requires data-drupal-selector to be the same between
// between the various Ajax requests. A bug in // the various Ajax requests. A bug in \Drupal\Core\Form\FormBuilder
// \Drupal\Core\Form\FormBuilder prevents that from happening unless // prevents that from happening unless $form['#id'] is also the same.
// $form['#id'] is also the same. Normally, #id is set to a unique HTML ID // Normally, #id is set to a unique HTML ID via Html::getUniqueId(), but
// via Html::getUniqueId(), but here we bypass that in order to work around // here we bypass that in order to work around the data-drupal-selector bug.
// the data-drupal-selector bug. This is okay so long as we assume that this // This is okay so long as we assume that this form only ever occurs once on
// form only ever occurs once on a page. // a page.
// @todo Remove this workaround once https://www.drupal.org/node/2897377 is // @todo Remove this workaround once https://www.drupal.org/node/2897377 is
// fixed. // fixed.
$form['#id'] = Html::getId($form_state->getBuildInfo()['form_id']); $form['#id'] = Html::getId($form_state->getBuildInfo()['form_id']);
...@@ -141,38 +143,17 @@ public function buildForm(array $form, FormStateInterface $form_state) { ...@@ -141,38 +143,17 @@ public function buildForm(array $form, FormStateInterface $form_state) {
} }
/** /**
* Submit form dialog #ajax callback. * {@inheritdoc}
*
* @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.
*/ */
public function submitFormDialog(array &$form, FormStateInterface $form_state) { protected function successfulAjaxSubmit(array $form, FormStateInterface $form_state) {
$response = new AjaxResponse(); if ($redirect_url = $this->getRedirectUrl()) {
if ($form_state->hasAnyErrors()) { $command = new RedirectCommand($redirect_url->setAbsolute()->toString());
$form['status_messages'] = [
'#type' => 'status_messages',
'#weight' => -1000,
];
$command = new ReplaceCommand('[data-drupal-selector="' . $form['#attributes']['data-drupal-selector'] . '"]', $form);
} }
else { else {
if ($redirect_url = $this->getRedirectUrl()) { // Settings Tray always provides a destination.
$command = new RedirectCommand($redirect_url->setAbsolute()->toString()); throw new \Exception("No destination provided by Settings Tray form");
}
else {
// Settings Tray always provides a destination.
throw new \Exception("No destination provided by Settings Tray form");
}
} }
$response = new AjaxResponse();
return $response->addCommand($command); return $response->addCommand($command);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment