Unverified Commit 19799280 authored by alexpott's avatar alexpott

Issue #2890758 by joseph.olstad, Berdir, Kumar Kundan, Piyush_Rai, mrinalini9,...

Issue #2890758 by joseph.olstad, Berdir, Kumar Kundan, Piyush_Rai, mrinalini9, Hardik_Patel_12, ridhimaabrol24, ljcarnieri, yoruvo, priyanka.sahni, jwilson3: Block visibility node type not working on preview/revision routes
parent 8e736dcb
......@@ -42,14 +42,24 @@ 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');
// Check for a node revision parameter first.
// @todo https://www.drupal.org/i/2730631 will allow to use the upcasted
// node revision object.
if ($revision_id = $this->routeMatch->getRawParameter('node_revision')) {
$value = \Drupal::entityTypeManager()->getStorage('node')->loadRevision($revision_id);
}
elseif (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();
......
name: 'Node block test'
type: module
package: Testing
version: VERSION
<?php
namespace Drupal\node_block_test\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
* Provides a 'Node Context Test' block.
*
* @Block(
* id = "node_block_test_context",
* label = @Translation("Node Context Test"),
* context_definitions = {
* "node" = @ContextDefinition("entity:node", label = @Translation("Node"))
* }
* )
*/
class NodeContextTestBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
/** @var \Drupal\node\NodeInterface $node */
$node = $this->getContextValue('node');
return [
'#type' => 'inline_template',
'#template' => 'Displaying node #{{ id }}, revision #{{ revision_id }}: {{ title }}',
'#context' => [
'id' => $node->id(),
'revision_id' => $node->getRevisionId(),
'title' => $node->label(),
],
];
}
}
......@@ -5,6 +5,7 @@
use Drupal\block\Entity\Block;
use Drupal\Core\Database\Database;
use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
use Drupal\Core\Url;
use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
use Drupal\user\RoleInterface;
......@@ -41,7 +42,7 @@ class NodeBlockFunctionalTest extends NodeTestBase {
*
* @var array
*/
protected static $modules = ['block', 'views'];
protected static $modules = ['block', 'views', 'node_block_test'];
protected function setUp(): void {
parent::setUp();
......@@ -50,6 +51,7 @@ protected function setUp(): void {
$this->adminUser = $this->drupalCreateUser([
'administer content types',
'administer nodes',
'bypass node access',
'administer blocks',
'access content overview',
]);
......@@ -83,6 +85,12 @@ public function testRecentNodeBlock() {
$node2 = $this->drupalCreateNode($default_settings);
$node3 = $this->drupalCreateNode($default_settings);
// Create a second revision of node1.
$node1_revision_1 = $node1;
$node1->setNewRevision(TRUE);
$node1->setTitle('Node revision 2 title');
$node1->save();
$connection = Database::getConnection();
// Change the changed time for node so that we can test ordering.
$connection->update('node_field_data')
......@@ -183,7 +191,33 @@ public function testRecentNodeBlock() {
$this->drupalGet('node/' . $node5->id());
$this->assertSame('HIT', $this->getSession()->getResponseHeader('X-Drupal-Dynamic-Cache'));
// Place a block to determine which revision is provided as context
// to blocks.
$this->drupalPlaceBlock('node_block_test_context', [
'context_mapping' => ['node' => '@node.node_route_context:node'],
]);
$this->drupalLogin($this->adminUser);
$this->drupalGet('node/' . $node1->id());
$this->assertSession()->pageTextContains($label);
$this->assertSession()->pageTextContains('Displaying node #' . $node1->id() . ', revision #' . $node1->getRevisionId() . ': Node revision 2 title');
// Assert that the preview page displays the block as well.
$this->drupalPostForm('node/' . $node1->id() . '/edit', [], t('Preview'));
$this->assertSession()->pageTextContains($label);
// The previewed node object has no revision ID.
$this->assertSession()->pageTextContains('Displaying node #' . $node1->id() . ', revision #: Node revision 2 title');
// Assert that the revision page for both revisions displays the block.
$this->drupalGet(Url::fromRoute('entity.node.revision', ['node' => $node1->id(), 'node_revision' => $node1_revision_1->getRevisionId()]));
$this->assertSession()->pageTextContains($label);
$this->assertSession()->pageTextContains('Displaying node #' . $node1->id() . ', revision #' . $node1_revision_1->getRevisionId() . ': ' . $node1_revision_1->label());
$this->drupalGet(Url::fromRoute('entity.node.revision', ['node' => $node1->id(), 'node_revision' => $node1->getRevisionId()]));
$this->assertSession()->pageTextContains($label);
$this->assertSession()->pageTextContains('Displaying node #' . $node1->id() . ', revision #' . $node1->getRevisionId() . ': Node revision 2 title');
$this->drupalGet('admin/structure/block');
$this->assertText($label, 'Block was displayed on the admin/structure/block page.');
$this->assertLinkByHref($block->toUrl()->toString());
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment