drupal hooks not firing for cck content types

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.

What if the module name and

What if the module name and content type is different?

Sweet. This was very helpful

Sweet. This was very helpful indeed!!!

Thanks a lot!! This was

Thanks a lot!! This was absolutely helpful for me, too! This should be contributed somewhere on api.drupal.org.

Please consider contributing

Please consider contributing this rather essential nugget to the CCK documentation.

Yeah ditto -- thanks Cailin!

Yeah ditto -- thanks Cailin!

Just what I needed. (I

Just what I needed.

(I think there's a '}' missing though, closing your first if-statement)

yes, you're right! fixed -

yes, you're right! fixed - and thank-you.

Thanks, this saved my ass!

Thanks, this saved my ass!

Thanks for this. was very

Thanks for this. was very helpful!

Please note, this entry has been closed to new comments.