diff --git a/core/modules/outside_in/js/outside_in.js b/core/modules/outside_in/js/outside_in.js index 5d42199..a49dd60 100644 --- a/core/modules/outside_in/js/outside_in.js +++ b/core/modules/outside_in/js/outside_in.js @@ -247,6 +247,7 @@ instance.options.data.dialogOptions = {}; } instance.options.data.dialogOptions.outsideInActiveEditableId = $(instance.element).parents('.outside-in-editable').attr('id'); + instance.options.url += '&editable_id=' + instance.options.data.dialogOptions.outsideInActiveEditableId; instance.progress = {type: 'fullscreen'}; } else { @@ -254,6 +255,10 @@ instance.options.data.formOptions = {messagesSelector: '.messages__wrapper'}; } }); + var qs = drupalSettings.path.currentQuery; + if (qs.hasOwnProperty('editable_id')) { + $('#' + qs.editable_id + ' a' + blockConfigureSelector).once('outside_in_qs').trigger('click'); + } } }; @@ -276,4 +281,4 @@ } }); -})(jQuery, Drupal); +})(jQuery, Drupal, drupalSettings); diff --git a/core/modules/outside_in/outside_in.libraries.yml b/core/modules/outside_in/outside_in.libraries.yml index 5eddea4..5c9fc8e 100644 --- a/core/modules/outside_in/outside_in.libraries.yml +++ b/core/modules/outside_in/outside_in.libraries.yml @@ -20,6 +20,7 @@ drupal.outside_in: - core/drupal - core/jquery.once - core/drupal.ajax + - core/drupalSettings 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 92fe391..08f5dad 100644 --- a/core/modules/outside_in/src/Block/BlockEntityOffCanvasForm.php +++ b/core/modules/outside_in/src/Block/BlockEntityOffCanvasForm.php @@ -100,21 +100,21 @@ protected function getPluginForm(BlockPluginInterface $block) { /** * {@inheritdoc} - * - * @todo Remove this function. Temp to demo validation error in OffCanvas. */ - public function validateForm(array &$form, FormStateInterface $form_state) { - parent::validateForm($form, $form_state); - $form_state->setErrorByName('none', 'All your submit belong to us!'); + protected function actionsElement(array $form, FormStateInterface $form_state) { + $actions_element = parent::actionsElement($form, $form_state); + $actions_element['submit']['#attributes']['class'][] = 'use-ajax-submit'; + return $actions_element; } /** * {@inheritdoc} */ - protected function actionsElement(array $form, FormStateInterface $form_state) { - $actions_element = parent::actionsElement($form, $form_state); - $actions_element['submit']['#attributes']['class'][] = 'use-ajax-submit'; - return $actions_element; + public function submitForm(array &$form, FormStateInterface $form_state) { + parent::submitForm($form, $form_state); + // \Drupal\block\BlockForm::submitForm() always redirects to block listing. + // This would doesn't work with Ajax submit. + $form_state->disableRedirect(); } } diff --git a/core/modules/outside_in/src/Render/MainContent/AjaxRenderer.php b/core/modules/outside_in/src/Render/MainContent/AjaxRenderer.php index 31e1f14..596824b 100644 --- a/core/modules/outside_in/src/Render/MainContent/AjaxRenderer.php +++ b/core/modules/outside_in/src/Render/MainContent/AjaxRenderer.php @@ -5,9 +5,11 @@ use Drupal\Core\Ajax\AjaxResponse; use Drupal\Core\Ajax\AlertCommand; use Drupal\Core\Ajax\InsertCommand; +use Drupal\Core\Ajax\RedirectCommand; use Drupal\Core\Ajax\ReplaceCommand; use Drupal\Core\Render\MainContent\AjaxRenderer as CoreAjaxRender; use Drupal\Core\Routing\RouteMatchInterface; +use Drupal\Core\Url; use Symfony\Component\HttpFoundation\Request; /** @@ -46,21 +48,46 @@ public function renderResponse(array $main_content, Request $request, RouteMatch // replace the element making the Ajax call. The default 'replaceWith' // behavior can be changed with #ajax['method']. $response->addCommand(new InsertCommand(NULL, $html)); - $status_messages = array('#type' => 'status_messages'); - $output = $this->drupalRenderRoot($status_messages); - if (!empty($output)) { + + if ($this->hasErrors()) { + $status_messages = ['#type' => 'status_messages']; + $output = $this->drupalRenderRoot($status_messages); // If there are any status messages replace the existing messages. $options = $request->request->get('formOptions', []); + $messages_selector = isset($options['messagesSelector']) ? $options['messagesSelector'] : NULL; $response->addCommand(new ReplaceCommand($messages_selector, $output)); } else { - // @todo If no messages then the dialog should be close, the page refreshed - // the same same dialog should be then be re-opened. - // A command will need to be created. + if ($destination = $request->query->get('destination')) { + // If an editable id is specified add to query string to open dialog. + if ($editable_id = $request->query->get('editable_id')) { + $destination .= "?editable_id=" . $editable_id; + } + $url = Url::fromUserInput('/' . $destination); + + $response->addCommand(new RedirectCommand($url->setAbsolute()->toString())); + } } return $response; } + /** + * Determine if there are errors on the page. + * + * @todo This is a hacky way to figure if there are form errors. + * + * @return bool + * TRUE if there were any errors, FALSE otherwise. + */ + protected function hasErrors() { + $messages = drupal_get_messages(NULL, FALSE); + foreach ($messages as $key => $list) { + if ($key !== 'status' && $list) { + return TRUE; + } + } + return FALSE; + } }