1

我正在以编程方式在 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);
}
4

2 回答 2

2

事实证明,这两个问题的解决方案是将其添加到 course.module 中:

function course_form($node, $form_state)
{
    return node_content_form($node, $form_state);
}
于 2012-04-25T19:49:37.590 回答
0

我只是想为这个问题添加一个旁白,以防有人遇到我遇到的问题。

如果您在创建自定义类型的节点时无法显示节点表单,请寻找一件小事:您是否使用与模块名称不同的名称作为节点类型机器名称,并且是 hook_form 的名称() 为您的节点类型使用您的节点类型机器名称还是模块名称?

我有一个名为 Novel 的模块,它创建了一个名为 Novel_section 的节点类型。经过大约 6 个小时的努力,试图弄清楚为什么新的 novel_section 类型的表单没有显示正文或标题字段,并且在网上阅读了关于如何使用自定义模块创建自定义节点类型的帖子后,我终于意识到了是因为我的 hook_form() 被称为 Novel_form()(就像您命名任何其他模块函数一样)而不是 Novel_section_form()。

所有在线示例都假设您的模块名称与您的节点类型机器名称相同,但并非总是如此。此外,没有人提到 hook_form() 需要使用您的节点类型机器名称,而不管模块名称是什么。

如果像我一样,您的模块不仅仅是创建节点类型或创建多个节点类型,并且您无法弄清楚为什么您无法让标题和/或正文显示在新节点中新节点类型的表单,这应该会有所帮助。

于 2013-11-13T14:09:11.497 回答