魑魅魍魉魃魈魁魅 2021-09-30 16:12
浏览 17
已结题

drupal 自定义实体报错:The id field definition does not exist and it is used as id entity key

#The id field definition does not exist and it is used as id entity key
我自定义了一个日志实体,但是在安装module的时候报错:The id field definition does not exist and it is used as id entity key

img

#代码:

img

<?php

namespace Drupal\ixtend_log\Entity;

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

/**
 * Defines the ixtend_custom_log entity.
 *
 * @ContentEntityType(
 *   id = "ixtend_custom_log",
 *   label = @Translation ("Ixtend Log"),
 *   base_table = "ixtend_log",
 *   entity_keys = {
 *     "id" = "id",
 *     "uuid" = "uuid",
 *   },
 *   handlers = {
 *     "views_data" = "Drupal\views\IxtendLogViewsData",
 *   },
 * )
 */
class IxtendLog extends ContentEntityBase implements ContentEntityInterface {

  /**
   * {@inheritdoc}
   */
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
    $fields = parent::baseFieldDefinitions($entity_type);

    $fields['user_id'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Created by'))
      ->setSetting('target_type', 'user')
      ->setDefaultValueCallback(static::class . "::getDefaultEntityCreator");

    $fields['created'] = BaseFieldDefinition::create('created')
      ->setLabel(t('Created'))
      ->setDescription(t('The time that the log created.'));

    $fields['message'] = BaseFieldDefinition::create('string_long')
      ->setLabel(t('Message'));

  }

  /**
   * Default value callback for 'uid' base field.
   *
   * @return mixed
   *   A default value for the uid field.
   */
  public static function getDefaultEntityCreator() {
    return \Drupal::currentUser()->id();
  }

}


  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 10月8日
    • 创建了问题 9月30日