0
votes

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?

1

1 Answers

0
votes

There're 3 main hooks you can ustilise here:

  1. hook_entity_insert: for when an entity is actually created
  2. hook_entity_update: for when an entity is updated
  3. 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.

  1. hook_ENTITY_TYPE_insert
  2. hook_ENTITY_TYPE_update
  3. 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.