我正在以编程方式在 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']);
}
}
?>
我在 course.module 中有这个:
<?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);
}