5

i actually need it for the content_moderation_state entity

 function hook_entity_presave(Drupal\Core\Entity\EntityInterface $entity) {
if ($entity->getEntityTypeId() !== 'content_moderation_state' ) {
    return;
}    
$moderation_state = $entity->get('moderation_state')->getString();

 //code to get the original entity moderation state

2 Answers 2

20

Use $entity->original to get the original entity:

function hook_entity_presave(Drupal\Core\Entity\EntityInterface $entity) {

  if ($entity->getEntityTypeId() !== 'content_moderation_state' ) {
    return;
  }    

  $moderation_state = $entity->get('moderation_state')->getString();

  //code to get the original entity moderation state
  $moderation_state_original = $entity->original->get('moderation_state')->getString();
10

If for some reason the original property isn't available(in my case in the save() method of a custom content entity) you can get it as

$entityOriginal = \Drupal::entityTypeManager()
  ->getStorage('my_entity')
  ->loadUnchanged($entity->id());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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