1

Drupal7モジュールでカスタムコンテンツタイプをプログラムで作成しています。例を踏襲しましたが、何らかの理由でインストール後、コンテンツタイプ一覧にコンテンツタイプが表示されず、コースタイプのコンテンツを作成する際にタイトルを入力する場所がありません。

私は何が間違っているのですか?

これが私の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

誰かが私が抱えていた問題に遭遇した場合に備えて、この問題を脇に置いておきたかっただけです。

カスタムタイプのノードを作成するときにノードフォームを表示するのに問題がある場合は、1つの小さなことを探してください。ノードタイプのマシン名としてモジュール名とは異なる名前を使用しており、これはhook_formの名前です。 ()ノードタイプのマシン名またはモジュール名を使用してノードタイプを指定しますか?

私はnovel_sectionと呼ばれるノードタイプを作成するnovelという名前のモジュールを持っています。新しいnovel_sectionタイプのフォームに本文またはタイトルのフィールドが表示されない理由を約6時間調べた後、カスタムモジュールを使用してカスタムノードタイプを作成する方法についてオンラインで投稿を読んだ後、ようやく気づきました。私のhook_form()がnovel_section_form()ではなく(他のモジュール関数に名前を付けるように)novel_form()と呼ばれたためです。

これに関するオンラインのすべての例は、モジュール名がノードタイプのマシン名と同じであると想定していますが、常にそうであるとは限りません。さらに、モジュール名が何であれ、hook_form()がノードタイプのマシン名を使用する必要があることについては誰も言及していません。

私のように、ノードタイプを作成するだけでなく、複数のノードタイプを作成するだけのモジュールがあり、タイトルや本文を新しいノードに表示できない理由がわからない場合新しいノードタイプのフォーム。これが役立つはずです。

于 2013-11-13T14:09:11.497 に答える