Tôi đang cố gắng sử dụng các thực thể và API trường của Drupal 7 để xây dựng chính xác một mô-đun mới. Những gì tôi đã không thể hiểu được từ tài liệu là cách chính xác để sử dụng API mới để tạo 'loại nội dung' (không phải là loại nút) với một số trường được đặt, chẳng hạn như Nội dung.Cách đúng để sử dụng Drupal 7 Thực thể và Field API
Tôi đang cố gắng thiết lập thực thể bằng hook_entity_info, sau đó tôi tin rằng tôi cần thêm trường body bằng cách sử dụng field_create_instance, nhưng dường như tôi không thể làm việc đó.
Trong mycontenttype.module:
/**
* Implements hook_entity_info().
*/
function mycontenttype_entity_info() {
$return = array(
'mycontenttype' => array(
'label' => t('My Content Type'),
'controller class' => 'MyContentTypeEntityController',
'base table' => 'content_type',
'uri callback' => 'content_type_uri',
'entity keys' => array(
'id' => 'cid',
'label' => 'title',
),
'bundles' => array(
'mycontenttype' => array(
'label' => 'My Content Type',
'admin' => array(
'path' => 'admin/contenttype',
'access arguments' => array('administer contenttype'),
),
),
),
'fieldable' => true,
),
);
return $return;
}
/**
* Implements hook_field_extra_fields().
*/
function mycontenttype_field_extra_fields() {
$return['mycontenttype']['mycontenttype'] = array(
'form' => array(
'body' => array(
'label' => 'Body',
'description' => t('Body content'),
'weight' => 0,
),
),
);
return $return;
}
Sau đó thực hiện điều này đi trong file .install?
function mycontenttype_install() {
$field = array(
'field_name' => 'body',
'type' => 'text_with_summary',
'entity_types' => array('survey'),
'translatable' => TRUE,
);
field_create_field($field);
$instance = array(
'entity_type' => 'mycontenttype',
'field_name' => 'body',
'bundle' => 'mycontenttype',
'label' => 'Body',
'widget_type' => 'text_textarea_with_summary',
'settings' => array('display_summary' => TRUE),
'display' => array(
'default' => array(
'label' => 'hidden',
'type' => 'text_default',
),
'teaser' => array(
'label' => 'hidden',
'type' => 'text_summary_or_trimmed',
),
),
);
field_create_instance($instance);
}
bài viết trên blog này có thể có ích: http://www.istos.it/blog/drupal-entities/drupal-entities-part-3-programming-hello-drupal-entity – bkildow