Commit f9365a0a authored by effulgentsia's avatar effulgentsia

Issue #2939742 by plach, Wim Leers: Node links always load the default revision during rendering

parent 27700fa1
......@@ -4,7 +4,6 @@
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityViewBuilder;
use Drupal\node\Entity\Node;
/**
* View builder handler for nodes.
......@@ -34,6 +33,7 @@ public function buildComponents(array &$build, array $entities, array $displays,
$view_mode,
$entity->language()->getId(),
!empty($entity->in_preview),
$entity->isDefaultRevision() ? NULL : $entity->getLoadedRevisionId(),
],
],
];
......@@ -77,11 +77,14 @@ protected function getBuildDefaults(EntityInterface $entity, $view_mode) {
* The language in which the node entity is being viewed.
* @param bool $is_in_preview
* Whether the node is currently being previewed.
* @param $revision_id
* (optional) The identifier of the node revision to be loaded. If none
* is provided, the default revision will be loaded.
*
* @return array
* A renderable array representing the node links.
*/
public static function renderLinks($node_entity_id, $view_mode, $langcode, $is_in_preview) {
public static function renderLinks($node_entity_id, $view_mode, $langcode, $is_in_preview, $revision_id = NULL) {
$links = [
'#theme' => 'links__node',
'#pre_render' => ['drupal_pre_render_links'],
......@@ -89,7 +92,10 @@ public static function renderLinks($node_entity_id, $view_mode, $langcode, $is_i
];
if (!$is_in_preview) {
$entity = Node::load($node_entity_id)->getTranslation($langcode);
$storage = \Drupal::entityTypeManager()->getStorage('node');
/** @var \Drupal\node\NodeInterface $revision */
$revision = !isset($revision_id) ? $storage->load($node_entity_id) : $storage->loadRevision($revision_id);
$entity = $revision->getTranslation($langcode);
$links['node'] = static::buildLinks($entity, $view_mode);
// Allow other modules to alter the node links.
......
<?php
namespace Drupal\Tests\node\Kernel;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\user\Entity\User;
/**
* Tests the node view builder.
*
* @group node
*
* @coversDefaultClass \Drupal\node\NodeViewBuilder
*/
class NodeViewBuilderTest extends EntityKernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['node'];
/**
* The node storage.
*
* @var \Drupal\node\NodeStorageInterface
*/
protected $storage;
/**
* The node view builder.
*
* @var \Drupal\Core\Entity\EntityViewBuilderInterface
*/
protected $viewBuilder;
/**
* The renderer.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->storage = $this->entityManager->getStorage('node');
$this->viewBuilder = $this->entityManager->getViewBuilder('node');
$this->renderer = $this->container->get('renderer');
$type = NodeType::create([
'type' => 'article',
'name' => 'Article',
]);
$type->save();
$this->installSchema('node', 'node_access');
$this->installConfig(['system', 'node']);
}
/**
* Tests that node links are displayed correctly in pending revisions.
*
* @covers ::buildComponents
* @covers ::renderLinks
* @covers ::buildLinks
*/
public function testPendingRevisionLinks() {
$account = User::create([
'name' => $this->randomString(),
]);
$account->save();
$title = $this->randomMachineName();
$node = Node::create([
'type' => 'article',
'title' => $title,
'uid' => $account->id(),
]);
$node->save();
/** @var \Drupal\node\NodeInterface $pending_revision */
$pending_revision = $this->storage->createRevision($node, FALSE);
$draft_title = $title . ' draft';
$pending_revision->setTitle($draft_title);
$pending_revision->save();
$build = $this->viewBuilder->view($node, 'teaser');
$output = (string) $this->renderer->renderPlain($build);
$this->assertContains("title=\"$title\"", $output);
$build = $this->viewBuilder->view($pending_revision, 'teaser');
$output = (string) $this->renderer->renderPlain($build);
$this->assertContains("title=\"$draft_title\"", $output);
}
}
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