Hi everybody,

I have a module which create nodes of type "mytype" which has a field
called "something" accessible from $node->something.
I'm working on another module (mytype_change.module) which need to
modify one of the "mytype.module" field.
I want to change the "something" field of the just submitted node to
something different that the user input.
If the user insert 'foo' as value of the something field I want that
my mytype_change.module can change the value to 'bar' just before the
node insertion on database.

So while the user thinks that he inserted 'foo' as value of the
something field instead the system has registered 'bar' into the database.

I'm doing this through the hook_nodeapi where $op param is 'validate'.
then I do something like:

function mytype_change_nodeapi(&$node, $op, $teaser) {
  switch ($op) {

    case 'validate':
      if($node->something == 'foo')
        $node->something = 'bar';
      break;
  }
}

But the code above does not work.
The something value is always 'foo' even if I submit or preview the node.

Can someone point me to the right direction???

Thank you.

Fabio Varesano

Comments

Answer is simple

...solution a bit harder.

the $node object passed to this hook has only the columns of the node table. Therefore, it does not have a something field.

So what you have to do is use the $node->nid to look up your custom table and change the field there.

actually the

actually the $node->something variable is defined and passed to the hook.
(I'm able to print $node->something)

But if I change the $node->something (as in my first post) value it is not changed.

Instead the documentation tell me that I'm able to modify a value:

"validate": The user has just finished editing the node and is trying to preview or submit it. This hook can be used to check or even modify the node. Errors should be set with form_set_error().

------------
www.varesano.net - Fabio Varesano Personal Homepage

------------
www.varesano.net - Fabio Varesano Personal Homepage

with cck?

How would one do this when operating on a cck-defined node type?

I'm having the same problem!

I have a module that's supposed to change $node->body on form submit.
In 4.6, I used nodeapi validate, and it worked just fine.
Now I'm upgrading to 4.7, and using nodeapi validate doesn't work. I even tried using nodeapi execute, which doesn't work also, and according to http://api.drupal.org/api/4.7/function/hook_nodeapi:

"execute": The node passed validation and will soon be saved.

A solution would be greatly appreciated.

-------------------
Alon Peer
Web development
http://alonpeer.com

the hook_execute doen't

the hook_execute doen't exists anymore.
Use hook_submit (not in documentation).

The problem (for me) is that hook_submit will do the task
only on submit, not on previews.

------------
www.varesano.net - Fabio Varesano Personal Homepage

------------
www.varesano.net - Fabio Varesano Personal Homepage

the documentation is inaccurate

I've read through the 4.6 and 4.7 node modules and it appears that, while one could modify a $node in hook_validate() or the 'validate' part of hook_nodeapi() in 4.6, this ability is lost in 4.7, despite what the documentation says. What I've been doing for lack of a better idea is, for example, to add a value to a node:

global $form_values;
$form_values['new_value'] = 'I'm new.';

Obviously this is not a good solution. Any suggestions from developers of the 4.7 Forms API on how this should really be done would be greatly appreciated.

form_set_value()

if you really must, you can call form_set_value() from a validate() hook. however, in principle, validate() is to check values, and submit/insert/update is for setting them.

__________________________________________________________________
My professional services are available through 3281d Consulting

When I call form_set_value()

When I call form_set_value() the post-processing form values (ie, the ones that go into the DB) are changed, but the values displayed to the user in the form are not changed. Do I need to do something to get the altered values to be displayed in the form fields on a preview?

Also, if the validate() hook is not the place to filter submitted values, what is? I consider input filtering an essential form API feature that should be easy to use.

Impossible to recover from an error?

It seams that the present implementation makes it impossible for nodeapi to do anything about an error.

For example, I would like to make a function that autogenerates the title, if the user leaves it blank (quite like how it is done for comments). It is possible to generate a new title and insert in in the correct location in nodeapi/'submit' (or in nodeapi/'validate', using some tricks mentioned here).

Problem is that the error is set in an static array inside the form_set_error function. As far as I can see there is no way to remove an error once it's been added there. And therefore there is no way to "recover" from an empty title field.

An solution would be to add a new "op" that is called before 'validate'.

(See also http://drupal.org/node/73186 )

Solved

I found a better solution for my particular problem: http://drupal.org/node/73186#comment-136398

For Drupal 5

I was having the same problem in Drupal 5. Based on what I read over at http://drupal.org/node/27007, it looks like node_save() is called before insert/update hooks. I think node_save() should be called after, but since I haven't investigated this enough, I won't consider it a bug (yet). Maybe there's a good reason for it?

It really seems like calling node_save() multiple times is excessive.

Anyway, it looks like you've got to do something like so, it worked for me:

$node->field_foo[0]['value'] = $bar;
node_save(node_submit($node));

use case 'submit' instead

changes made to $node in the $op=='validate' phase won't stick... make your changes in the $op=='submit' phase instead.

this seems to trip up a lot of folks, me included!

here's another case:
http://drupal.org/node/133038#comment-640753

The API docs for 5 are a little more explicit in hook_nodeapi's explanation than is 4.7, but the thing to remember regardless is that you should only validate in the 'validate' phase, and then go about with your changes in the 'submit' or 'insert' phases.

--
Matt J. Sorenson (emjayess)
d.o. | g.d.o. | twitter