1

The module content moderation has been activated and a custom workflow added to a content type. On editing content, when selecting a content moderation state then clicking on save, I would like to programmatically save that value in a variable.

$mod in that code doesn't display the latest values for moderation state chosen on update.

I wonder if what I get is the default revision value (as configured for a state, Révision par défaut Quand le contenu atteint cet état il devrait devenir la révision par défaut; ceci est implicite pour les états publiés.) but it is definitely not the latest revision value.

function my_hook_entity_update(Drupal\Core\Entity\EntityInterface $entity) {
  if ($entity->getEntityTypeId() !== 'node') {
    return;
  }

  $content_id = $entity->id();
  $content = \Drupal\node\Entity\Node::load($content_id);       

  $mod=$content->get('moderation_state')->getValue();        
  var_dump($mod);
  die();
}

How do I get the latest value of content moderation on node update?

  • 1
    So far all you've stated are facts. What's the question? Is it "why does $mod not have a value?" – Shawn Conn Jul 10 '18 at 2:12
  • the question if unclear is how to get the latest value of content moderation on node update – Matoeil Jul 10 '18 at 7:14
  • 1
    I think what he meant is the transition towards the latest state. As in: You're editing a draft and setting the draft to published then saving, perhaps he wants the published state in the update process. – Postovan Dumitru Jul 10 '18 at 7:28
  • i have edited my question – Matoeil Jul 10 '18 at 7:35
  • 1
    I think you're getting the current revision moderation state value, what you need is to load the latest revision of the node, and then just get the moderation state value from there. – Postovan Dumitru Jul 11 '18 at 8:06
4

Getting the version of the entity meant getting the last revision and allow to get the correct value

function my_hook_entity_update(Drupal\Core\Entity\EntityInterface $entity) {    
  if ($entity->getEntityTypeId() !== 'node') {
    return;
  }

 $current_entity_lang = $entity->get('langcode')->value;
 $translated_entity = $entity->getTranslation($current_entity_lang);
 $mod = $translated_entity->get('moderation_state')->getValue());
}
| improve this answer | |

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.