Monday, March 9, 2009

drupal hooks not firing for cck content types

drupal hooks not firing for cck content types
posted october 26th, 2007 by cailin
in

* cck
* drupal
* tech

a common path followed by advanced drupal developers using cck is the following

1. create a content type using cck
2. create a supporting custom module to handle advanced customizations. typically, the module is given the same name as the content type

in this custom module, developers then attempt to implement standard drupal hooks like hook_access and hook_submit. much confusion then arises as to why the drupal hook is not firing for the cck content type.
the reason is the following. hook_access, hook_insert, hook_submit, hook_update and hook_view only fire for the module that owns the content type. for cck content types, the module that owns the content type is content (e.g. cck) not your supporting custom module. therefore, drupal leaves your supporting custom module totally out of the loop!

so what's a developer supposed to do? for hook_insert, hook_submit, hook_update and hook_view use hook_nodeapi instead. let's say your content type is called food, and your module is also called food. then, the hook_nodeapi method might look something like

function food_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
switch ($op) {
case 'view':
if($node->type == 'food') {
food_view($node);
}
break;
case 'validate':
if($node->type == 'food') {
food_validate($node);
}
break;
}
}

in which food_view and food_validate are just normal methods that you also implement in your module.

unfortunately, hook_access is an entirely different story, now addressed in a later chapter.

* cailin's blog
* 1953 reads

bookmark/search this post with:

* delicious delicious
* digg digg
* reddit reddit
* google google
* yahoo yahoo

What if the module name and
on september 29th, 2008 maverick (not verified) says:

What if the module name and content type is different?
Sweet. This was very helpful
on july 24th, 2008 guest (not verified) says:

Sweet. This was very helpful indeed!!!
Thanks a lot!! This was
on july 2nd, 2008 guest (not verified) says:

Thanks a lot!! This was absolutely helpful for me, too! This should be contributed somewhere on api.drupal.org.
Please consider contributing
on june 26th, 2008 guest (not verified) says:

Please consider contributing this rather essential nugget to the CCK documentation.
Yeah ditto -- thanks Cailin!
on may 29th, 2008 Ben (not verified) says:

Yeah ditto -- thanks Cailin!
Just what I needed. (I
on may 16th, 2008 jefke (not verified) says:

Just what I needed.

(I think there's a '}' missing though, closing your first if-statement)
yes, you're right! fixed -
on may 19th, 2008 cailin says:

yes, you're right! fixed - and thank-you.
Thanks, this saved my ass!
on march 7th, 2008 guest (not verified) says:

Thanks, this saved my ass!
Thanks for this. was very
on february 21st, 2008 pagaille (not verified) says:

Thanks for this. was very helpful!
Please note, this entry has been closed to new comments.

No comments: