我正在 Drupal 7 中构建一个模块( my_module
)。
它有一些功能,也会创建新的内容类型。
在 my_module.install 中,我实现了 hook_install
(my_module_install
)。
我可以使用多个 hook_install
的实现吗?在这个模块中创建新的内容类型(my_cck_install
)?
如果(是),我应该怎么做?
其他:我是否在另一个模块中执行此操作? :-)
最佳答案
您不能使用多个 hook_install
的实现。在同一个模块中;在 PHP 中,你不能有 2 个同名的函数来排除这种情况。
您只需在同一 hook_install
中添加新的内容类型无论如何(看看标准安装配置文件是如何在/profiles/standard/standard.install 中完成的)。这就是我总是从安装文件中添加新内容类型的方式(使用推荐模块的示例):
function testimonial_install() {
// Make sure a testimonial content type doesn't already exist
if (!in_array('testimonial', node_type_get_names())) {
$type = array(
'type' => 'testimonial',
'name' => st('Testimonial'),
'base' => 'node_content',
'custom' => 1,
'modified' => 1,
'locked' => 0,
'title_label' => 'Customer / Client Name'
);
$type = node_type_set_defaults($type);
node_type_save($type);
node_add_body_field($type);
}
}
关于drupal - 如何在 Drupal 7 中以编程方式创建新的内容类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8118634/