I have a hierarchy of Entities derived from ContentEntityBase. Now I want to create a log when the publishing status ( = status field) changes.

What is the best method to override/implement for this purpose? In the Entity that extends ContentEntityBase or in the Form that extends ContentEntityForm?

Or somewhere else? I don't want to use a hook!

Thanks. Rainer.

  • Just a quick note: please use "extends" instead of derived. That's the right word in OO world.
    – ssibal
    Commented Jun 28, 2016 at 7:41
  • "extends" is prevelant as a term because a few popular languages (java/php/etc) use it @ssibal; "derived" is also very commonly used when talking about inheritance, possibly even more so in my experience. There's no technical/conventional reason to use one over the other, they mean the same thing. techopedia.com/definition/3780/derived-class for example
    – Clive
    Commented Jun 28, 2016 at 8:03
  • Okey! I agree. I was too subjective! For me "extends" is more accurate and commonly used in php language for inheritance!
    – ssibal
    Commented Jun 28, 2016 at 8:06
  • Did you try rules module?
    – Jimmy Ko
    Commented Jun 28, 2016 at 10:01

2 Answers 2


I would like to go for hooks (maybe events), hook_ENTITY_TYPE_update. You can find more about it in details here.

You only have to check if "status" field is being changed after update, and using the logging factory to put it into logs. This is also "form" independent (means it will run each time when entity gets updated), which is more general approach, and better for your system (more consistency).


Thanks ssibal, with your hint I found an answer. In the hook page on drupal.org I found the dirty fact: "Get the original entity object from $entity->original."

So I implemented the hook, set a breakpoint and found that stack:

proreos/preosanwalt/preosanwalt.module.preosanwalt_anwalt_entity_update(): lineno 110   
proreos/core/lib/Drupal/Core/Extension/ModuleHandler.php.call_user_func_array:{proreos/core/lib/Drupal/Core/Extension/ModuleHandler.php:402}(): lineno 402  
proreos/core/lib/Drupal/Core/Extension/ModuleHandler.php.Drupal\Core\Extension\ModuleHandler->invokeAll(): lineno 402   
proreos/core/lib/Drupal/Core/Entity/EntityStorageBase.php.Drupal\Core\Entity\EntityStorageBase->invokeHook(): lineno 167    
proreos/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php.Drupal\Core\Entity\ContentEntityStorageBase->invokeHook(): lineno 418  
proreos/core/lib/Drupal/Core/Entity/EntityStorageBase.php.Drupal\Core\Entity\EntityStorageBase->doPostSave(): lineno 470    
proreos/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php.Drupal\Core\Entity\ContentEntityStorageBase->doPostSave(): lineno 304  
proreos/core/lib/Drupal/Core/Entity/EntityStorageBase.php.Drupal\Core\Entity\EntityStorageBase->save(): lineno 395  
proreos/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php.Drupal\Core\Entity\Sql\SqlContentEntityStorage->save(): lineno 747  
proreos/core/lib/Drupal/Core/Entity/Entity.php.Drupal\Core\Entity\Entity->save(): lineno 358    
proreos/core/lib/Drupal/Core/Entity/EntityForm.php.Drupal\Core\Entity\EntityForm->save(): lineno 283    
proreos/proreos/src/Form/ProreosEntityFormBase.php.Drupal\proreos\Form\ProreosEntityFormBase->save(): lineno 81 

Going backward from that I found that EntityStorageBase is setting that dirty original right before it calls the Entity's preSave method.

So, the correct approach to do it without a hook in my EntityBase is this:

  public function preSave(EntityStorageInterface $storage) {

       $org = $this->original;
       if ($org->isPublished() != $this->isPublished()) {
         proreos_auditinfo("Publish Status geändert, ".$this->getAuditString());

I think we should have a documented API call here in EntityInterface instead of that dirty "original" property - I'll file that as feature request at drupal.org.

Thanks. And btw. I don't like the hook system anymore because I don't like to mix that procedural approach with my OO code.

Your Answer

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

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