diff --git a/core/modules/outside_in/outside_in.libraries.yml b/core/modules/outside_in/outside_in.libraries.yml index 574d7ed656..5c388b82f1 100644 --- a/core/modules/outside_in/outside_in.libraries.yml +++ b/core/modules/outside_in/outside_in.libraries.yml @@ -20,8 +20,6 @@ drupal.outside_in: - core/drupal - core/jquery.once - core/drupal.ajax - - core/drupalSettings - - core/jquery.form drupal.off_canvas: version: VERSION js: diff --git a/core/modules/outside_in/src/Block/BlockEntityOffCanvasForm.php b/core/modules/outside_in/src/Block/BlockEntityOffCanvasForm.php index f473d18ee6..8e74754fdb 100644 --- a/core/modules/outside_in/src/Block/BlockEntityOffCanvasForm.php +++ b/core/modules/outside_in/src/Block/BlockEntityOffCanvasForm.php @@ -8,6 +8,7 @@ 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. @@ -19,7 +20,7 @@ */ class BlockEntityOffCanvasForm extends BlockForm { - use DialogFormTrait; + use OffCanvasFormDialogTrait; /** * Provides a title callback to get the block's admin label. diff --git a/core/lib/Drupal/Core/Form/DialogFormTrait.php b/core/modules/outside_in/src/OffCanvasFormDialogTrait.php similarity index 80% rename from core/lib/Drupal/Core/Form/DialogFormTrait.php rename to core/modules/outside_in/src/OffCanvasFormDialogTrait.php index dead12f3fd..2f0da18b01 100644 --- a/core/lib/Drupal/Core/Form/DialogFormTrait.php +++ b/core/modules/outside_in/src/OffCanvasFormDialogTrait.php @@ -1,37 +1,51 @@ getRequest() + ->get(MainContentViewSubscriber::WRAPPER_FORMAT); + return $wrapper_format === 'drupal_dialog.off_canvas'; + } /** - * Adds dialog support to a form. + * Adds modal 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. - * @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, $dialog_selector = '#drupal-modal') { - if (!$this->isDialog()) { + protected function buildFormDialog(array &$form, FormStateInterface $form_state) { + if (!$this->isOffCanvasDialog()) { return; } - $form_state->set('dialog_selector', $dialog_selector); $ajax_callback_added = FALSE; @@ -45,62 +59,36 @@ protected function buildFormDialog(array &$form, FormStateInterface $form_state, $ajax_callback_added = TRUE; } - 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'] = [ + '#type' => 'submit', + '#value' => $this->t('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'] = 'dialog-form'; + $form['#attributes']['id'] = 'off-canvas-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. + * @return mixed */ - protected function isDialog() { - return in_array($this->getRequestWrapperFormat(), [ - 'drupal_dialog', - 'drupal_modal', - 'drupal_dialog.off_canvas', - ]); + protected function getRequestWrapperFormat() { + $wrapper_format = $this->getRequest() + ->get(MainContentViewSubscriber::WRAPPER_FORMAT); + return $wrapper_format; } - /** * */ @@ -139,7 +127,7 @@ public function submitFormDialog(array &$form, FormStateInterface $form_state) { '#type' => 'status_messages', '#weight' => -1000, ]; - $command = new ReplaceCommand('#dialog-form', $form); + $command = new ReplaceCommand('#off-canvas-form', $form); } else { if ($redirect_url = $this->getRedirectUrl()) { @@ -164,17 +152,21 @@ public function submitFormDialog(array &$form, FormStateInterface $form_state) { * An AJAX response that display validation error messages. */ public function closeDialog(array &$form, FormStateInterface $form_state) { - $selector = $form_state->get('dialog_selector'); - return (new AjaxResponse())->addCommand(new CloseDialogCommand($selector)); + return (new AjaxResponse())->addCommand(new CloseDialogCommand('#drupal-off-canvas')); } /** - * @return mixed + * 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. */ - protected function getRequestWrapperFormat() { - $wrapper_format = $this->getRequest() - ->get(MainContentViewSubscriber::WRAPPER_FORMAT); - return $wrapper_format; + public function noSubmit(array &$form, FormStateInterface $form_state) { } /** @@ -198,8 +190,7 @@ protected function getRedirectUrl() { */ protected function getDestinationUrl() { if ($destination = $this->getRedirectDestinationPath()) { - $options = UrlHelper::parse($destination); - return Url::fromUserInput('/' . ltrim($options['path'], '/'), $options); + return Url::fromUserInput('/' . $destination); } } 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 3812e40ca9..eaddd816f0 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 DialogFormTrait; + use OffCanvasFormDialogTrait; /** * {@inheritdoc}