Drupal菜鸟笔记之自定义内容实体及踩坑

39 篇文章 0 订阅

因为drupal自带的log系统在项目上线之后不方便使用,于是就想自定义一个简单的日志实体,用于一些关键功能,方便出现问题的时候容易排查跟踪。
因为是自己使用,不需要界面,因此是按照最简单做法。
1、新建一个log模块,创建module_name.info.yml文件,这里的module_name就是模块名称。
在这里插入图片描述
2、创建日志实体:
在模块文件下创建src文件夹,在src文件夹下创建Entity文件夹,然后在Entity下创建实体文件:比如我这里是IxtendCustomLog.php
我这里只是实现了 baseFieldDefinitions 方法,其他的都不需要。

<?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;

/**
 * @ContentEntityType (
 *   id = "ixtend_custom_log",
 *   label = @Translation("Ixtend Custom Log"),
 *   base_table = "ixtend_custom_log",
 *   entity_keys = {
 *     "id" = "id",
 *     "uuid" = "uuid",
 *   },
 *   handlers = {
 *     "views_data" = "Drupal\views\CustomLogViewsData",
 *   }
 * )
 */
class IxtendCustomLog extends ContentEntityBase implements ContentEntityInterface {

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

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

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

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

    return $fields;
  }

  public static function getDefaultEntityCreator() {
    return \Drupal::currentUser()->id();
  }
}

最简单的实体到这里就够了。在后台的Extend搜索模块名并安装,然后清理缓存,就可以了。这个时候在数据库就可以看到对应的数据表。

踩坑记录:
1、The id field definition does not exist and it is used as id entity key
https://ask.csdn.net/questions/7521830
在安装module的时候出现这个错误,这个折磨了两三天,最后才发现是因为实体的ID和模块的名称重复了。我第一次是直接修改实体ID,然后清缓存,重启站点都没有效果,最后是删掉实体,重新创建实体才生效。
在这里插入图片描述
2、实体安装后,没有数据表,也没报错。
这个也是琢磨很久,参考了别人的代码,发现少了下面这一行代码,就加上试了下,还真就好了。
在这里插入图片描述
3、安装好之后就测试下添加数据。
两种添加方法:

$res = \Drupal::database()->insert('ixtend_custom_log')
      ->fields(['uuid', 'message'], ['6af09a0f','test1'])
      ->execute();

    $obj = IxtendCustomLog::create();
    $obj->set('message', '1233435');
    $obj->save();

第一种除了主键ID会自增,其他的必须代码添加,不会自动生成数据。
第二种像uuid,creator,created都会自动生成数据,不需要指定。
这里在使用第二种方法的时候踩坑了,creator没有数据,最后发现是实体文件里面的问题:

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

这段代码中,setDefaultValueCallback(static::class . ‘::getDefaultEntityCreator’)括号内的参数是使用的 . 链接,而我最开始没看清楚,以为是两个参数,所以用的 , 分隔,所以无法设置默认值。

好了,以上就是自定义实体的尝试以及踩坑分享,大佬勿喷,有错欢迎大佬指正,谢谢!

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值