views:

36

answers:

2

Is it possible to declare and manage several custom content types inside one module? I'm creating a site that needs four custom content types and I'd like to manage them from one module instead of creating module for every content type. After some testing, I found out that it seems impossible. Because, unless hook_form and content type share the same name of module, drupal doesn't call hook_form.

Here's how I'd like to do -

function mycontent_node_info(){
return array(
    'mycontent1' => array(
        'name' => t('....'),
        'module' => 'mycontent',
        'description' => t('...),
        'has_title' => TRUE,
        'title_label' => t('Title'),
        'has_body' => TRUE,
        'body_label' => t('content body'),
    ),
    'mycontent2' => array(
        .......
    ),
    'mycontent3' => array(
        ......
    ),
    'mycontent4' => array(
        ......
    ),
);
}

function mycontent1_form(&$node){
$form['control1'] = array(
    '#type' => 'select',
    '#options' => array(
        '0' => t('selection 1'),
        '1' => t('selection 2'),
    ),
    '#attributes' => array('id'=>'control1'),
);

$form['control2'] = array(
    '#type' => 'select',
    '#options' => array(
        '0' => t('1'),
        '1' => t('2'),
        '2' => t('3'),
        '3' => t('4'),
    ),
    '#attributes' => array('id'=>'control2'),
);   
return $form;
}

function mycontent2_form(&$node){
....
} 

function mycontent3_form(&$node){
    ....
} 


function mycontent4_form(&$node){
    ....
}  

Am I doing something wrong here or is not possible and there's no alternative other than creating module for every content types. I appreciate much your help.

+1  A: 

You might try using the Features module (http://drupal.org/project/features) to export your content types. It auto generates the code to make this work, and you can have a look at what is going wrong with your code.

bmann
+1  A: 

The prefix for all your hooks should be the name of your module, i.e. mycontent_node_info() and mycontent_form(&$node). I think the content type itself can be called whatever you want but by convention anything global that you define in a module should be prefixed with the name of the module to avoid namespace problems. So your content becomes mycontent_type1, mycontent_type2, etc... As for dealing with hook_form, I guess the way to do it is to check the type of the node passed in and act accordingly.

CurtainDog
Sorry for my late reply. Thanks it works.
Andrew