I'm new in Drupal 8 and I need to update the user when a node of a specific content type is created or updated. I found hook_entity_create but this hook acts when creating a new entity. Any solution?
0
votes
1 Answers
0
votes
There're 3 main hooks you can ustilise here:
- hook_entity_insert: for when an entity is actually created
- hook_entity_update: for when an entity is updated
- hook_entity_delete: for when an entity is deleted
Note: Beware that this hook will be called on any entity so unless you intend to make whatever operation you're performing run on every entity, do something like this:
function yourmudolename_entity_insert(Drupal\Core\Entity\EntityInterface $entity) {
if ($entity instanceof \Path\to\your\EntityInterface){
// Your code here
}
}
Or better still use the entity type hook insead.
- hook_ENTITY_TYPE_insert
- hook_ENTITY_TYPE_update
- hook_ENTITY_TYPE_delete
See here: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21entity.api.php/function/hook_ENTITY_TYPE_insert/8.5.x for details.