Good evening, I'm writing a custom module, with a call to hook_nodeapi (on php 5.3, latest drupal 6) I update some cck fields, but looking at the saved node page, these fields are empty. Seems that after the hook, cck fields lose their values. function mymod_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL){ switch($op){ case "insert": case "update": my_fill_cck( &$node ); // here cck are populated // krumo( $node ); break; case "load": // viewing node, cck are empty :( // krumo( $node ); return array(); break; } } Any idea is really appreciated. Thank in advance. Best regards, Luca
Good morning Luca, 'insert' and 'update' ops are fired *after* the node has been saved to the database. To make changes to the node before it is saved, you should make adjustments in 'presave'. - Grayside On Mon, Oct 4, 2010 at 7:50 AM, luca capra <luca.capra@gmail.com> wrote:
Good evening,
I'm writing a custom module, with a call to hook_nodeapi (on php 5.3, latest drupal 6) I update some cck fields, but looking at the saved node page, these fields are empty.
Seems that after the hook, cck fields lose their values.
function mymod_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL){ switch($op){ case "insert": case "update":
my_fill_cck( &$node );
// here cck are populated // krumo( $node ); break; case "load": // viewing node, cck are empty :( // krumo( $node ); return array(); break; } }
Any idea is really appreciated. Thank in advance.
Best regards, Luca
Oops... it's right. Thank you Luca On 04/10/2010 17:14, Adam B. Ross wrote:
Good morning Luca,
'insert' and 'update' ops are fired *after* the node has been saved to the database. To make changes to the node before it is saved, you should make adjustments in 'presave'.
- Grayside
On Mon, Oct 4, 2010 at 7:50 AM, luca capra<luca.capra@gmail.com> wrote:
Good evening,
I'm writing a custom module, with a call to hook_nodeapi (on php 5.3, latest drupal 6) I update some cck fields, but looking at the saved node page, these fields are empty.
Seems that after the hook, cck fields lose their values.
function mymod_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL){ switch($op){ case "insert": case "update":
my_fill_cck(&$node );
// here cck are populated // krumo( $node ); break; case "load": // viewing node, cck are empty :( // krumo( $node ); return array(); break; } }
Any idea is really appreciated. Thank in advance.
Best regards, Luca
Le lundi 04 octobre 2010 à 16:50 +0200, luca capra a écrit :
Good evening,
I'm writing a custom module, with a call to hook_nodeapi (on php 5.3, latest drupal 6) I update some cck fields, but looking at the saved node page, these fields are empty.
Seems that after the hook, cck fields lose their values.
function mymod_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL){ switch($op){ case "insert": case "update":
my_fill_cck( &$node );
// here cck are populated // krumo( $node ); break; case "load": // viewing node, cck are empty :( // krumo( $node ); return array(); break; } }
Any idea is really appreciated. Thank in advance.
Best regards, Luca
Spotted syntax error: my_fill_cck( &$node ); Forcing reference at call time is deprecated already with PHP 5 (if I remember well), and using PHP 5.3 could lead the interpreter to ignore the function call. The same for the hook(), if your module is for PHP >=5.2, don't write the & in the function signature, $node is an object therefore it's been passed by reference anyway, using & in method signature makes it being a pointer, which means that some module write $node = NULL will change the pointer value (not the object itself) and it may break the execution flow after. Adam was right, use presave to ensure your data is being modified after the real save. Or you also could ensure your module has a lower weight than the content module, to ensure content_nodeapi() being called after your own. Pierre.
Oups, some fixes: "The same for the hook(), if your module is for PHP >=5.2" I mean "PHP >= 5" "Adam was right, use presave to ensure your data is being modified after" I meant "before". Pierre.
On 04/10/2010 17:41, Pierre Rineau wrote:
Le lundi 04 octobre 2010 à 16:50 +0200, luca capra a écrit :
Good evening,
I'm writing a custom module, with a call to hook_nodeapi (on php 5.3, latest drupal 6) I update some cck fields, but looking at the saved node page, these fields are empty.
Seems that after the hook, cck fields lose their values.
function mymod_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL){ switch($op){ case "insert": case "update":
my_fill_cck(&$node );
// here cck are populated // krumo( $node ); break; case "load": // viewing node, cck are empty :( // krumo( $node ); return array(); break; } }
Any idea is really appreciated. Thank in advance.
Best regards, Luca
Spotted syntax error: my_fill_cck(&$node );
Forcing reference at call time is deprecated already with PHP 5 (if I remember well), and using PHP 5.3 could lead the interpreter to ignore the function call.
The same for the hook(), if your module is for PHP>=5.2, don't write the& in the function signature, $node is an object therefore it's been passed by reference anyway, using& in method signature makes it being a pointer, which means that some module write $node = NULL will change the pointer value (not the object itself) and it may break the execution flow after.
Adam was right, use presave to ensure your data is being modified after the real save. Or you also could ensure your module has a lower weight than the content module, to ensure content_nodeapi() being called after your own.
Pierre.
The $op presave was my problem. This is a syntax error my_fill_cck( &$node ); and this should be my hook: function mymod_nodeapi($node ... because &$node is a pointer, and what I need is a reference (that is how is passed an object ). It's right ? Interesting, I learnt something new :) Regards, Luca
On Mon, 2010-10-04 at 19:53 +0200, luca capra wrote:
The $op presave was my problem.
This is a syntax error my_fill_cck( &$node );
and this should be my hook:
function mymod_nodeapi($node ...
because &$node is a pointer, and what I need is a reference (that is how is passed an object ). It's right ? Interesting, I learnt something new :)
Regards, Luca
With PHP 4, objects where like any other values, copied when passed as function parameter, which means any modification on the local copy wouldn't be visible within the caller function. With PHP 5, you don't have to add the & operator to force the argument to pass by reference because objects are always references (as we could see in other languages such as python or java, or such). The reason why D6 still have the & operator is because it's meant to be compatible with PHP 4, but it can have really ugly side effects with PHP 5 (fortunately, I still did not encounter it with other modules, it seems that developers are for the most careful about what they are doing). But if you have a module that unset the local pointer value or replace it by anything else, it could have rather strange side effects. I did experienced it with the views module somehow doing some bad typo (views uses a lot of objects and always force the & operator everywhere to remain compatible with PHP 4). Pierre.
On 10/4/10 4:01 PM, Pierre Rineau wrote:
On Mon, 2010-10-04 at 19:53 +0200, luca capra wrote:
The $op presave was my problem.
This is a syntax error my_fill_cck(&$node );
and this should be my hook:
function mymod_nodeapi($node ...
because&$node is a pointer, and what I need is a reference (that is how is passed an object ). It's right ? Interesting, I learnt something new :)
Regards, Luca
With PHP 4, objects where like any other values, copied when passed as function parameter, which means any modification on the local copy wouldn't be visible within the caller function. With PHP 5, you don't have to add the& operator to force the argument to pass by reference because objects are always references (as we could see in other languages such as python or java, or such).
Technicality: Without the &, you are passing by value. Always. The difference is that in PHP 5 what you are passing is a handle to an object rather than the object itself (not to be confused with a pointer as one sees in C/C++, as those don't exist in PHP). So there's one object, but two variables of type "handle to get an object" that both point to the same object. It's a very subtle difference that only bites you occasionally, but when it does it bites hard. :-) The net result is the same: you need &$node to be compatible with PHP 4, but as of PHP 5 / Drupal 7 you don't want it anymore. --Larry Garfield
participants (4)
-
Adam B. Ross -
larry@garfieldtech.com -
luca capra -
Pierre Rineau