使用Drupal 7实体和字段API的正确方法

使用Drupal 7实体和字段API的正确方法,drupal,drupal-modules,drupal-7,Drupal,Drupal Modules,Drupal 7,我试图使用Drupal7的实体和FieldAPI来正确构建一个新模块。从文档中我无法理解的是使用新API创建“内容类型”(而不是节点类型)的正确方法,它包含许多设置字段,例如Body 我正在尝试使用hook\u entity\u info设置实体,然后我认为我需要使用field\u create\u实例添加body字段,但我似乎无法让它工作 在mycontenttype.module中: /** * Implements hook_entity_info(). */ function myc

我试图使用Drupal7的实体和FieldAPI来正确构建一个新模块。从文档中我无法理解的是使用新API创建“内容类型”(而不是节点类型)的正确方法,它包含许多设置字段,例如Body

我正在尝试使用hook\u entity\u info设置实体,然后我认为我需要使用field\u create\u实例添加body字段,但我似乎无法让它工作

在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;
} 
那么这会进入.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);
}

我认为您的问题是,如果安装了节点模块,那么已经有一个名为“body”的字段。您应该将字段重新命名为“mycontenttype\u body”(comment.module使用comment\u body),或者重新使用“body”字段,跳过添加字段部分,然后跳到添加其实例。建议使用前者而不是后者。

每个字段都有一个数组属性entity\u types,它限制了字段可以附加到的实体。 我能找到的最好的Drupal解决方案,hook\u field\u create\u field,可以在创建字段时更改字段,但这对在安装时创建的body字段没有好处。 所以我的解决方案就是直接在我的hook_安装中编辑数据库

$data_col = db_query("SELECT data from field_config where field_name = 'body'")->fetchAssoc(); $data = unserialize($data_col['data']); $data['entity_types'][] = 'MY_ENTITY_TYPE'; db_update('field_config') ->fields(array('data' => array('data' => serialize($data)))) ->condition('field_name', 'body') ->execute(); $data\u col=db\u query(“从字段配置中选择数据,其中字段名称='body'”)->fetchAssoc(); $data=unserialize($data_col['data']); $data['entity_TYPE'][='MY_entity_TYPE'; 数据库更新(“字段配置”) ->字段(数组('data'=>array('data'=>serialize($data))) ->条件('字段名称','正文')
->执行() 刚刚沿着同一条路径开始,这里有一个很好的repo开始:

是的,这就是我想要做的-重用节点中的body字段。那么我在hook_安装中添加它的实例对吗?它似乎不起作用。我是否应该看到使用field_attach_form()将body字段自动添加到表单中?这篇博文可能很有用: