Hook_form for several types of content defined by the same module

The new module "foo" implements foo_node_info () , where one or more new content types can be defined.

If foo_node_info () defines two types of content, namely the content type “footypea” and the content type “footypeb”, how do one execute hook_form () (what should be the name “hook”?) To configure each form of editing node?

In the drupal example, the name of the new content type is the same as the module name. What happens in the example above when two new types of content are defined by a module?

If the implemented hook_form () function has the form: footypea_form () and footypeb_form ()? (this does not work)

Or should you implement one function foo_form () and inside this create and return an array of $ form with elements $ form ['footypea'] and $ form ['footypeb'], which in turn are arrays of a separate field of the definition form?

+4
source share
2 answers

In your module hook_node_info () add the property 'module' (see http://api.drupal.org/api/function/hook_node_info/6 ).

For instance:

 /** * 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() should not be the same as the module that implements hook_node_info() . Each type defined can have a unique module property for implementing types of specific hooks.

+3
source

It doesn't matter what calls the node types make when you implement hooks in Drupal, the first part is the name of the module that creates them. So your foo module implements hook_form () in foo_form ().

By the way, because it is simpler and because it is included in Drupal 7, you should also check the CCK to create content types.

0
source

Source: https://habr.com/ru/post/1308930/


All Articles