[Originally found here, with full credit to the original author (I've edited it a bit). Posting this here allows closing this documentation bug #155382: hook_nodeapi execution order ]

To solve a really ugly bug of the video module, I had to track down, in a very detailed way, what was the hook execution order in the Drupal 5.x hook_nodeapi. Unfortunately I didn't found any detailed documentation of this neither on the handbooks nor in the developer documentation (on CVS). This is why, armed with a lot of patience, I wrote down what happens at every content submission/previewing/loading.

Those are my results:

node submit (validation successful):
prepare form validate submit insert

node submit (validation unsuccessful):
prepare form validate

node/add/type (first visualization):
prepare form

node preview new node (validation unsuccessful & successful):
prepare form validate

node view:
load view

node edit:
load prepare form

node preview existing node:
load prepare form validate view

node submit existing node:
load prepare form validate submit update

Please note that the form operation above is not available on the hook_nodeapi. It's just mean that the form is being constructed at that time. If you need to interact with the form (eg: adding/modifying form fields) you can do it using the hook_form_alter

Question: What is the order in which the nodeapi hooks are called? For instance, if I want to change $node->content['files'], which is inserted by the upload_nodeapi, how do I make sure that upload_nodeapi has already been called when mymodule_nodeapi is called?

Answer: Yes... this is a common problem too. The system database table holds information about installed drupal modules. In this table there is a field called weight which provide a sort of execution priority between modules. Unfortunately Drupal 5 doesn't provide any sort of direct api to modify a module weight so you will need to directly write sql queries in your .install files to alter the weight values of your modules, or use the moduleweight module to add the weight setting to the module admin page.
If two modules have the same weight they are executed in alphabetical order: captcha.module is executed before video.module.

Comments

curtaindog’s picture

Potentially this could be calculated directly from the dependency graph of the modules.

RSTaylor’s picture

In Drupal 6.x, the hook_nodeapi 'submit' op has been replaced by 'presave', but the timing is slightly different:

"this op is invoked at the beginning of node_save(), in contrast to op='submit' which was invoked at the end of node_submit(). Thus 'presave' operations will be performed on nodes that are saved programmatically via node_save(), while in Drupal 5.x op='submit' was only applied to nodes saved via the node form."

jerielmari’s picture

Node delete:
load