operator value is always "load" in nodeapi
yozdog - July 7, 2009 - 09:01
I've written a module using CCK fields. When drupal invokes the hook_nodeapi function that I've written the $op value is always set to "load" regardless of whether I'm inserting, viewing, or editing a new node. How can I can discriminate what operation is actually being requested? Thanks in advance.

Use switch()
There are a few different ops that run on hook_nodeapi. For example, in a page view you might get load/view/alter. The best way to discover what's running is to insert
echo "$node->title $op <br />";as the first line of your hook_nodeapi(). Then load a few different types of pages (view, edit, etc). You'll see that hook_nodeapi() runs multiple times.Here's an example from a page view:
Welcome! load
Welcome! view
Welcome! alter
And this is from an edit page:
Welcome! load
Welcome! prepare
You'll need to use switch() statement(s) to determine when to perform operations on your data. Here is some code from a module I'm working on:
function newsletter_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
switch ($node->type) {
case 'member':
subscriber_node_member($node, $op);
break;
}
}
function subscriber_node_member($node, $op) {
if ($node->type == 'member') {
switch($op) {
case 'prepare':
variable_set('member_email', member_email($node));
break;
case 'insert':
case 'update':
subscriber_update($node, $op);
variable_del('member_email');
break;
}
}
}
http://w5pc.org
psychic debugging
It's best to post relevant code when you have a code question. The following is based on an educated guess.
The comparison operator is == not =
<?php
// Oops, assignment, not comparison: $op will always be 'load'.
if ($op = 'load') {
}
else if() {}
// Should be:
if ($op == 'load') {
}
else if() {}
// Or use a switch
switch ($op) {
case 'load':
//...
break;
case 'insert':
//...
break;
}
?>
--
The Manual | Troubleshooting FAQ | Tips for posting | How to report a security issue.
Crikey I don't know how I
Crikey I don't know how I missed that!!! You saved me pulling my hair out through frustration
<?phpfunction gbif_vocabulary_nodeapi(&$node, $op, &$form, $page) {
if($op = 'view'){ //<----Much stupidity here should have been $op == 'view'
$teaser = $form;
}
//...
}
?>
Manipulating Nodes with
Manipulating Nodes with hook_nodeapi()
The preceding hooks are only invoked based on the module key of the module’s hook_
node_info() implementation. When Drupal sees a blog node type, blog_load() is called.
What if you want to add some information to every node, regardless of its type? The hooks
we’ve reviewed so far aren’t going to cut it; for that, we need an exceptionally powerful hook:
hook_nodeapi().
This hook creates an opportunity for modules to react to the different operations during
the life cycle of any node. The nodeapi() hook is usually called by node.module just after
the node-type–specific callback is invoked. For example, first joke_insert() might be
called, then immediately the nodeapi hook would be called with $op set to insert. Here’s
the function signature:
hook_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL)
The $node object is passed by reference, so any changes you make will actually change
the node. The $op parameter is used to describe the current operation being performed on the
node, and can have many different values:
• prepare: The node formis about to be shown. This applies to both the node Add and
Edit forms.
• validate: The user has just finished editing the node and is trying to preview or submit
it. Here, your code should check to make sure the data is what you expect and should
call form_set_error() if something is wrong; that will return an error message to the
user. You can use this hook to check or even modify the data, though modifying data in
the validation hook is considered bad style.
• presave: The node passed validation and will soon be saved to the database.
• insert: A new node has just been inserted into the database.
• update: The node has just been updated in the database.
• delete: The node was deleted.
• delete revision: A revision of a node was deleted. Modules will respond to this if they
are keeping data related to the revision. The node ID can be found at $node->nid, and
the revision ID can be found at $node->vid.
• load: The basic node object has been loaded from the database, plus the additional
node properties set by the node type (in response to hook_load(), which has already
been run; see “Modifying Nodes of Our Type with hook_load()” earlier in this chapter).
You can add new properties or manipulate node properties.
• alter: The node’s content has gone through drupal_render() and been saved in
$node->body (if the node is being built for full view) or $node->teaser (if the node is
being built for teaser view), and the node is about to be passed to the theme layer.
Modules may modify the fully built node. Changes to fields in $node->content should
be done in the view operation, not this operation.
• view: The node is about to be presented to the user. This action is called after
hook_view(), so the module may assume the node is filtered and now contains HTML.
Additional items may be added to $node->content (see how we added a joke punch line
previously, for example).
• search result: The node is about to be displayed as a search result item.
• update index: The node is being indexed by the search module. If you want additional
information to be indexed that isn’t already visible through the nodeapi view operation,
you should return it here (see Chapter 12).
• prepare translation: The node is being prepared for translation by the translation
module. Modules may add custom translated fields.
• rss item: The node is being included as part of an RSS feed.
The last two parameters to a hook_nodeapi() function are variables whose values change
depending on which operation is being performed. When a node is being displayed and $op is
alter or view, $a3 will be $teaser, and $a4 will be $page (see node_view() in node.module).
update index not being called
I'm trying to write a function that alters the data indexed by some of my content types, basically, hiding some cck fields and adding values from referenced nodes.
The modified the first line of the function to be $node->body.=" op:".$op;
When I view a cck node I can see:
op:presave op:update op:presave op:load
[content]
op:alter
But when I query the search_dataset I don't see any of my modifications and I see:
title op presave op update op presave op load [content]
I was expecting a update index there.
What am I doing wrong?
Any advice snippet or module to accomplish this?
~~ http://josep.valls.name