diff --git a/core/modules/outside_in/src/OffCanvasFormDialogTrait.php b/core/modules/outside_in/src/OffCanvasFormDialogTrait.php index 2f0da18b01..990d897e4f 100644 --- a/core/modules/outside_in/src/OffCanvasFormDialogTrait.php +++ b/core/modules/outside_in/src/OffCanvasFormDialogTrait.php @@ -3,7 +3,6 @@ namespace Drupal\outside_in; use Drupal\Core\Ajax\AjaxResponse; -use Drupal\Core\Ajax\CloseDialogCommand; use Drupal\Core\Ajax\RedirectCommand; use Drupal\Core\Ajax\ReplaceCommand; use Drupal\Core\EventSubscriber\MainContentViewSubscriber; @@ -47,38 +46,15 @@ protected function buildFormDialog(array &$form, FormStateInterface $form_state) return; } - $ajax_callback_added = FALSE; + $form['actions']['submit']['#ajax'] = [ + 'dialogType' => $this->getDialogType(), + 'dialogRenderer' => $this->getDialogRenderer(), + 'callback' => '::submitFormDialog', + 'event' => 'click', + ]; - if (!empty($form['actions']['submit'])) { - $form['actions']['submit']['#ajax'] = [ - 'dialogType' => $this->getDialogType(), - 'dialogRenderer' => $this->getDialogRenderer(), - 'callback' => '::submitFormDialog', - 'event' => 'click', - ]; - $ajax_callback_added = TRUE; - } - - if (!empty($form['actions']['cancel'])) { - // Replace 'Cancel' link button with a close dialog button. - $form['actions']['cancel'] = [ - '#type' => 'submit', - '#value' => $this->t('Cancel'), - '#submit' => ['::noSubmit'], - '#limit_validation_errors' => [], - '#weight' => 100, - '#ajax' => [ - 'callback' => '::closeDialog', - 'event' => 'click', - ], - ]; - $ajax_callback_added = TRUE; - } - - if ($ajax_callback_added) { - $form['#attached']['library'][] = 'core/drupal.dialog.ajax'; - $form['#attributes']['id'] = 'off-canvas-form'; - } + $form['#attached']['library'][] = 'core/drupal.dialog.ajax'; + $form['#attributes']['id'] = 'off-canvas-form'; } /** @@ -134,42 +110,14 @@ public function submitFormDialog(array &$form, FormStateInterface $form_state) { $command = new RedirectCommand($redirect_url->setAbsolute()->toString()); } else { - return $this->closeDialog($form, $form_state); + // Settings Tray always provides a destination. + throw new \Exception("No destination provide for Settings Tray form"); } } return $response->addCommand($command); } /** - * Close 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 bool|\Drupal\Core\Ajax\AjaxResponse - * 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')); - } - - /** - * 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) { - } - - /** * Gets the form's redirect URL. * * Isolate a form's redirect URL/destination so that it can be used by diff --git a/core/modules/outside_in/tests/modules/off_canvas_test/src/Controller/TestController.php b/core/modules/outside_in/tests/modules/off_canvas_test/src/Controller/TestController.php index 1208edf2a0..58bebc3a0b 100644 --- a/core/modules/outside_in/tests/modules/off_canvas_test/src/Controller/TestController.php +++ b/core/modules/outside_in/tests/modules/off_canvas_test/src/Controller/TestController.php @@ -111,23 +111,6 @@ public function linksDisplay() { ], ], ], - 'off_canvas_form_no_dest' => [ - '#title' => 'Show form: no destination!', - '#type' => 'link', - '#url' => Url::fromRoute( - 'off_canvas_test.form' - ), - '#attributes' => [ - 'class' => ['use-ajax'], - 'data-dialog-type' => 'dialog', - 'data-dialog-renderer' => 'off_canvas', - ], - '#attached' => [ - 'library' => [ - 'outside_in/drupal.outside_in', - ], - ], - ], ]; } 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 eaddd816f0..a9a4350042 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 @@ -34,12 +34,8 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#type' => 'submit', '#value' => $this->t('Submit'), ], - 'cancel' => [ - '#type' => 'submit', - '#value' => $this->t('Cancel'), - ], ]; - $this->buildFormDialog($form, $form_state, FALSE, '#drupal-off-canvas'); + $this->buildFormDialog($form, $form_state); return $form; } diff --git a/core/modules/outside_in/tests/src/FunctionalJavascript/OffCanvasTest.php b/core/modules/outside_in/tests/src/FunctionalJavascript/OffCanvasTest.php index bc39787b09..a2bd7a46a5 100644 --- a/core/modules/outside_in/tests/src/FunctionalJavascript/OffCanvasTest.php +++ b/core/modules/outside_in/tests/src/FunctionalJavascript/OffCanvasTest.php @@ -146,31 +146,6 @@ public function testFormErrors() { $web_assert->assertWaitOnAjaxRequest(); $web_assert->elementNotContains('css', 'body', 'submitted'); $web_assert->elementContains('css', '#drupal-off-canvas', 'Validation error'); - - // Submit form with no error and NOT sending a destination. - $this->drupalGet('/off-canvas-test-links'); - $page->clickLink('Show form: no destination!'); - $this->waitForOffCanvasToOpen(); - $page->pressButton('Submit'); - $web_assert->assertWaitOnAjaxRequest(); - // Make sure the changes are present. - $this->assertEmpty($web_assert->waitForElement('css', 'div.messages.messages--status:contains(submitted)')); - $web_assert->elementNotContains('css', 'body', 'Validation error'); - // If no validation error and no destination provided page will not be - // redirected but the dialog should be closed. - $this->waitForNoElement('css', '#drupal-off-canvas'); - - $this->drupalGet('/off-canvas-test-links'); - - // Submit form with error and NOT sending a destination. - $this->drupalGet('/off-canvas-test-links'); - $page->clickLink('Show form: no destination!'); - $this->waitForOffCanvasToOpen(); - $page->checkField('Force error?'); - $page->pressButton('Submit'); - $web_assert->assertWaitOnAjaxRequest(); - $web_assert->elementNotContains('css', 'body', 'submitted'); - $web_assert->elementContains('css', '#drupal-off-canvas', 'Validation error'); } }