6.3 简化了实体类型的定义

作者:老葛,北京亚艾元软件有限责任公司,http://www.yaiyuan.com

这个模块还提供了一个Entity对象,如果我们要定义一个新的实体类型时,只需要继承这个对象就可以了,这个对象帮助我们实现了完整的CRUD操作。

Entity API里面,所有的bundle,这里说的是Bundle本身,也都被处理成为了实体,这是一种可以导入、导出的实体。在Drupal核心中,内容类型的添加、编辑,是没有采用实体的形式的。Entity API模块,在这里走的更远,Bundle本身也处理成为了实体。我们看Profile2模块,在profile2_entity_info里面,可以找到这段代码:

  $return['profile2_type'] = array(

    'label' => t('Profile type'),

    'plural label' => t('Profile types'),

    'description' => t('Profiles types of Profile2 user profiles.'),

    'entity class' => 'ProfileType',

    'controller class' => 'EntityAPIControllerExportable',

    'base table' => 'profile_type',

    'fieldable' => FALSE,

    'bundle of' => 'profile2',

    'exportable' => TRUE,

    'entity keys' => array(

      'id' => 'id',

      'name' => 'type',

      'label' => 'label',

    ),

    'access callback' => 'profile2_type_access',

    'module' => 'profile2',

    // Enable the entity API's admin UI.

    'admin ui' => array(

      'path' => 'admin/structure/profiles',

      'file' => 'profile2.admin.inc',

      'controller class' => 'Profile2TypeUIController',

    ),

  );

通过这段代码,把Profile类型也处理成为实体了。对于这种包括Bundle类型的实体,Entity API还提供了一个管理界面,我们只需要对这个默认的管理界面进行扩展,就可以了。这里使用EntityDefaultUIController来管理默认的界面,Profile2TypeUIController则继承了EntityDefaultUIController

如果我们基于Entity API来定义实体类型的话,此时与ViewsRules的集成都比较友好。Entity API模块提供了Views的基本集成,而Rules模块则是Fago的另一个杰作,本身就是基于Entity API模块的。


Drupal版本: