By dmwlff on
I've used CCK to create a few related custom content types (i.e. they share some fields.) I've looked through the developer tutorial, and I get (at a high level) how to write modules and hooks for fields, widgets, and formatters.
I'm stuck on something relatively simple: how do I define common *node* hooks (like hook_access, hook_validate, hook_view) for the custom content types I've defined with CCK? Where does this code go? I'm sure this is something I just missed or didn't understand.
TIA,
Adam
Comments
Implementing hook_validate, hook_access for CCK content types
Start by defining a custom module. For clarity, you'll probably want to give your module the same name as your content type. However, Drupal has no way of "knowing" that this module is couple in any way to your content type (nor have I found a way to tell Drupal this).
Therefore, you'll want to start by implementing hook_nodeapi in your custom module. According to the official documentation, this hook "Acts on nodes defined by other modules." This gets around the problem that Drupal does not know that your custom module is coupled in any way to your content type.
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
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. There is no way to implement hook_access for a CCK content type, and in Drupal 5.x, access is not an operator exposed in hook_nodeapi. See http://drupal.org/node/143075 for a good discussion of the issue and a suggestion for a patch that can be applied to Drupal core to expose an access hook in hook_nodeapi. Note : the patch given is for Drupal 6.x, so you may need to adapt a little for Drupal 5.x.
quick fix
if($node->type == 'food') {
food_view($node);
should be
if($node->type == 'food') {
food_view($node);
}
I have another way to do
I have another way to do that.
If you see, cck has content_access, which is right called per access rights.
If you implement hook_field_access, regarding on field or not, you can control the same way as a hook_access.
Of course, this denies access to fields, and probably not to node body/title... but can be easily used without patching the core, and node body can be set as a cck field.
Jordi.