【问题标题】:Managing several custom content types from one module(drupal)从一个模块管理多个自定义内容类型(drupal)
【发布时间】:2010-06-06 16:39:34
【问题描述】:

是否可以在一个模块中声明和管理多个自定义内容类型?我正在创建一个需要四种自定义内容类型的网站,我想从一个模块管理它们,而不是为每种内容类型创建模块。经过一番测试,我发现这似乎是不可能的。因为,除非 hook_form 和 content type 共享相同的模块名称,drupal 不会调用 hook_form。

这就是我想做的-

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){
    ....
}  

我在这里做错了什么还是不可能,除了为每种内容类型创建模块之外别无选择。非常感谢您的帮助。

【问题讨论】:

    标签: drupal module


    【解决方案1】:

    所有钩子的前缀应该是模块的名称,即 mycontent_node_info() 和 mycontent_form(&$node)。我认为内容类型本身可以随心所欲地调用,但按照惯例,您在模块中定义的任何全局内容都应以模块名称为前缀,以避免命名空间问题。所以你的内容就变成了mycontent_type1、mycontent_type2等……至于处理hook_form,我想办法是检查传入的节点的类型,并采取相应的行动。

    【讨论】:

      【解决方案2】:

      您可以尝试使用功能模块 (http://drupal.org/project/features) 来导出您的内容类型。它会自动生成代码来完成这项工作,您可以查看您的代码出了什么问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-24
        • 2011-10-11
        • 1970-01-01
        • 2012-09-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-03
        相关资源
        最近更新 更多