[development] Trying to understand hook_nodeapi() invocation sequence from node.module

Syscrusher scott at 4th.com
Thu Nov 24 16:20:23 UTC 2005

Good morning!

I've run into some odd behavior from node.module in Drupal HEAD (updated as of
2005-11-23 at about 2300 UTC). I'm not reporting an "issue" on the module yet,
because I strongly suspect the "problem" is ignorance on my part rather than
a bug in the core code.

I have a module that adds extra fields to node edit screens, similar to the
way the upload.module does. The difference is that my module can have zero,
one, or more instances of my custom fields, and I've got code that examines
how many are defined and always ensures that all the existing ones are shown
plus one blank one, on each preview or initial entry into the Edit page for
an existing node.

The particulars of my code aren't really relevant, and I'm not asking for help
debugging that. Instead, what I'm seeking is to understand why hook_nodeapi()
is being invoked in the particular sequence that I'm seeing. I have the following
trace enabled in my hook_nodeapi() implementation:

function mymodule_nodeapi(&$node, $op, $teaser=NULL, $page=NULL) {
  // DEBUG: Extra trace logic
  drupal_set_message("nodeapi $op $node->nid");
  switch($op) {
     // .... the usual stuff here

Now, on initial entry into the edit form on node with $nid == 8, here is the
output of that drupal_set_message():

nodeapi load 8
nodeapi load 8
nodeapi prepare 8
nodeapi form 8

And on a "preview" of the node after editing, I see this:

nodeapi load 8
nodeapi load 8
nodeapi prepare 8
nodeapi form 8
nodeapi validate 8
nodeapi view 8
nodeapi validate 8

My question is, why is $op=='load' being always invoked twice, and why is
$op=='validate' being invoked twice for previews?

The problem I'm having with *my* logic has to do with that second invocation
of "load", and I'm finding that I have to build explicit conditional logic in
the load function to detect whether I've already done it before. It seems
to me that represents extra database hits, if nothing else, and in my
particular case a design obstacle as well.

I've checked the documentation for hook_nodeapi(), but it doesn't seem to be
fully updated for 4.7 yet, and also doesn't really talk about the *sequence*
of calls to the function, but rather treats each one individually.

Can someone offer some general commentary on how this is supposed to work in
4.7? I suspect I'm not the only one who's confused. Or if I've overlooked it
in the docs, please point me to a page, and I'll cheerfully apologize for
wasting your time. I did try to RTFM before asking on list, but I may have
missed something.

Many thanks!


Scott Courtney     Drupal user name: "syscrusher"   http://drupal.org/user/9184
scott at 4th dot com       Drupal projects: http://drupal.org/project/user/9184
Sandbox:  http://cvs.drupal.org/viewcvs/drupal/contributions/sandbox/syscrusher

More information about the development mailing list