1

I'm trying to compare some field values from the edit form of a node to the published/archived field values. However I'm running to an issue where when im trying to load the Node object using Drupal::entityTypeManager()->getStorage('node')->load($nid) It's returning the last saved draft revision. How do i get the Node object of the published/archived revision?

P.S. Content Moderation is turned on, and content type is assigned to a workflow.

0
1

You can do this with a direct database query in your function, then load the specific revision.

$connection = \Drupal::database();
$query = $connection->query("SELECT vid FROM {node_field_data} WHERE nid = :nid",
    [':nid' => $node_nid,]);
$node_revision_id = $query->fetchField();

$node_revision = \Drupal::entityTypeManager()
  ->getStorage('node')
  ->loadRevision($node_revision_id);

Untested code, but this should put you on the right track. Might also be worth confirming in your SQL query that "WHERE status = 1" to confirm that the node is still published.

2
  • Thanks. But this didn't work. It gave me the same node as Drupal::entityTypeManager()->getStorage('node')->load($nid) which is the draft revision and not the currently published revision.
    – Howard
    Feb 3 '20 at 15:25
  • Actually disregard my comment above this works. However, the condition to check status/moderation state is important because, a node might not have a published/archived version yet.
    – Howard
    Feb 3 '20 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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