2

I want to load the node details for a specific revision. I tried using the following code.

use Drupal\node\Entity\Node;
$nid = 1171;
$vid = 9423;
$rev_node = Node::load($nid, $vid, TRUE);

I also tried the following code.

$rev_node = node_load($nid, $vid);

It always loads the latest version, irrespective of the revision ID passed to the method/function call.

Is there a way to load the data for a specific revision?

0

3 Answers 3

10

Try with this code:

\Drupal::entityTypeManager()->getStorage('node')->loadRevision($vid);
3
  • but there is no $node_id specified in this right ??
    – KTM
    Commented May 5, 2017 at 13:30
  • Not working actually
    – KTM
    Commented May 5, 2017 at 13:49
  • 2
    There are no need of node ID because revision id is a unique id and will associate with only one node only. Commented May 6, 2017 at 4:59
2

You can use

$node = node_load($node_id);
\Drupal::entityManager()->getStorage('node')->revisionIds($node);

to get the revision IDs for a node.

You can then load a specific revision of a node with $rev = node_revision_load($rev_id);

1
  • It actually prints out large array unlike a node_load
    – KTM
    Commented May 5, 2017 at 14:55
1

Actually, entityManager() is deprecated in Drupal 8: https://api.drupal.org/api/drupal/core%21lib%21Drupal.php/function/Drupal%3A%3AentityManager/8.5.x, so, instead, you could use entityTypeManager():

\Drupal::entityTypeManager()->getStorage('node')->loadRevision($revision_id);

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