使用drupal 7中的自定义表字段创建自定义内容类型

时间:2013-04-03 10:05:30

标签: drupal drupal-7

我想创建一个模块,当我安装该模块时,它应该创建homepage_blocks_schema()中定义的自定义表,创建homepage_blocks_install()中定义的内容类型,并在该内容类型中创建与我相同的自定义字段架构。

这是功能,

function homepage_blocks_schema() {
    $schema['homepage_blocks'] = array(
        'fields' => array(
            'hid' => array(
                'type' => 'int',
                'length' => 11,
                'unsigned' => TRUE,
                'not null' => TRUE,
            ),
            'title' => array(
                'type' => 'varchar',
                'length' => 255,
            ),
            'image' => array(
                'type' => 'varchar',
                'length' => 255,
            ),
        ),
        'primary key' => array('hid'),
    );
    return $schema;
}

homepage_blocks_install() {
       $homepage_blocks = array(
            'type' => 'homepage_blocks',
            'name' => $t('Homepage Blocks'),
            //'base' => 'node_content',
            'base' => 'homepage_blocks',
            'custom' => true,
            'modified' => true,
            'locked' => false,
            'title_label' => $t('Homepage Blocks')
        );

        $content_type = node_type_set_defaults($homepage_blocks);

        //node_add_body_field($content_type, 'Body'); // add the body field to the content type
        node_type_save($content_type); // create the content type

        variable_set('node_options_homepage_blocks', array('status'));
        variable_set('comment_homepage_blocks', 'COMMENT_NODE_HIDDEN'); // hide comments for this node.
        variable_set('node_submitted_homepage_blocks', false); // Hide date and author information

        //drupal_install_schema('homepage_blocks');

        foreach (_homepage_blocks_installed_fields() as $field) { // Create all the fields we are adding to our content type.
            //krumo(field_create_field($field));
            field_create_field($field);
        }

        foreach (_homepage_blocks_installed_instances() as $instance) { // Create all the instances for our fields.
            $instance['entity_type'] = 'node';
            $instance['bundle'] = 'homepage_blocks';
            field_create_instance($instance);
        }
}

function _homepage_blocks_installed_fields() {
    $t = get_t();
    $fields = array(
        'hmblock_title' => array(
            'field_name'    => 'hmblock_title',
            'label'         => $t('Title'),
            //'cardinality' => 1,
            'type'          => 'text',
            'settings'      => array(
                'max_length' => 255,
            ),
        ),
        'hmblock_image' => array(
            'field_name' => 'hmblock_image',
            'label' => $t('Image'),
            //'cardinality' => 1,
            'type' => 'image',
            'settings' => array(
                'default_image' => 0,
                'uri_scheme' => 'public',
            ),
        ),
    );

    //$fields = drupal_get_schema('homepage_blocks');
    return $fields;
}

function _homepage_blocks_installed_instances() {
    $t = get_t();
    $instances = array(
        'hmblock_title' => array(
            'field_name'    => 'hmblock_title',
            'label'         => $t('Title'),
            'cardinality'   => 1,
            'widget'        => array(
                'type'      => 'text_textfield',
                'settings'  => array('size' => 255),
            ),
        ),
        'hmblock_image' => array(
            'field_name' => 'hmblock_image',
            'label' => $t('Image'),
            'cardinality' => 1,
            'type' => 'image',
            'settings' => array(
                'alt_field' => 1,
                'file_directory' => 'image',
                'file_extensions' => 'png gif jpg jpeg',
                'max_filesize' => '50mb',
                'max_resolution' => '',
                'min_resolution' => '',
                'title_field' => 1,
                'user_register_form' => FALSE,
            ),
            'widget' => array(
                'settings' => array(
                    'preview_image_style' => 'thumbnail',
                    'progress_indicator' => 'throbber',
                ),
            )
        ),
    );
    return $instances;
}

此代码创建表格和内容类型,但不创建其字段。 有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

有一种更简单的方法 您可以使用drupal core和fields模块创建自定义类型,然后使用Features模块将其导出到模块中。 您可以在此处找到完整指南:Bundling site settings using Features