3

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
10

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();
| improve this answer | |
6

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());
| 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.