diff --git a/core/modules/layout_builder/layout_builder.info.yml b/core/modules/layout_builder/layout_builder.info.yml index 7947421cf5..e985911464 100644 --- a/core/modules/layout_builder/layout_builder.info.yml +++ b/core/modules/layout_builder/layout_builder.info.yml @@ -6,5 +6,4 @@ version: VERSION core: 8.x dependencies: - layout_discovery - # @todo Remove dependency once https://www.drupal.org/node/2784443 is in. - - settings_tray + - contextual diff --git a/core/modules/layout_builder/layout_builder.libraries.yml b/core/modules/layout_builder/layout_builder.libraries.yml index 9c17391620..8472775636 100644 --- a/core/modules/layout_builder/layout_builder.libraries.yml +++ b/core/modules/layout_builder/layout_builder.libraries.yml @@ -7,4 +7,4 @@ drupal.layout_builder: js/layout-builder.js: {} dependencies: - core/jquery.ui.sortable - - settings_tray/drupal.off_canvas + - core/drupal.dialog.off_canvas diff --git a/core/modules/layout_builder/src/Controller/AjaxHelperTrait.php b/core/modules/layout_builder/src/Controller/AjaxHelperTrait.php index 88f96d2c21..d0317570b9 100644 --- a/core/modules/layout_builder/src/Controller/AjaxHelperTrait.php +++ b/core/modules/layout_builder/src/Controller/AjaxHelperTrait.php @@ -13,13 +13,6 @@ */ trait AjaxHelperTrait { - /** - * The request stack. - * - * @var \Symfony\Component\HttpFoundation\RequestStack - */ - protected $requestStack; - /** * Determines if the current request is via AJAX. * @@ -27,7 +20,7 @@ * TRUE if the current request is via AJAX, FALSE otherwise. */ protected function isAjax() { - return in_array($this->requestStack->getCurrentRequest()->get(MainContentViewSubscriber::WRAPPER_FORMAT), [ + return in_array($this->getCurrentRequest()->get(MainContentViewSubscriber::WRAPPER_FORMAT), [ 'drupal_ajax', 'drupal_dialog', 'drupal_dialog.off_canvas', @@ -35,4 +28,14 @@ protected function isAjax() { ]); } + /** + * Wraps the current request. + * + * @return \Symfony\Component\HttpFoundation\Request + * The currently active request object. + */ + protected function getCurrentRequest() { + return \Drupal::request(); + } + } diff --git a/core/modules/layout_builder/src/Routing/LayoutBuilderRouteEnhancer.php b/core/modules/layout_builder/src/Routing/LayoutBuilderRouteEnhancer.php index 7c5aa28623..4a8b0afe23 100644 --- a/core/modules/layout_builder/src/Routing/LayoutBuilderRouteEnhancer.php +++ b/core/modules/layout_builder/src/Routing/LayoutBuilderRouteEnhancer.php @@ -2,7 +2,7 @@ namespace Drupal\layout_builder\Routing; -use Drupal\Core\Routing\Enhancer\RouteEnhancerInterface; +use Drupal\Core\Routing\EnhancerInterface; use Symfony\Cmf\Component\Routing\RouteObjectInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Route; @@ -12,12 +12,18 @@ * * @internal */ -class LayoutBuilderRouteEnhancer implements RouteEnhancerInterface { +class LayoutBuilderRouteEnhancer implements EnhancerInterface { /** - * {@inheritdoc} + * Returns whether the enhancer runs on the current route. + * + * @param \Symfony\Component\Routing\Route $route + * The current route. + * + * @return bool + * TRUE if this enhancer applies to this route. */ - public function applies(Route $route) { + protected function applies(Route $route) { return $route->getOption('_layout_builder') && $route->getDefault('entity_type_id'); } @@ -25,6 +31,11 @@ public function applies(Route $route) { * {@inheritdoc} */ public function enhance(array $defaults, Request $request) { + $route = $defaults[RouteObjectInterface::ROUTE_OBJECT]; + if (!$this->applies($route)) { + return $defaults; + } + if (!isset($defaults[$defaults['entity_type_id']])) { throw new \RuntimeException(sprintf('Failed to find the "%s" entity in route named %s', $defaults['entity_type_id'], $defaults[RouteObjectInterface::ROUTE_NAME])); } diff --git a/core/modules/layout_builder/tests/src/FunctionalJavascript/LayoutBuilderTest.php b/core/modules/layout_builder/tests/src/FunctionalJavascript/LayoutBuilderTest.php index 1bf83bca47..eefe63be34 100644 --- a/core/modules/layout_builder/tests/src/FunctionalJavascript/LayoutBuilderTest.php +++ b/core/modules/layout_builder/tests/src/FunctionalJavascript/LayoutBuilderTest.php @@ -328,4 +328,28 @@ public function testLayoutNoDialog() { $assert_session->pageTextContains('The block label'); } + /** + * {@inheritdoc} + * + * @todo Workaround for https://www.drupal.org/node/2918718. + */ + protected function clickContextualLink($selector, $link_locator, $force_visible = TRUE) { + if ($force_visible) { + $this->getSession()->executeScript("jQuery('{$selector} .contextual .trigger').removeClass('visually-hidden');"); + } + + $element = $this->getSession()->getPage()->find('css', $selector); + $link = $element->findLink($link_locator); + // If the link cannot be found, click the contextual link button first. + if (!$link || !$link->isVisible()) { + $element->find('css', '.contextual button')->press(); + $link = $element->findLink($link_locator); + } + $link->click(); + + if ($force_visible) { + $this->getSession()->executeScript("jQuery('{$selector} .contextual .trigger').addClass('visually-hidden');"); + } + } + } diff --git a/core/modules/layout_builder/tests/src/Unit/LayoutBuilderRouteEnhancerTest.php b/core/modules/layout_builder/tests/src/Unit/LayoutBuilderRouteEnhancerTest.php index 6e449c6cdb..3736ce84e6 100644 --- a/core/modules/layout_builder/tests/src/Unit/LayoutBuilderRouteEnhancerTest.php +++ b/core/modules/layout_builder/tests/src/Unit/LayoutBuilderRouteEnhancerTest.php @@ -22,7 +22,9 @@ public function testApplies($defaults, $options, $expected) { $route_enhancer = new LayoutBuilderRouteEnhancer(); $route = new Route('/some/path', $defaults, [], $options); - $result = $route_enhancer->applies($route); + $reflection_method = new \ReflectionMethod($route_enhancer, 'applies'); + $reflection_method->setAccessible(TRUE); + $result = $reflection_method->invoke($route_enhancer, $route); $this->assertSame($expected, $result); } @@ -73,17 +75,22 @@ public function providerTestApplies() { * @covers ::enhance */ public function testEnhanceValidDefaults() { + $route = new Route('/the/path', ['entity_type_id' => 'the_entity_type'], [], ['_layout_builder' => TRUE]); $route_enhancer = new LayoutBuilderRouteEnhancer(); $object = new \stdClass(); $defaults = [ 'entity_type_id' => 'the_entity_type', 'the_entity_type' => $object, + RouteObjectInterface::ROUTE_NAME => 'the_route_name', + RouteObjectInterface::ROUTE_OBJECT => $route, ]; // Ensure that the 'entity' key now contains the value stored for a given // entity type. $expected = [ 'entity_type_id' => 'the_entity_type', 'the_entity_type' => $object, + RouteObjectInterface::ROUTE_NAME => 'the_route_name', + RouteObjectInterface::ROUTE_OBJECT => $route, 'entity' => $object, ]; @@ -101,8 +108,10 @@ public function testEnhanceValidDefaults() { */ public function testEnhanceMissingEntity() { $route_enhancer = new LayoutBuilderRouteEnhancer(); + $route = new Route('/the/path', ['entity_type_id' => 'the_entity_type'], [], ['_layout_builder' => TRUE]); $defaults = [ RouteObjectInterface::ROUTE_NAME => 'the_route', + RouteObjectInterface::ROUTE_OBJECT => $route, 'entity_type_id' => 'the_entity_type', ]; $this->setExpectedException(\RuntimeException::class, 'Failed to find the "the_entity_type" entity in route named the_route');