11

I know we can use hook_entity_update() hook to do something when an entity is updated. But is there an event to listen for node updates/deletes, or other entities such for that matter?

3 Answers 3

19

You can't listen to an event. You can either use a hook or implement a method like postSave() in your custom entity.

Here is the order of hooks and other events that happen during an entity save:

2
  • So entities won't dispatch any events that I can subscribe to? I would have to use a hook?
    – John
    Commented Dec 3, 2016 at 0:10
  • 1
    Yes, many Drupal specific events are still in hooks. Eventually all hooks will be replaced, see drupal.org/node/1509164 and drupal.org/node/1972304.
    – 4uk4
    Commented Dec 3, 2016 at 13:07
7

Ideally you would listen for entity related events but these aren't ready yet. See "Add events for matching entity hooks".

For now, if you're wanting to stay away from hooks, you can use a contrib module: Hook Event Dispatcher

0
3

A node is an entity type, so hook_ENTITY_TYPE_update(.. is the same as hook_node_update(..,

function hook_node_update(Drupal\Core\Entity\EntityInterface $entity) {
    //$entity->bundle()  - 'article' 'page'
}

or

function hook_customentity_update(Drupal\Core\Entity\EntityInterface $entity{
    //$entity->bundle()  - 'awesome_entity_bundle'
}

https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!entity.api.php/function/hook_ENTITY_TYPE_update/8.2.x

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.