A new module 'foo' implements foo_node_info() where one or more new content types can be defined.
If foo_node_info() defines two content types, namely a content type 'footypea' and a content type 'footypeb', how does one go about implementing hook_form() (what should the name of the "hook" be?) to configure each node's editing form?
In the drupal example, the name of the new content type is the same as the module name. What happens in the above described example where two new content types are defined by the module?
Should the implemented hook_form() function be of the form: foo开发者_如何学Pythontypea_form() and footypeb_form() ? (this doesn't seem to work)
Or should you implement a single foo_form() function and within this create and return an array $form with elements $form['footypea'] and $form['footypeb'] that are in turn arrays of the individual form field definitions?
In your module's hook_node_info(), add the 'module'
property (see http://api.drupal.org/api/function/hook_node_info/6).
For example:
/**
* Implementation of hook_node_info().
*/
function foo_node_info() {
return array(
'footypea' => array(
'name' => t('Foo Type A'),
'description' => t('This is Foo Type A'),
'module' => 'footypea', //This will be used for hook_form()
),
'footypeb' => array(
'name' => t('Foo Type B'),
'description' => t('This is Foo Type B'),
'module' => 'footypeb', //This will be used for hook_form()
),
);
}
Now you can add the following hook_form() implementations for each type (see http://api.drupal.org/api/function/hook_form/6).
/**
* Implementation of hook_form().
*/
function footypea_form(&$node, $form_state) {
// Define the form for Foo Type A
}
/**
* Implementation of hook_form().
*/
function footypeb_form(&$node, $form_state) {
// Define the form for Foo Type B
}
The trick here is that the module property of each element in hook_node_info()
does not have to be the same as the module implementing hook_node_info()
. Each type defined can have a unique module property for implementing type specific hooks.
It doesn't matter what the node types are called, when you implement hooks in Drupal, the first part is the name of the module that creates them. So here, your foo module implement hook_form() in foo_form().
By the way, because it's easier and because it's coming in Drupal 7, you should also check out CCK for making content types.
精彩评论