drupal 自定义 单表 entity 所有字段在一张表里面 并可以配置views

drupal 自定义 单表 entity 所有字段在一张表里面

举例使用创建一张简单的股票代码表, 最小化实现

模块: k_line_analysis

文件: k_line_analysis.info.yml

name: 'k line analysis'
type: module
description: 'k line analysis'
package: 'lion modules'
core: 8.x

文件: <module file>/src/Entity/K_Line_Analysis.php

附了几种字段例子,更详细的可以参考:
https://www.drupal.org/docs/8/api/entity-api/introduction-to-entity-api-in-drupal-8

https://www.drupal.org/docs/8/api/entity-api/introduction-to-entity-api-in-drupal-8

注意: 文件里面的注释也是必须的格式!!!

<?php

namespace Drupal\k_line_analysis\Entity;

use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\ContentEntityInterface;


/**
 * Defines the lion entity.
 *
 * @ingroup lion
 *
 * @ContentEntityType(
 *   id = "k_line_analysis",
 *   label = @Translation("k_line_analysis"),
 *   handlers = {
 *     "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
 *     "list_builder" = "Drupal\Core\Entity\EntityListBuilder",
 *     "access" = "Drupal\Core\Entity\EntityAccessControlHandler",
 *     "views_data" = "Drupal\views\EntityViewsData",
 *     "form" = {
 *       "default" = "Drupal\Core\Entity\ContentEntityForm",
 *       "add" = "Drupal\Core\Entity\ContentEntityForm",
 *       "edit" = "Drupal\Core\Entity\ContentEntityForm",
 *       "delete" = "Drupal\Core\Entity\ContentEntityDeleteForm",
 *      },
 *   },
 *   base_table = "k_line_analysis",
 *   fieldable = TRUE,
 *   bundle_entity_type = "node_type",
 *   admin_permission = "administer most_simple types",
 *   entity_keys = {
 *     "id" = "id",
 *     "label" = "code",
 *   },
 * )
 */
class K_Line_Analysis extends ContentEntityBase implements ContentEntityInterface {

  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {

    // Standard field, used as unique if primary index.
    $fields['id'] = BaseFieldDefinition::create('integer')
      ->setLabel(t('ID'))
      ->setDescription(t('The ID of the k_line_analysis entity.'))
      ->setReadOnly(TRUE);

    $fields['code'] = BaseFieldDefinition::create('string')
      ->setLabel(t('股票代码'))
      ->setDescription(t('股票代码'))
      ->setDefaultValue(NULL);

    $fields['entity_ref'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('entity_reference'))
      ->setDescription(t('entity_reference'))
      ->setDefaultValue(NULL);

    $fields['date'] = BaseFieldDefinition::create('created')
      ->setLabel(t('日期'))
      ->setDescription(t('日期'))
      ->setDefaultValue(NULL);

    $fields['open'] = BaseFieldDefinition::create('decimal')
      ->setLabel(t('开盘价'))
      ->setDescription(t('开盘价'))
      ->setSettings(array(
        'precision' => 10,
        'scale' => 3,
      ))
      ->setDefaultValue(NULL);

    $fields['ktype'] = BaseFieldDefinition::create('string')
      ->setLabel(t('K线类型'))
      ->setDescription(t('日K, 5分钟, 15分钟, 30分钟'))
      ->setDefaultValue(NULL);

    return $fields;
  }
}

如果需要多个类似的entity那就多增加几个php文件就可以了

分类