欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

实现hook_entity_info

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

接下来,我们创建breadcrumb2.module文件,首先输入以下代码:

<?php

 

/**

 * @file

 * Support for configurable breadcrumbs.

 */

   接着,实现hook_entity_info()

/**

 * Implement hook_entity_info().

 */

function breadcrumb2_entity_info() {

  $return = array(

    'breadcrumb2' => array(

      'label' => t('Breadcrumb'),

      'plural label' => t('Breadcrumbs'),

      'description' => t('Breadcrumb2 entity breadcrumbs.'),

      'entity class' => 'Breadcrumb',

      'controller class' => 'EntityAPIController',

      'base table' => 'breadcrumb',

      'fieldable' => TRUE,

      'view modes' => array(

        'full' => array(

          'label' => t('Breadcrumb'),

          'custom settings' => FALSE,

        ),

      ),

      'entity keys' => array(

        'id' => 'pid',

      ),

      'bundles' => array(

        'breadcrumb2' => array(

          'label' => t('Breadcrumb'),

          'admin' => array(

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

            'access arguments' => array('administer breadcrumbs'),

          ),

        ),

      ),

      'bundle keys' => array(

        'bundle' => 'type',

      ),

      'uri callback' => 'entity_class_uri',

      'access callback' => 'breadcrumb2_access',

      'module' => 'breadcrumb2',

      'metadata controller class' => 'Breadcrumb2MetadataController'

    ),

  );

  return $return;

}

首先,这个钩子的实现,我们主要参考了profile2_entity_info的实现,这里是以它为基础做的修改。其次,由于我们这里只有一个bundle,并且不允许创建其它的bundle,这个和user实体非常类似,所以中间的部分代码,我们借鉴的是user_entity_info的实现,我们来看一下user模块的实现:

function user_entity_info() {

  $return = array(

    'user' => array(

      'label' => t('User'),

      'controller class' => 'UserController',

      'base table' => 'users',

      'uri callback' => 'user_uri',

      'label callback' => 'format_username',

      'fieldable' => TRUE,

      // $user->language is only the preferred user language for the user

      // interface textual elements. As it is not necessarily related to the

      // language assigned to fields, we do not define it as the entity language

      // key.

      'entity keys' => array(

        'id' => 'uid',

      ),

      'bundles' => array(

        'user' => array(

          'label' => t('User'),

          'admin' => array(

            'path' => 'admin/config/people/accounts',

            'access arguments' => array('administer users'),

          ),

        ),

      ),

      'view modes' => array(

        'full' => array(

          'label' => t('User account'),

          'custom settings' => FALSE,

        ),

      ),

    ),

  );

  return $return;

}

我们的'entity keys''bundles''view modes',都是从user_entity_info借鉴过来的。所谓借鉴,就是将它们的代码复制过来,然后改成我们自己的。比葫芦画瓢。比如'entity keys',最初这个是从profile2中借鉴过来的,profile2_entity_info中这样定义的:

      'entity keys' => array(

        'id' => 'pid',

        'bundle' => 'type',

        'label' => 'label',

      ),

我们知道,pidprofile里面的主键,我们将它修改为bid,就成了最初的样子:

      'entity keys' => array(

        'id' => 'bid',

        'bundle' => 'type',

        'label' => 'label',

      ),

 

然后再借鉴一下user里面的实现,这个时候,我们会发现里面的'bundle''label'没有什么用,把它们删除,就成了现在的样子:

      'entity keys' => array(

        'id' => 'pid',

      ),

Drupal里面的很多钩子,尤其是这种带有info后缀的钩子,里面通常是一个大的数组,遇到这样的钩子,我们学习的路径,最好是找个类似的实现作为参考,当然我们还需要阅读这个钩子的文档,弄清楚里面的键值的具体含义,不过大部分键的含义,从字面上很容易理解出来,比如这里的'label''plural label''description''entity class''controller class''base table''fieldable'等。

来自 http://www.thinkindrupal.com/node/5749

普通分类: