Posted by ronyon on March 6, 2008 at 7:56am
I'm getting ramped up with Drupal and finding it tricky fitting the 'key in the lock' for hooks. One area we're running into trouble is CCK. It's a really useful module for creating custom node types but there doesn't seem to be an easy way to hook node actions.
We need to be able to send email or populate tables, etcetera if a nodes are hit or forms are submitted. Using mytheme_myformid_submit() on a CCK node kept the CCK handler from picking up the form. Assigning a function with hook_form_alter also failed.
I would like to find some advise on where I can grab all node views or submissions... What's the right syntax for hook_nodeapi???
Thanks
Comments
All of the drupal calls are
All of the drupal calls are rather well documented in the API database.
See http://api.drupal.org/api/function/hook_nodeapi/5 for information about hook_node_api (assuming that you're using D5, but the 4 & 6 versions are there too).
Since you referenced the theme, I'm guessing you're trying to do this stuff in the template.php file - that's too late in the page building process. Template modifications are more about rendering; most of the actual processing has already happened.
You'll need to move this into your own module, which is not as hard as it sounds.
see the module developers guide: http://drupal.org/node/508 for more info and samples.
One tricky thing about the hook_nodeapi: you can't actually change the node values at the validate stage, like might expect - they have to be changed at the submit or update stage.
If you have the devel module installed (you should!!), there are many helper debugging and variable checking functions you can call. Check the devel.module code.
There's also a module_builder module that helps create structure in a new module. I don't know if it's really something you'd use once you know your way around, but it can be helpful for new coders, just to see the suggested structure.
If you're still lost, check out a copy of 'Pro Drupal Development' by John K. VanDyk and Matt Westgate. It's the book I wish I had when I first started programming with drupal. Alas, by the time it came out, I had already beat myself senseless trying to figure most things out on my own, but I still got a copy.
Look at some contrib modules
Take a look at the implementation of those hooks in some of the popular contrib modules like Nodequeue, Organic Groups, etc. Trace through the code there and you'll learn a lot. If you are then able to contribute some documentation back on Drupal.org to make it easier for the next folks, all the better.
Marc
http://www.funnymonkey.com
Tools for Teachers
That is really helpful. What
That is really helpful. What I was reading on hook_nodeapi didn't say _where it could be called?! Unless I was a little too bleary eyed.
It sounds like that could work if the module's hook_nodeapi will get called for any type of node. If ours is called after CCK, we can run fine grained updates. But at the least call more generic routines for different form_ids.
That should get us over the bend! Maybe filters can be added in the 'view' case as an easy way to add some filtering... Thanks again for the helpful feedback :)
yes, hook_nodeapi will be
yes, hook_nodeapi will be called for any node, even if it is a CCK or module created node. You can act on only one type of node by testing for $node->type in the code.
-Roger