diff --git a/core/modules/outside_in/src/OffCanvasFormDialogTrait.php b/core/lib/Drupal/Core/Form/DialogFormTrait.php similarity index 81% rename from core/modules/outside_in/src/OffCanvasFormDialogTrait.php rename to core/lib/Drupal/Core/Form/DialogFormTrait.php index 62a76ba652..145a34ecfa 100644 --- a/core/modules/outside_in/src/OffCanvasFormDialogTrait.php +++ b/core/lib/Drupal/Core/Form/DialogFormTrait.php @@ -1,48 +1,29 @@ getRequest() - ->get(MainContentViewSubscriber::WRAPPER_FORMAT); - return in_array($wrapper_format, [ - 'drupal_ajax', - 'drupal_modal', - 'drupal_dialog.off_canvas', - ]); - } - - /** - * Adds modal dialog support to a form. + * Adds dialog support to a form. * * @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. */ - protected function buildFormDialog(array &$form, FormStateInterface $form_state) { - if (!$this->isModalDialog()) { + protected function buildFormDialog(array &$form, FormStateInterface $form_state, $create_cancel = FALSE) { + if (!$this->isDialog()) { return; } @@ -56,29 +37,64 @@ protected function buildFormDialog(array &$form, FormStateInterface $form_state) $ajax_callback_added = TRUE; } - if (!empty($form['actions']['cancel'])) { - // Replace 'Cancel' link button with a close dialog button. + if ($create_cancel) { $form['actions']['cancel'] = [ '#type' => 'submit', '#value' => $this->t('Cancel'), + '#weight' => 100, + ]; + } + if (!empty($form['actions']['cancel'])) { + // Replace 'Cancel' link button with a close dialog button. + $form['actions']['cancel'] = [ '#submit' => ['::noSubmit'], '#limit_validation_errors' => [], - '#weight' => 100, '#ajax' => [ 'callback' => '::closeDialog', 'event' => 'click', ], - ]; + ] + $form['actions']['cancel']; $ajax_callback_added = TRUE; } if ($ajax_callback_added) { $form['#attached']['library'][] = 'core/drupal.dialog.ajax'; - $form['#attributes']['id'] = 'off-canvas-form'; + $form['#attributes']['id'] = 'dialog-form'; } } /** + * Empty submit #ajax submit callback. + * + * This allows modal dialog to using ::submitCallback to validate and submit + * the form via one ajax required. + * + * @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. + */ + public function noSubmit(array &$form, FormStateInterface $form_state) { + } + + + + /** + * Determines if the current request is for an AJAX dialog. + * + * @return bool + * TRUE is the current request if for an AJAX dialog. + */ + protected function isDialog() { + return in_array($this->getRequestWrapperFormat(), [ + 'drupal_ajax', + 'drupal_dialog', + 'drupal_modal', + 'drupal_dialog.off_canvas', + ]); + } + + /** * Submit form dialog #ajax callback. * * @param array $form @@ -97,7 +113,7 @@ public function submitFormDialog(array &$form, FormStateInterface $form_state) { '#type' => 'status_messages', '#weight' => -1000, ]; - $command = new ReplaceCommand('#off-canvas-form', $form); + $command = new ReplaceCommand('#dialog-form', $form); } else { if ($redirect_url = $this->getRedirectUrl()) { @@ -122,21 +138,16 @@ public function submitFormDialog(array &$form, FormStateInterface $form_state) { * An AJAX response that display validation error messages. */ public function closeDialog(array &$form, FormStateInterface $form_state) { - return (new AjaxResponse())->addCommand(new CloseDialogCommand('#drupal-off-canvas')); + return (new AjaxResponse())->addCommand(new CloseDialogCommand($this->getDialogSelector())); } /** - * Empty submit #ajax submit callback. - * - * This allows modal dialog to using ::submitCallback to validate and submit - * the form via one ajax required. - * - * @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 mixed */ - public function noSubmit(array &$form, FormStateInterface $form_state) { + protected function getRequestWrapperFormat() { + $wrapper_format = $this->getRequest() + ->get(MainContentViewSubscriber::WRAPPER_FORMAT); + return $wrapper_format; } /** @@ -180,12 +191,15 @@ protected function getRedirectDestinationPath() { } /** - * Implements \Drupal\Core\Form\FormInterface::buildForm(). + * @return string */ - public function buildForm(array $form, FormStateInterface $form_state) { - $form = parent::buildForm($form, $form_state); - $this->buildFormDialog($form, $form_state); - return $form; + protected function getDialogSelector() { + if ($this->getRequestWrapperFormat() === 'drupal_dialog.off_canvas') { + return '#drupal-off-canvas'; + } + else { + return '#drupal-modal'; + } } } diff --git a/core/modules/outside_in/src/Block/BlockEntityOffCanvasForm.php b/core/modules/outside_in/src/Block/BlockEntityOffCanvasForm.php index 709f922e6d..58d287fed8 100644 --- a/core/modules/outside_in/src/Block/BlockEntityOffCanvasForm.php +++ b/core/modules/outside_in/src/Block/BlockEntityOffCanvasForm.php @@ -5,9 +5,9 @@ use Drupal\block\BlockForm; use Drupal\block\BlockInterface; use Drupal\Core\Block\BlockPluginInterface; +use Drupal\Core\Form\DialogFormTrait; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\PluginWithFormsInterface; -use Drupal\outside_in\OffCanvasFormDialogTrait; /** * Provides form for block instance forms when used in the off-canvas dialog. @@ -17,7 +17,7 @@ */ class BlockEntityOffCanvasForm extends BlockForm { - use OffCanvasFormDialogTrait; + use DialogFormTrait; /** * Provides a title callback to get the block's admin label. @@ -123,4 +123,13 @@ public function submitForm(array &$form, FormStateInterface $form_state) { $form_state->disableRedirect(); } + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state) { + $form = parent::buildForm($form, $form_state); + $this->buildFormDialog($form, $form_state); + return $form; + } + } diff --git a/core/modules/outside_in/tests/modules/off_canvas_test/src/Form/TestForm.php b/core/modules/outside_in/tests/modules/off_canvas_test/src/Form/TestForm.php index 8a12d192d9..64b14e8e6c 100644 --- a/core/modules/outside_in/tests/modules/off_canvas_test/src/Form/TestForm.php +++ b/core/modules/outside_in/tests/modules/off_canvas_test/src/Form/TestForm.php @@ -2,16 +2,16 @@ namespace Drupal\off_canvas_test\Form; +use Drupal\Core\Form\DialogFormTrait; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; -use Drupal\outside_in\OffCanvasFormDialogTrait; /** * Just a test form. */ class TestForm extends FormBase { - use OffCanvasFormDialogTrait; + use DialogFormTrait; /** * {@inheritdoc}