我在Drupal 7模块中以编程方式创建自定义内容类型。我已按照示例进行操作,但由于某些原因,在安装后,内容类型未显示在内容类型列表下,并且在创建类型课程的内容时,没有可输入标题的位置。
我做错了什么?
这是我的course.install文件:
<?php
function course_schema()
{
$schema['course_status'] = array('description' => t('Stores user specific course status information.'),
'fields' => array('id' => array('description' => t('The primary identifier'),
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE),
'uid' => array('description' => t('The user identifier.'),
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0),
'nid' => array('description' => t('The node identifier.'),
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0),
'visits' => array('description' => t('The visit count.'),
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0),
'is_completed' => array('description' => t('The completion flag.'),
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0),
'completed_at' => array('description' => t('The completion date, as a timestamp.'),
'type' => 'int',
'default' => NULL)),
'primary key' => array('id'));
return $schema;
}
function course_install()
{
// During installation, the t() function is unavailable, so we use get_t()
// to store the name of the translation function.
$t = get_t();
// We define the node type as an associative array.
$course = array('type' => 'course',
'name' => $t('Course'),
// 'base' tells Drupal the base string for hook functions.
// This is often the module name; if base is set to 'mymodule', Drupal
// would call mymodule_insert() or similar for node hooks.
// In this case, we set base equal to 'node_content' so Drupal will handle
// our node as if we had designed it in the UI.
'base' => 'node_content',
'description' => $t('This is a course node.'),
'title_label' => $t('Title'),
'custom' => TRUE,);
// Complete the node type definition by setting any defaults not explicitly
// declared above.
// http://api.drupal.org/api/function/node_type_set_defaults/7
$content_type = node_type_set_defaults($course);
//Course blocks have an image, and body.
node_add_body_field($content_type, $t('Description'));
// Save the content type
node_type_save($content_type);
// Create all the fields we are adding to our content type.
// http://api.drupal.org/api/function/field_create_field/7
foreach(_course_installed_fields() as $field)
{
field_create_field($field);
}
// Create all the instances for our fields.
// http://api.drupal.org/api/function/field_create_instance/7
foreach(_course_installed_instances() as $instance)
{
$instance['entity_type'] = 'node';
$instance['bundle'] = $course['type'];
field_create_instance($instance);
}
//Don't show submitted info on course nodes
// variable_set('node_submitted_course', 0);
}
/**
* Returns a structured array defining the fields created by this content type.
* This is factored into this function so it can be used in both
* node_example_install() and node_example_uninstall().
* @return
* An associative array specifying the fields we wish to add to our
* new node type.
* @ingroup node_example
*/
function _course_installed_fields()
{
$t = get_t();
return array('course_image' => array('field_name' => 'course_image',
'type' => 'image',
'cardinality' => 1,),
'course_curriculum_id' => array('field_name' => 'course_curriculum_id',
'type' => 'number_integer',
'settings' => array('max_length' => 9),
'cardinality' => 1,));
}
/**
* Returns a structured array defining the instances for this content type.
* The instance lets Drupal know which widget to use to allow the user to enter
* data and how to react in different view modes. We are going to display a
* page that uses a custom "node_example_list" view mode. We will set a
* cardinality of three allowing our content type to give the user three color
* fields.
* This is factored into this function so it can be used in both
* node_example_install() and node_example_uninstall().
* @return
* An associative array specifying the instances we wish to add to our new
* node type.
* @ingroup node_example
*/
function _course_installed_instances()
{
$t = get_t();
return array('course_image' => array('field_name' => 'course_image',
'label' => $t('Image:'),
'required' => FALSE,
'widget' => array('type' => 'image_image',
'weight' => 2.10),
'display' => array('course_list' => array('label' => 'hidden',
'type' => 'image_link_content__thumbnail',))),
'course_curriculum_id' => array('field_name' => 'course_curriculum_id',
'label' => $t('Curriculum Id') . ':',
'required' => TRUE,
'widget' => array('type' => 'text_textfield'),
'settings' => array('text_processing' => 0),
'display' => array('course_list' => array('label' => 'hidden',
'type' => 'hidden'))));
}
function course_uninstall()
{
// Drop my tables.
drupal_uninstall_schema('course');
//remove any nodes of the type course
$sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
$result = db_query($sql, array(':type' => 'course'));
$nids = array();
foreach($result as $row)
{
$nids[] = $row->nid;
}
// Delete all the nodes at once
node_delete_multiple($nids);
//remove the content type
node_type_delete('course');
//and their associated fields
foreach(_course_installed_fields() as $field)
{
field_delete_field($field['field_name']);
}
}
?>
我在课程中有这个。模块:
<?php
function course_node_info()
{
return array('course' => array('name' => t('Course'),
'base' => 'course',
'description' => t('A course content type'),
'has_title' => TRUE,
'title_label' => t('Title'),
'locked' => FALSE,),);
}
?>
解决 事实证明,这两个问题的解决方案是将其添加到course.module:
function course_form($node, $form_state)
{
return node_content_form($node, $form_state);
}
答案 0 :(得分:2)
原来解决这两个问题的方法是将其添加到course.module:
function course_form($node, $form_state)
{
return node_content_form($node, $form_state);
}
答案 1 :(得分:0)
如果有人遇到我遇到的问题,我只想补充一下这个问题。
如果在创建自定义类型的节点时无法显示节点表单,请查找一个小问题:您是否使用与模块名称不同的名称作为节点类型的计算机名称并且是名称使用节点类型的机器名称或模块名称来获取节点类型的hook_form()?
我有一个名为novel的模块,它创建一个名为novel_section的节点类型。经过大约6个小时试图弄清楚为什么新的novel_section类型的表单没有显示正文或标题字段,并在阅读帖子后发布在线后关于如何使用自定义模块创建自定义节点类型后,我终于实现了它是因为我的hook_form()被称为novel_form()(就像你会命名任何其他模块函数)而不是novel_section_form()。
在线的所有示例都假设您的模块名称与您的节点类型机器名称相同,并且情况并非总是如此。另外,没有人提到hook_form()需要使用你的节点类型的机器名,无论模块名称是什么。
如果像我一样,你有一个模块不仅仅创建一个节点类型或创建多个节点类型,你无法弄清楚为什么你不能让标题和/或正文出现在新节点类型的新节点表单,这应该有所帮助。