Multiple node types specified within one module?
chris_thecarguy - July 3, 2008 - 21:42
Hi there. Pardon me if this is a simple question, and my apologies if it's an obvious answer and I'm just missing it in the API, if one has two (or more) node types specified in a module, is it possible to handle the separate node types in the other hooks, such as hook_insert()?
For a more specific example, let's say I have module "Foo" with node types "Foo" and "Bar," where Foo and Bar both have tables reserved for them with extra content. Using the hooks, one can just use the "Foo" module to handle both the "Foo" and "Bar" database inserts, right?
Thanks in advance.

Yes, though personally I
Yes, though personally I find it easier simply to make two modules with one dependent on the other. Having two (or more) or each node hook tends to make the code harder to follow. The way to do it is the return value from hook_node_info(). It can return an array of node types, the key to your question is the 'module' element. While general the name of the module, it can be more generally thought of the the function prefix for calling the hooks. So if your module name is mymodule and you set 'module' to 'mymodule_type1', then hooks would be prefixed with 'mymodule_type1', so hook_insert() would be called as mymodule_type1_insert().
If you examine the inner
If you examine the inner workings of modules that define a content type, you'll notice that especially in hook_node_info(), you return a multi-dimensional array that contains certain information. So looking at hook_node_info(), you'll usually do this to define one type (this is from this tutorial):
<?phpfunction node_example_node_info() {
return array(
'node_example' => array(
'name' => t('example node'),
'module' => 'node_example',
'description' => "This is an example node type with a few fields.",
)
);
}
?>
If you want to have more types defined within one module, you can append more to this returned array, like so:
<?phpfunction node_example_node_info() {
return array(
'node_example' => array(
'name' => t('example node'),
'module' => 'node_example',
'description' => "This is an example node type with a few fields.",
), // End "node_example"
'another_node_example' => array(
'name' => t('another example node'),
'module' => 'node_example',
'description' => "This is an additional node type.",
) // End "another_node_example"
); // End return array definition...
} // End function definition.
?>
You can append as many as you'd like.
There's one issue of concern here, however. In every hook that you implement that deals directly with your node definitions, you must deal with each data type separately. So if data for node type "another_node_example" comes in, you'll need to deal with that data from that node type appropriately. Hooks that pertain to defining node types are almost always passed a parameter that contains information about the node type of concern. This is usually either $node or $form. (Again, read this tutorial if need be).
Advanced programmers can do this fairly easily but for each type you define within a single module your code will expand greatly.
The other way to do what you want is to just create separate modules for each type.