I am writing a module that will produce three new content types. The documentation of the API is confusing - I have read the node_example.module http://api.drupal.org/api/file/developer/examples/node_example.module/5/... and finaly understood that its wrong. It says that you should name the hooks by the name of the node type - thats not true - the hooks is named by the module. Perhaps a example module should not have the same name as the example node?

My problem is still how I implement different hooks for different nodetypes, for example hook_insert or hook_access.

Comments

criznach’s picture

You can implement multiple node types in one module by specifying a different hook prefix in the "module" field returned from hook_node_info. Yes, it's a bit confusing, but your node type related hooks don't have to start with the module name.

From the hook_node_info() docs:

"module": a string telling Drupal how a module's functions map to hooks (i.e. if module is defined as example_foo, then example_foo_insert will be called when inserting a node of that type). This string is usually the name of the module in question, but not always. Required.

So if your module was called foo, and created two types, "car" and "rocket", you could define your callbacks something like this...

function foo_node_info() {
  return array(
    'car' => array(
      'name' => t('Car'),
      'module' => 'foo_car',
      'description' => t("Blah blah blah Car."),
    ),
    'rocket' => array(
      'name' => t('Rocket'),
      'module' => 'foo_rocket',
      'description' => t("Blah blah blah Rocket."),
    )
  );
}

Your callbacks would then be something like...

function foo_car_insert() {}
and
function foo_rocket_insert() {}

laghalt’s picture

Ok,

now Im confused - I read that the hooks related to modules. Thank you for the answer.
I have already figured out the foo_node_info array - that was no problem - it was in the api doc.

But witch hooks belongs to the type and witch belongs to the module - does the hooks in node_example.module belong to the nodetype?

Are all these not related to the module: hook_access, hook_delete, hook_foorm, hook_insert, hook_nodeapi, hook_perm, hook_update, hook_validate, hook_view ?

Where can I find documentation on this?