diff --git a/core/modules/layout_builder/src/Controller/LayoutBuilderController.php b/core/modules/layout_builder/src/Controller/LayoutBuilderController.php index 4acf94538f..32a96a608b 100644 --- a/core/modules/layout_builder/src/Controller/LayoutBuilderController.php +++ b/core/modules/layout_builder/src/Controller/LayoutBuilderController.php @@ -325,32 +325,4 @@ public function cancelLayout(SectionStorageInterface $section_storage) { return new RedirectResponse($section_storage->getCanonicalUrl()->setAbsolute()->toString()); } - /** - * Reverts the overridden layout to the defaults. - * - * @param \Drupal\layout_builder\SectionStorageInterface $section_storage - * The section storage. - * - * @return \Symfony\Component\HttpFoundation\RedirectResponse - * A redirect response. - */ - public function revertLayout(SectionStorageInterface $section_storage) { - if (!$section_storage instanceof OverridesSectionStorageInterface) { - throw new \InvalidArgumentException(sprintf('The section storage with type "%s" and ID "%s" does not provide overrides', $section_storage->getStorageType(), $section_storage->getStorageId())); - } - - // Remove all sections. - while ($section_storage->count()) { - $section_storage->removeSection(0); - } - $section_storage->save(); - $this->layoutTempstoreRepository->delete($section_storage); - - $this->messenger->addMessage($this->t('The layout has been reverted back to defaults.')); - - // @todo Decide if this is the correct URL to return in - // https://www.drupal.org/project/drupal/issues/2936501. - return new RedirectResponse($section_storage->getLayoutBuilderUrl()->setAbsolute()->toString()); - } - } diff --git a/core/modules/layout_builder/src/Form/RevertOverridesForm.php b/core/modules/layout_builder/src/Form/RevertOverridesForm.php new file mode 100644 index 0000000000..54d33634fd --- /dev/null +++ b/core/modules/layout_builder/src/Form/RevertOverridesForm.php @@ -0,0 +1,110 @@ +layoutTempstoreRepository = $layout_tempstore_repository; + $this->messenger = $messenger; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('layout_builder.tempstore_repository'), + $container->get('messenger') + ); + } + + /** + * {@inheritdoc} + */ + public function getFormId() { + return 'layout_builder_revert_overrides'; + } + + /** + * {@inheritdoc} + */ + public function getQuestion() { + return $this->t('Are you sure you want to revert this to defaults?'); + } + + /** + * {@inheritdoc} + */ + public function getCancelUrl() { + return $this->sectionStorage->getLayoutBuilderUrl(); + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL) { + if (!$section_storage instanceof OverridesSectionStorageInterface) { + throw new \InvalidArgumentException(sprintf('The section storage with type "%s" and ID "%s" does not provide overrides', $section_storage->getStorageType(), $section_storage->getStorageId())); + } + + $this->sectionStorage = $section_storage; + return parent::buildForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + // Remove all sections. + while ($this->sectionStorage->count()) { + $this->sectionStorage->removeSection(0); + } + $this->sectionStorage->save(); + $this->layoutTempstoreRepository->delete($this->sectionStorage); + + $this->messenger->addMessage($this->t('The layout has been reverted back to defaults.')); + $form_state->setRedirectUrl($this->getCancelUrl()); + } + +} diff --git a/core/modules/layout_builder/src/Routing/LayoutBuilderRoutesTrait.php b/core/modules/layout_builder/src/Routing/LayoutBuilderRoutesTrait.php index 920cbcf93e..da9e1c1759 100644 --- a/core/modules/layout_builder/src/Routing/LayoutBuilderRoutesTrait.php +++ b/core/modules/layout_builder/src/Routing/LayoutBuilderRoutesTrait.php @@ -78,7 +78,7 @@ protected function buildRoute($class, $route_name_prefix, $path, array $defaults if (is_subclass_of($class, OverridesSectionStorageInterface::class)) { $revert_defaults = $defaults; - $revert_defaults['_controller'] = '\Drupal\layout_builder\Controller\LayoutBuilderController::revertLayout'; + $revert_defaults['_form'] = '\Drupal\layout_builder\Form\RevertOverridesForm'; $route = (new Route("$path/revert")) ->setDefaults($revert_defaults) ->setRequirements($requirements) diff --git a/core/modules/layout_builder/tests/src/Unit/LayoutBuilderRoutesTest.php b/core/modules/layout_builder/tests/src/Unit/LayoutBuilderRoutesTest.php index 7eb293bd7a..08e234eaa2 100644 --- a/core/modules/layout_builder/tests/src/Unit/LayoutBuilderRoutesTest.php +++ b/core/modules/layout_builder/tests/src/Unit/LayoutBuilderRoutesTest.php @@ -154,7 +154,7 @@ public function testGetRoutes() { 'entity_type_id' => 'with_link_template', 'section_storage_type' => 'overrides', 'section_storage' => '', - '_controller' => '\Drupal\layout_builder\Controller\LayoutBuilderController::revertLayout', + '_form' => '\Drupal\layout_builder\Form\RevertOverridesForm', ], [ '_has_layout_section' => 'true', @@ -235,7 +235,7 @@ public function testGetRoutes() { 'entity_type_id' => 'with_integer_id', 'section_storage_type' => 'overrides', 'section_storage' => '', - '_controller' => '\Drupal\layout_builder\Controller\LayoutBuilderController::revertLayout', + '_form' => '\Drupal\layout_builder\Form\RevertOverridesForm', ], [ '_has_layout_section' => 'true', @@ -317,7 +317,7 @@ public function testGetRoutes() { 'entity_type_id' => 'with_field_ui_route', 'section_storage_type' => 'overrides', 'section_storage' => '', - '_controller' => '\Drupal\layout_builder\Controller\LayoutBuilderController::revertLayout', + '_form' => '\Drupal\layout_builder\Form\RevertOverridesForm', ], [ '_has_layout_section' => 'true', @@ -399,7 +399,7 @@ public function testGetRoutes() { 'entity_type_id' => 'with_bundle_key', 'section_storage_type' => 'overrides', 'section_storage' => '', - '_controller' => '\Drupal\layout_builder\Controller\LayoutBuilderController::revertLayout', + '_form' => '\Drupal\layout_builder\Form\RevertOverridesForm', ], [ '_has_layout_section' => 'true', @@ -481,7 +481,7 @@ public function testGetRoutes() { 'entity_type_id' => 'with_bundle_parameter', 'section_storage_type' => 'overrides', 'section_storage' => '', - '_controller' => '\Drupal\layout_builder\Controller\LayoutBuilderController::revertLayout', + '_form' => '\Drupal\layout_builder\Form\RevertOverridesForm', ], [ '_has_layout_section' => 'true',