How to publish latest revision langcode programmatically in each translation

id flag
  1. My workflow is draft, publish and archived.
  2. My translations are 'en(default)', 'fr', and 'de'.
  3. My clients create a lot of nodes in draft status for their release.
  4. We must release these latest revisions in these nodes.
  5. Each nodes has some paragraph field.

I try the following code on drush eval, but "de" translations are changed as first revision in paragaraph field.

$langcode = "fr";
$nid = 12345;
// node loaded
$node = \Drupal\node\Entity\Node::load($nid);
$node = $node->getTranslation($langcode);
    // node storage
    $storage = \Drupal::entityTypeManager()->getStorage($node->getEntityTypeId());
    // latest translation revision id
    $revision_id = $storage->getLatestTranslationAffectedRevisionId($nid, $langcode);
    $revision = $storage->loadRevision($revision_id);
      $revision = $revision->getTranslation($langcode);
      $status = $revision->moderation_state->value;
      $is_draft = $status == "draft";
        $revision->set('moderation_state', 'published');

How to publish latest revision langcode programmatically in each translation.



Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.