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.