Post-save callback?

Moon picture Moon · Mar 15, 2011 · Viewed 22.1k times · Source

How do I execute a function every time a node is being updated? I tried to use hook_nodeapi, but it looks like that the hook is removed from Drupal 7.

What I want to do is,

  1. add a new field to my content type called 'main image (bool)' to let the admin set a main image.
  2. I am going to use Views module to display nodes, but order nodes by 'main image' field and added date.
  3. so that the first node is always the main image.

to do this, I need to make sure that there is always only one node with 'main image' set to true.

Answer

Sina Salek picture Sina Salek · Jun 4, 2014

Currently Drupal core does not offer any hook to do actions after a node/entity is inserted/updated/deleted in Database. For example, you can not send an email mentioning the node after the node is inserted because Drupal uses SQL transactions and the node is not yet fully written to database when hook node presave is called so if for any reason the transaction is rolled back, users will receive a false mail.

So Hook Post Action module introduces several new Drupal hooks to overcome this limitation:

  • hook_entity_postsave
  • hook_entity_postinsert
  • hook_entity_postupdate
  • hook_entity_postdelete
  • hook_node_postsave
  • hook_node_postinsert
  • hook_node_postupdate
  • hook_node_postdelete

https://drupal.org/project/hook_post_action