diff --git a/core/modules/node/src/ContextProvider/NodeRouteContext.php b/core/modules/node/src/ContextProvider/NodeRouteContext.php index 8f0acd7de9..2eee8aa593 100644 --- a/core/modules/node/src/ContextProvider/NodeRouteContext.php +++ b/core/modules/node/src/ContextProvider/NodeRouteContext.php @@ -42,14 +42,18 @@ public function getRuntimeContexts(array $unqualified_context_ids) { $result = []; $context_definition = EntityContextDefinition::create('node')->setRequired(FALSE); $value = NULL; - if (($route_object = $this->routeMatch->getRouteObject()) && ($route_contexts = $route_object->getOption('parameters')) && isset($route_contexts['node'])) { - if ($node = $this->routeMatch->getParameter('node')) { + if (($route_object = $this->routeMatch->getRouteObject())) { + $route_contexts = $route_object->getOption('parameters'); + if (isset($route_contexts['node']) && $node = $this->routeMatch->getParameter('node')) { $value = $node; } - } - elseif ($this->routeMatch->getRouteName() == 'node.add') { - $node_type = $this->routeMatch->getParameter('node_type'); - $value = Node::create(['type' => $node_type->id()]); + elseif (isset($route_contexts['node_preview']) && $node = $this->routeMatch->getParameter('node_preview')) { + $value = $node; + } + elseif ($this->routeMatch->getRouteName() == 'node.add') { + $node_type = $this->routeMatch->getParameter('node_type'); + $value = Node::create(['type' => $node_type->id()]); + } } $cacheability = new CacheableMetadata(); diff --git a/core/modules/node/tests/src/Functional/NodeBlockFunctionalTest.php b/core/modules/node/tests/src/Functional/NodeBlockFunctionalTest.php index 131a9ce910..cc4dc0ba94 100644 --- a/core/modules/node/tests/src/Functional/NodeBlockFunctionalTest.php +++ b/core/modules/node/tests/src/Functional/NodeBlockFunctionalTest.php @@ -50,6 +50,7 @@ protected function setUp(): void { $this->adminUser = $this->drupalCreateUser([ 'administer content types', 'administer nodes', + 'bypass node access', 'administer blocks', 'access content overview', ]); @@ -184,6 +185,11 @@ public function testRecentNodeBlock() { $this->assertSame('HIT', $this->getSession()->getResponseHeader('X-Drupal-Dynamic-Cache')); $this->drupalLogin($this->adminUser); + + // Assert that the preview page displays the block as well. + $this->drupalPostForm('node/' . $node1->id() . '/edit', [], t('Preview')); + $this->assertSession()->pageTextContains($label); + $this->drupalGet('admin/structure/block'); $this->assertText($label, 'Block was displayed on the admin/structure/block page.'); $this->assertLinkByHref($block->toUrl()->toString());