You are here

7.7 定义实体类

admin 的头像
Submitted by admin on 星期一, 2015-08-31 10:21

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

我们在breadcrumb2_entity_info里面,将'entity class'指定为了Breadcrumb,现在我们来看看这个类的定义。向breadcrumb2.module里面添加以下代码:

/**

 * The class used for breadcrumb entities

 */

class Breadcrumb extends Entity {

  

  public function __construct($values = array()) {

    parent::__construct($values, 'breadcrumb');

  }

 

  protected function defaultLabel() {

    return $this->path;

  }

 

  protected function defaultUri() {

    return array('path' => 'breadcrumb/' . $this->bid);

  }

}

  类似的代码可以参看model里面的Model类的实现。

   

  我们将'controller class'设置为了EntityAPIController,当然,我们也可以有自己的实现,比如将其设置为BreadcrumbController。此时需要定义这个控制器类,我们向module文件追加以下代码:

 

/**

 * The Controller for Breadcrumb entities

 */

class BreadcrumbController extends EntityAPIController {

  public function __construct($entityType) {

    parent::__construct($entityType);

  }

 

  /**

   * Create a breadcrumb - we first set up the values that are specific

   * to our breadcrumb schema but then also go through the EntityAPIController

   * function.

   * 

   * @param $type

   *   The machine-readable type of the breadcrumb.

   *

   * @return

   *   A breadcrumb object with all default fields initialized.

   */

  public function create(array $values = array()) {

    // Add values that are specific to our Breadcrumb

    $values += array( 

      'bid' => '',

    );

    

    $breadcrumb = parent::create($values);

    return $breadcrumb;

  }

  

这里也是模仿的model模块的实现。


Drupal版本: