Extra Block Types (EBT) - New Layout Builder experience❗

Extra Block Types (EBT) - styled, customizable block types: Slideshows, Tabs, Cards, Accordions and many others. Built-in settings for background, DOM Box, javascript plugins. Experience the future of layout building today.

Demo EBT modules Download EBT modules

❗Extra Paragraph Types (EPT) - New Paragraphs experience

Extra Paragraph Types (EPT) - analogical paragraph based set of modules.

Demo EPT modules Download EPT modules

Scroll

How to get current revision in node translation?

How to get current revision in node translation?
, by

How to get current revision in node translation?

1 answer
votes: 1673
Answer

To get the current revision of a node translation in Drupal, you can use the translation_revert_revision() function. This function takes two parameters: the first is the node object, and the second is the language code of the translation that you want to retrieve.

Here's an example of how to use this function:

// Load the node object for the current node.
$node = \Drupal::routeMatch()->getParameter('node');

// Get the current translation revision of the node.
$current_revision = \Drupal::service('content_translation.manager')->getTranslationLatestRevision($node->getEntityTypeId(), $node->id(), $node->language()->getId());

// Load the node revision object.
$node_revision = \Drupal::entityTypeManager()->getStorage('node')->loadRevision($current_revision);

// Do something with the node revision object.

In this example, we first load the node object for the current node using the \Drupal::routeMatch()->getParameter('node') function. We then use the getTranslationLatestRevision() function to get the current revision for the translation of the node in the current language. Finally, we load the node revision object using the loadRevision() function and do something with it.

Note that this code assumes that you have the Content Translation module enabled and that your node has translations available.