1

我想创建一个模块,当我安装该模块时,它应该创建在 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;
}

此代码创建表和内容类型,但不是它的字段。任何人都可以帮忙吗?

4

1 回答 1

1

There is an easier way you can create your custom type using drupal core and fields module and then export it into a module using Features module. You can find a full guide here : Bundling site settings using Features

于 2013-04-28T06:34:35.023 回答