Score:0

Do something when a content is saved

lc flag

I'm a total beginner in module coding and i'm trying to adapt a simple D7 module which is updating a field from a content type from an external API when the content is created or modified.

On an fresh D9 install, i've created a module named : edu_module

Created a folder /custom/edu_module in the module directory

Created a edu_module.info.yml

Activated the module => OK

and at last created an edu_module.module file with this code :

use Drupal\Core\Entity\EntityInterface;

function edu_module_entity_update(Drupal\Core\Entity\EntityInterface $entity) {
  \Drupal::logger('edu_module')->alert('page updated by me');
  \Drupal::messenger()->addMessage('Page has been updtaed by me: ' . $entity->title->value);
}

And nothing happen. I've tried two different way to obtain information (logger & messenger) but i have no results. I think hook is probably not fired but why not ? Can anyone help ?

Thanks for your help.

cn flag
After adding a hook to a module, you need to clear the Drupal cache at least once for the hook to start working.
smeys avatar
lc flag
Thanks, i feel a bit stupid :) I've done multiple try and cache what not cleared on this last one.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.