9

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?

17

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:

3
  • So entities won't dispatch any events that I can subscribe to? I would have to use a hook?
    – John
    Dec 3 '16 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.
    – 4k4
    Dec 3 '16 at 13:07
  • Great answer. Didn't realize hook_entity_update doesn't cover new objects. There's no hook for new or updated, right? I'd have to create another function and make both hooks call the one function. Jul 27 '18 at 18:59
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

1
  • 4
    I thought I was adding something to the discussion. If you vote down please leave a comment to explain why. This can help me improve my answers. Thanks.
    – imclean
    Oct 19 '18 at 11:12
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, privacy policy and cookie policy

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