How to execute an action in drupal after each save node?

I am developing Action in Drupal, which should be activated after saving the node, exporting the content in XML (which includes the data from the node that was just saved), using "Trigger: after saving the updated post".

Unfortunately, this action does occur right before the information from the recently saved message is stored in the database. i.e. looking at XML later, I found that the last change I made was not included. Saving after editing another node will lead to the restoration of previously missing data.

How can I start my action after the save process is complete?

+6
triggers drupal drupal-6
source share
2 answers

In this context, there is a general error, regardless of whether you use the trigger or Mike Manrose's suggestion via hook_nodeapi() (+1):

As long as your export logic runs on the same page loop that handled the update and uses node_load() to retrieve the node data, node_load() can return a statically cached version of node from before the update, which does not yet contain any changes . If this is a problem in your case, you can work around it in two ways:

  • node_load() node static cache reset by passing TRUE as the third parameter to node_load() . This would ensure that the node is populated only from the database (at the price of some extra db requests, so keep in mind the potential performance impact).
  • If you follow the hook_nodeapi() route, you can avoid the need to call node_load() altogether if you pass in the $node object that is available directly to your export function, as this will represent the updated state.
+6
source share

You must use hook_nodeapi and activate the action when inserting and updating. Check out the documentation for hook_nodeapi for other instances where you could name your export logic.

example, where module name = 'export_to_xml':

  /** * Implementation of hook_nodeapi(). */ function export_to_xml_nodeapi(&$node, $op, $a3, $a4) { if ($op == 'update' || $op == 'insert') { export_logic_function(); } } 
+5
source share

All Articles