By jdesrig on
The node_api hook provides a bunch of triggers which allows you to customize what might happen on, for example, a presave. The node_api presave opcode triggers an action to occur after clicking on the node 'save' button but BEFORE the information in the node form is inserted into the database. I would like to create a trigger which will be a post-save trigger, to trigger my custom code after clicking the save button but AFTER the node info has been saved into the database. Does anyone have any ideas of how I might create such a trigger?
(I have read the documentation about how to create a trigger, I just don't know to code the trigger I am describing above.)
Comments
There are already cases for
There are already cases for this where $op equals 'insert' or 'update'.
I know that these two op's
I know that these two op's exist, but for some reason they aren't doing the job. I posted my code at http://drupal.stackexchange.com/questions/30402/post-save-case-in-node-api
Update doesn't do anything different than presave, and when I tried insert nothing happened at all when I pressed the save button. Thats why I want to know how to write the trigger myself and hopefully that will fix the problem.
It is not clear what your
It is not clear what your issue is.
'presave' allows you to act on the data about to be saved, as has been noted nid will not be set for new nodes.
'insert' and 'update' allow you to take additional actions after the node has been created/updated. It is generally used to store data in additional tables.
You code since it sends an email and depends on the node id would normally use the insert and update cases.
Implementing
Implementing hook_node_update() does what you need.
edit: sorry this implies for drupal 7...
A couple suggestions
Hi,
I agree with the other comments. The logical operation of your requirements occur during the node_api (insert and update) operations.
If you tried to perform your database query in "presave', I am pretty sure there is no node id (nid) at that point. Generally the node id is generated by the database. If the operation is "presave", the nid has not been generated yet. Your SQL requires a node id (nid).
Don't take this the wrong way, but I've learned to never assume anything. If I am correct and you tried to invoke your code without a node id, that indicates you aren't inspecting the intermediate results. In other words, you didn't verify the your SQL select returned any results. If that's true, I would advise verifying the all the intermediate steps. I never assume that my code is being called or an exception has not been thrown.
Hope that makes sense. If there is a logic error in your existing code, calling it from a different mechanism is not going to fix any existing logic problems. You are going to gain much more if you first verify your existing logic :)
I always use a debugger to trace my code. That's always my advice, do the same. If you are adverse to using a debugger, try the print screen method. It barely better than nothing, but is better than nothing :)
Hope that helps (a little).
Good Luck
See
See http://janezurevc.name/strange-behaviour-race-condition-during-nodesave
Use register_shutdown_function
Use the register_shutdown_function, such as:
doesn't work for me
Interesting approach but this doesn't work for me. I used the register_shutdown_function to register a function that generates a lengthy PDF, which takes a considerable amount of time (approx. 40 seconds). When I save the node, it cycles for the length of time it takes to generate the PDF b/f the node is actually saved, so it obviously isn't waiting for the hook_node_update function to go out of scope before calling my registered function. Not sure why it's not working. PHP manual says that the function "Registers a callback to be executed after script execution finishes or exit() is called." I'm not sure what "script execution" means in this context.
It's been a while since I've
------
Contact me to contract me for D7 -> D10/11 migrations.