Act on nodes defined by other modules.

Act on nodes defined by other modules.

Despite what its name might make you think, hook_nodeapi() is not reserved for node modules. On the contrary, it allows modules to react to actions affecting all kinds of nodes, regardless of whether that module defined the node.

It is common to find hook_nodeapi() used in conjunction with hook_form_alter(). Modules use hook_form_alter() to place additional form elements onto the node edit form, and hook_nodeapi() is used to read and write those values to and from the database.


kiamlaluno’s picture

Drupal 7 doesn't use anymore hook_nodeapi(), but it uses a set of new hooks called hook_node_$op(), where $op is the parameter passed to hook_nodeapi() in Drupal 6. See Converting 6.x modules to 7.x for more details.

alexmoreno’s picture

more than the parameter (which is 100% true) the function now contains the operation. That's it, update, save, etc...


martin_q’s picture

Remember that search indexing needs to see the whole node, so the node is loaded and hook_nodeapi is called with $op = 'view' for every node on the site during indexing. If you have redirects this could completely cripple cron, and even if you don't it could cause unwanted side effects.

To ensure that you only respond to occasions where the node is actually being shown in full on a user's screen, you can examine $a4 to see if it is TRUE, or include this check (via http://drupal.org/node/286263 ) :

  if (arg(0) == 'node')
NancyDru’s picture

In all likelihood, there is a $node->build_mode for that.

Daniel Norton’s picture

Where it reads “[t]he node is about to be loaded from the database” is incorrect. When the hook is invoked, the node has already been loaded from the database and the related fields have been populated.

tmsimont’s picture

when $op == "insert"
to say "the node is being inserted" is not quite true if you are altering a node that was created outside of your module.
the node has already been inserted into the database if the node you are altering at any point invoked hook_insert().

gmarcotte’s picture

I dont think the insert is completely done at this point, if I try to query a node with a join on any CCK fields I get no results. the CCK fields have not been inserted yet. Node fields are there.

Not sure what the order or the inserts are. what hook to use after all inserts from all modules are finished.

ron.astin@bluevolt.com’s picture

I'm still testing this out but I came to this page with the strong suspicion this was true. The caveman in me was about to rail against nodeapi but I realize all of my code that effects the node is working and any that relies on cck content for the node is failing on $op = insert. Changing weight of the module so that it fires after CCK may be an option, assuming CCK relies on nodeapi for its actions. More to come soon I hope....

fgm’s picture

Note that hook_nodeapi() is only invoked with $op == 'validate' when submitting data using the node form, which is why the $form parameter is available.

It is not invoked when saving data from code.