diff --git a/core/lib/Drupal/Core/Form/DialogFormTrait.php b/core/lib/Drupal/Core/Form/DialogFormTrait.php index 145a34ecfa..3d4d884a50 100644 --- a/core/lib/Drupal/Core/Form/DialogFormTrait.php +++ b/core/lib/Drupal/Core/Form/DialogFormTrait.php @@ -21,11 +21,16 @@ * An associative array containing the structure of the form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. + * @param bool $create_cancel + * If TRUE the create submit button will be created. + * @param string $dialog_selector + * The CSS selector for the associated dialog. */ - protected function buildFormDialog(array &$form, FormStateInterface $form_state, $create_cancel = FALSE) { + protected function buildFormDialog(array &$form, FormStateInterface $form_state, $create_cancel = FALSE, $dialog_selector = '#drupal-modal') { if (!$this->isDialog()) { return; } + $form_state->set('dialog_selector', $dialog_selector); $ajax_callback_added = FALSE; @@ -138,7 +143,8 @@ 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($this->getDialogSelector())); + $selector = $form_state->get('dialog_selector'); + return (new AjaxResponse())->addCommand(new CloseDialogCommand($selector)); } /** @@ -190,16 +196,4 @@ protected function getRedirectDestinationPath() { } } - /** - * @return string - */ - 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 58d287fed8..063a2ea87d 100644 --- a/core/modules/outside_in/src/Block/BlockEntityOffCanvasForm.php +++ b/core/modules/outside_in/src/Block/BlockEntityOffCanvasForm.php @@ -128,7 +128,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) { */ public function buildForm(array $form, FormStateInterface $form_state) { $form = parent::buildForm($form, $form_state); - $this->buildFormDialog($form, $form_state); + $this->buildFormDialog($form, $form_state, FALSE, '#drupal-off-canvas'); 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 64b14e8e6c..3812e40ca9 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 @@ -39,7 +39,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#value' => $this->t('Cancel'), ], ]; - $this->buildFormDialog($form, $form_state); + $this->buildFormDialog($form, $form_state, FALSE, '#drupal-off-canvas'); return $form; }