如何在Drupal7中以编程方式创建新的内容类型?
我正在Drupal7中构建一个模块(如何在Drupal7中以编程方式创建新的内容类型?,drupal,module,drupal-7,content-type,Drupal,Module,Drupal 7,Content Type,我正在Drupal7中构建一个模块(myu模块) 它有一些功能,还将创建新的内容类型 在my_module.install中,我实现了hook_install(my_module\u install) 我是否可以使用多个hook\u install实现在此模块中创建新的内容类型(my\u cck\u install) 如果(是),我应该怎么做 其他:我是否在其他模块中完成了此操作?:-) 同一模块中不能使用多个hook\u install实现;在PHP中,不能有两个同名函数,这就排除了这一点 您
myu模块
)
它有一些功能,还将创建新的内容类型
在my_module.install中,我实现了hook_install
(my_module\u install
)
我是否可以使用多个hook\u install
实现在此模块中创建新的内容类型(my\u cck\u install
)
如果(是),我应该怎么做
其他:我是否在其他模块中完成了此操作?:-) 同一模块中不能使用多个
hook\u install
实现;在PHP中,不能有两个同名函数,这就排除了这一点
您只需要在相同的hook\u install
中添加新的内容类型(看看标准安装配置文件是如何在/profiles/standard/standard.install中实现的)。这就是我总是从安装文件添加新内容类型的方式(使用推荐模块的示例):
下面的代码将创建一个名为“Event”的内容类型,其机器名为“Event”,标题字段为-
//CREATE NEW CONTENT TYPE
function orderform_node_info() {
return array(
'event' => array(
'name' => t('Event'),
'base' => 'event',
'description' => t('A event content type'),
'has_title' => TRUE
),
);
}
function event_form($node,$form_state) {
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('event Title'),
'#default_value' => !empty($node->title) ? $node->title : '',
'#required' => TRUE,
'#weight' => -5
);
return $form;
}
//END CONTENT TYPE
你应该把它放在你的.module
文件中。。。如果你想添加额外的字段,让我知道,我会修补你的代码。。。祝你好运
/**
* Implements hook_node_info()
*/
function mymodule_node_info() {
return array(
'news' => array(
'name' => t('News'),
'base' => 'news',
'description' => t('You can add News here'),
'has_title' => TRUE,
'title_label' => t('News title')
)
);
}
/**
* Implement hook_form()
*/
function mymodule_form($node, $form_state) {
return node_content_form($node, $form_state);
}
将实现添加到mymodule.install,如下所示:
/**
* Implements hook_install().
*/
function mymodule_install() {
node_types_rebuild();
$types = node_type_get_types();|
node_add_body_field($types['news']);
}
您可以从/*
*在模块文件中的钩子节点信息中实现
*/
}
/**
*机具挂钩形式()
*/
}
/**
*在安装文件中实现hook_install()。
*/
}
/*
*定义您的字段
*/
}
/*
*定义字段的实例
*/
}
/**
*实现hook_uninstall()。
*/
}
就这样 我只在数据库中看到“鉴定”,我添加了什么来在admin/structure/types中看到“鉴定”呢?清除Drupal的缓存,这样就可以了。尝试直接访问
admin/structure/types/commential
,以确保您的代码已实际运行。很抱歉,我的错误,它根本不起作用。我是否应该使用hook\u node\u info()?这完全取决于具体情况。如果您想自己处理内容类型的功能,那么是的,您想创建一个所谓的“节点模块”(使用hook\u node\u info()
etc)。如果您只是想创建一个没有特殊处理的内容类型,那么上面的方法就容易多了。当你放入新代码时,你确定要卸载并重新安装模块吗?不,我还没有安装。你能告诉我怎么做吗?拜托,我能做到这一点,但我想知道如何存储和访问表单数据
/**
* Implements hook_install().
*/
function mymodule_install() {
node_types_rebuild();
$types = node_type_get_types();|
node_add_body_field($types['news']);
}
function test_node_info() {
return array(
'product' => array(
'name' => t('Product'),
'base' => 'product',
'description' => t('Product Title'),
)
);
function product_form($node, $form_state) {
return node_content_form($node, $form_state);
function test_install() {
node_types_rebuild();
$types = node_type_get_types();
node_add_body_field($types['product']);
//New way to implement to add fields in your content type
foreach (_test_installed_fields() as $field) {
field_create_field($field);
}
foreach (_test_installed_instances() as $fieldinstance) {
$fieldinstance['entity_type'] = 'node';
$fieldinstance['bundle'] = 'product';
field_create_instance($fieldinstance);
}
function _test_installed_fields() {
$t = get_t();
return array(
'product_title123' => array(
'field_name' => 'product_title123',
'label' => $t('Product Title'),
'type' => 'text'
),
'description123' => array(
'field_name' => 'description123',
'label' => $t('Description'),
'type' => 'text'
),
);
function _test_installed_instances() {
$t = get_t();
return array(
'product_title123' => array(
'field_name' => 'product_title123',
'type' => 'text',
'label' => $t('Product Title'),
'widget' => array(
'type' => 'text_textfield'
),
'display' => array(
'example_node_list' => array(
'label' => $t('Product Title'),
'type' => 'text'
)
)
),
'description123' => array(
'field_name' => 'description123',
'type' => 'text',
'label' => $t('Description'),
'widget' => array(
'type' => 'text_textarea_with_summary'
),
'display' => array(
'example_node_list' => array(
'label' => $t('Description'),
'type' => 'text'
)
)
),
);
function test_uninstall() {
$ournewtype = 'product';
$sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
$result = db_query($sql, array(':type' => $ournewtype));
$nodeids = array();
foreach ($result as $row) {
$nodeids[] = $row->nid;
}
node_delete_multiple($nodeids);
node_type_delete($ournewtype);