0

我正在尝试为新节点类型“synopsis_book”创建一个简单的自定义字段“field_book_year”。我在 .install 文件中写道:

function synopsis_install() {
    node_types_rebuild();
    $types = node_type_get_types();
    // In Drupal 7 you must explicitly add a "body" field when you create a custom content type,
    // thus the call to node_add_body_field($types['newsletter']);.
    node_add_body_field($types['synopsis_book']);
    $body_instance = field_info_instance('node', 'body', 'synopsis_book');
    $body_instance['type'] = 'text';
    field_update_instance($body_instance);

    field_create_field(array(
        'field_name' => 'field_book_year',
        'label' => t('Company posting the job listing'),
        'type' => 'text',
        'translatable' => TRUE,
        )
    );
    field_create_instance(array(
            'field_name' => 'field_book_year',
            'label' => t('Company posting the job listing'),
            'entity_type' => 'node',
            'bundle' => 'synopsis_book',
            'type' => 'text',
            'widget' => array(
                'type' => 'text_textfield',
            ),
            'display' => array(
                'example_node_list' => array(
                    'label' => t('Company posting the job listing'),
                    'type' => 'text',
                ),
            ),
            'description' => 'Begin Date',
        )
    );
}

然后,我在 .module 中有这些功能:

function synopsis_node_info()
{$types = node_type_get_types();print_r($types);
    return array(
        'synopsis_author' => array('name' => t('Author'), 'base' => 'synopsis_author', 'description' => t('Author description, biography etc.'),
            'has_body' => true, 'has_title' => true, /*'min_word_count' => 11,*/ 'help' => t('Enter blah-blah-blah'),
            'title_label' => t('Author full name')
        ),
        'synopsis_book' => array('name' => t('Book'), 'base' => 'synopsis_book', 'description' => t('Book description, synopsis etc.'),
            'has_body' => true, 'has_title' => true, /*'min_word_count' => 11,*/ 'help' => t('Enter blah-blah-blah'),
            'title_label' => t('Book title')
        ),
    );
}
/**
* Implement hook_form() with the standard default form.
*/
function synopsis_book_form($node, $form_state) {
    return node_content_form($node, $form_state);
}

/**
 * Implements hook_validate().
 */
function synopsis_book_validate($node)
{
    // Enforce a minimum character count of 2 on company names.
    if (isset($node->field_book_year) &&
            strlen($node->field_book_year['und'][0]['value']) < 2
    ) {
        form_set_error('job_post_company',
            t('The company name is too short. It must be atleast 2 characters.'),
            $limit_validation_errors = NULL);
    }
}

不管我写了多少,添加书的页面是这样的!没有“field_book_year”字段。连体场都没有添加书的页面是这样的

我究竟做错了什么?谢谢你。

4

1 回答 1

3

我认为您遇到的问题是hook_install在安装模块时运行,在创建节点类型之前。所以本质上你是在尝试将一个字段附加到一个不存在的节点类型。

我一直这样做的方法是hook_install在尝试附加字段之前创建节点类型(这是提供testimonial内容类型的模块的示例):

// Make sure a testimonial content type doesn't already exist
if (!in_array('testimonial', node_type_get_names())) {
  $type = array(
    'type' => 'testimonial',
    'name' => st('Testimonial'),
    'base' => 'node_content',
    'description' => st("Use <em>basic pages</em> for your static content, such as an 'About us' page."),
    'custom' => 1,
    'modified' => 1,
    'locked' => 0,
    'title_label' => 'Customer / Client Name'
  );

  $type = node_type_set_defaults($type);
  node_type_save($type);
  node_add_body_field($type);
}

// See if the testimonial date field exists
if (!field_info_field('field_testimonial_date')) {
  field_create_field(array(
    // Field info here
  ));
}

// If the date field is not attached, attach it
if (!field_info_instance('node', 'field_testimonial_date', 'testimonial')) {
  field_create_instance(array(
    // Field instance info here
  ));
}

如果您查看标准安装配置文件安装文件,您会发现这实际上是 Drupal 核心执行此任务的方式,所以我希望这是执行此操作的“正确”方式。

于 2011-11-08T15:49:12.267 回答