I had been trying a lot to display latest revision into view tab irrespective of its state, so if a node is in "Draft" state I still want it to be displayed under "View" Tab rather than "Latest version". I had tried using hook_ENTITY_TYPE_view, hook_ENTITY_TYPE_view_alter and even tried in template_preprocess_node hook but no luck. I was able to load latest revision but not able to set it using below code in preprocess,

function mytheme_preprocess_node(&$variables) {
  if ($variables['node']->getType() == 'news' && $variables['elements']['#view_mode'] = 'full') {
    $node = $variables['elements']['#node'];
    $vid = \Drupal::entityTypeManager()->getStorage('node')->getLatestRevisionId($node->id());
    $node = \Drupal::entityTypeManager()->getStorage('node')->loadRevision($vid);
    $variables['elements']['#node']  = $node;

Also tried with below code from a custom module,

function mymodule_node_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
  $node = Node::load($build['#node']->id());

  if ($entity->bundle() == 'article' && $view_mode = 'full') {
    $revision_ids = \Drupal::entityTypeManager()->getStorage('node')->revisionIds($node);
    $latest_rev_id  =  end($revision_ids);
    $node_new = node_revision_load($latest_rev_id);
    $view_builder = \Drupal::entityTypeManager()->getViewBuilder($entity->getEntityTypeId());
    $storage = \Drupal::entityTypeManager()->getStorage($entity->getEntityTypeId());
    //$node = $storage->load($build['#node']->id());
    $build_new = $view_builder->view($node_new, $view_mode);
    //$output = render($build);
    $build['#node'] =  $build_new['#node'];                 


Please if anyone can point me in a right direction it will be very helpful. Thanks in advance.

1 Answer 1


Finally after spending huge amount of time on this, I am able to figure it out. Thanks to shawn-conn's answer here - Take over display of a content type node route I have created a custom module and have added below files and code to achieve this,

First created mymodule.services.yml and added below code,

    class: Drupal\mymodule\Routing\RouteSubscriber
      - { name: event_subscriber }

Then created Routing folder under src. Added file named RouteSubscriber.php with below code,


namespace Drupal\mymodule\Routing;

use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;

 * Listens to the dynamic route events.
class RouteSubscriber extends RouteSubscriberBase {

   * {@inheritdoc}
  public function alterRoutes(RouteCollection $collection) {

    // Alter the canonical node route to our custom route
    if ($route = $collection->get('entity.node.canonical')) {
      $route->setDefault('_controller', '\Drupal\mymodule\Controller\NodeRedirectController::view');

Then added NodeRedirectController.php under src/Controller directory with below code,


namespace Drupal\mymodule\Controller;

use Drupal\Core\Entity\EntityInterface;
use Drupal\node\Controller\NodeViewController;

 * Custom node redirect controller
class NodeRedirectController extends NodeViewController {

  public function view(EntityInterface $node, $view_mode = 'full', $langcode = NULL) {
    // Display latest revision content in view mode
    if ($node->bundle() === 'my_content_type' && $view_mode == 'full') {
      $vid = \Drupal::entityTypeManager()->getStorage('node')->getLatestRevisionId($node->id());
      $node_new = \Drupal::entityTypeManager()->getStorage('node')->loadRevision($vid);
      return parent::view($node_new, $view_mode, $langcode);
    // Otherwise, display as per Drupal's default behaviour
    else {
      return parent::view($node, $view_mode, $langcode);

This might help someone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.