openeuropa/entity_meta_relation

提供实体元关系实体及其相关行为。

安装量: 2,042

依赖项: 0

建议者: 0

安全性: 0

星标: 0

关注者: 16

分支: 0

开放问题: 1

类型:drupal-module

dev-8.x-1.x 2024-07-05 07:22 UTC

README

实体元关系模块允许将存储在独立实体(元实体)中的额外信息与内容(宿主)实体关联起来。这避免了将此类信息作为内容实体字段存储并污染内容实体,同时将控制特定实体行为的元数据信息保留在主要存储之外。

内容结构

  • EntityMeta 实体是常规内容实体,可以是可字段化和可修订的。
  • EntityMetaRelation 实体持有内容实体(如节点)及其 EntityMeta 实体之间的关系。关系直接与内容实体修订和 EntityMeta 修订相关联,允许在两个方向上跟踪新修订。 EntityMeta 实体需要在自身和内容(宿主)实体之间定义一个关系。此外,每个 EntityMeta 包束通过第三方设置中定义的配置适用于每个内容实体包束。所需配置可以通过使用 EntityMetaRelationInstaller 服务自动设置。其用法的一个示例可以在 entity_meta_example_install 中看到。 entity_meta_example 模块包含几个用于测试的示例插件,但也可以用作参考。

对新内容实体的支持

实体元关系可用于任何内容实体。

然而,emr_node 模块已经提供了对 Node 实体的支持。它可以用来了解如何为其他内容实体提供支持。

  • 应创建一个新的 EntityMetaRelation 包束,至少包含两个字段
    • 一个 EntityReferenceRevision 字段,用于针对(宿主)内容实体修订(例如,emr_node_revision 字段作为与节点实体相关联的示例)。
    • 一个字段,用于针对 EntityMeta 修订(例如,emr_meta_revision 字段作为与 EntityMeta 实体相关联的示例。此字段的存储可以重复使用)。
  • 应修改宿主实体定义,以包含以下属性
    • entity_meta_relation_bundle:应指定上面创建的 EntityMetaRelation 包束。
    • entity_meta_relation_content_field:应指定与上面创建的内容(宿主)实体相关联的字段名称。
    • entity_meta_relation_meta_field:应指定与上面创建的 EntityMeta 实体相关联的字段名称。
    • emr_content_form:如果内容(宿主)实体表单应用于包含用于操作相关 EntityMeta 实体的表单,则应指定用于此目的的表单处理程序类。

有关更多信息,请检查 emr_node_entity_type_alter 以了解这是如何为节点完成的示例。

以下定义如下

  • EntityMetaRelation 包束为 node_meta_relation,并且有两个字段
    • emr_node_revision:指向 Node 实体修订
    • emr_meta_revision:指向 EntityMeta 修订
  • 已定义处理程序类 NodeFormHandler 以处理内容实体表单更改。

实体元关系插件

EntityMeta 实体可以通过 EntityMetaRelation 插件进行管理。插件应在其定义中指明其关联的 EntityMeta 包束:

例如,从 Drupal\entity_meta_speed\Plugin\EntityMetaRelation\SpeedConfiguration

@EntityMetaRelation(
 *   id = "speed",
 *   label = @Translation("Speed"),
 *   entity_meta_bundle = "speed",
 *   content_form = TRUE,
 *   entity_meta_wrapper_class = "\Drupal\entity_meta_speed\SpeedEntityMetaWrapper",
 *   description = @Translation("Speed.")
 * )

此插件使用 EntityMeta 包束 "speed" 并提供通过 \Drupal\entity_meta_speed\SpeedEntityMetaWrapper 操作其数据的包装器类。包装器是可选的。

使用实体元关系API

内容(宿主)实体可以直接使用计算字段 emr_entity_metas 来操作它们的 EntityMeta 实体。

向内容实体添加单个 EntityMeta 实体

$node_storage = \Drupal::entityTypeManager()->getStorage('node');

// Create a new node.

/** @var \Drupal\node\NodeInterface $node */
$node = $node_storage->create([
  'type' => 'entity_meta_multi_example',
  'title' => 'Node test',
]);


/** @var \Drupal\emr\Field\EntityMetaItemListInterface $entity_meta_list */
$entity_meta_list = $node->get('emr_entity_metas');

// Instantiate a new entity meta of a given bundle. Since it doesn't exist,
// it will be created on the fly.
/** @var \Drupal\emr\Entity\EntityMetaInterface $entity_meta */
$entity_meta = $entity_meta_list->getEntityMeta('visual');

// Set a meta value.
$entity_meta->set('field_color', 'red');

// Attach the new entity meta. This will add it to the list and once the
// node is saved, the relations are created automatically.
$entity_meta_list->attach($entity_meta);
$node->save();

 

向内容实体添加多个 EntityMeta 实体

$node_storage = \Drupal::entityTypeManager()->getStorage('node');

// Create a new node.

/** @var \Drupal\node\NodeInterface $node */
$node = $node_storage->create([
  'type' => 'entity_meta_multi_example',
  'title' => 'Node test',
]);


/** @var \Drupal\emr\Field\EntityMetaItemListInterface $entity_meta_list */
$entity_meta_list = $node->get('emr_entity_metas');

$entity_metas = [];
/** @var \Drupal\emr\Entity\EntityMetaInterface $entity_meta */
$entity_meta = $entity_meta_list->getEntityMeta('visual');
$entity_meta->set('field_color', 'red');
$entity_metas[] = $entity_meta;

/** @var \Drupal\emr\Entity\EntityMetaInterface $entity_meta */
$entity_meta = $entity_meta_list->getEntityMeta('audio');
$entity_meta->set('field_volume', 'low');
$entity_metas[] = $entity_meta;

// Set the array of entites meta entities as values.
$entity_meta_list->set($entity_metas);
$node->save();

 

使用 EntityMetaWrapper

可以通过定义 EntityMetaWrapper 与 EntityMeta 实体交互,而不需要直接与其字段和配置交互。一个包装器通过 entity_meta_wrapper_class 属性与 EntityMetaRelationPlugin 关联,因此,而不是

$example_meta = $node->get('emr_entity_metas')->getEntityMeta('audio');
$example_meta->set('field_volume', 'medium');

 

$example_meta = $node->get('emr_entity_metas')->getEntityMeta('audio');
$example_meta->getWrapper()->setVolume('medium');

 

更改现有的 EntityMeta

可以通过 attach() 方法更改现有的 EntityMeta。

$example_meta = $node->get('emr_entity_metas')->getEntityMeta('audio');
$example_meta->getWrapper()->setVolume('medium');
$node->get('emr_entity_metas')->attach($example_meta);
$node->save();

请注意,如果 EntityMeta 实体中没有检测到任何更改,则 attach() 方法不会做任何事情。

删除现有的 EntityMeta

可以通过 detach() 方法从修订版中删除现有的 EntityMeta 实体。

$example_meta = $node->get('emr_entity_metas')->getEntityMeta('example_bundle');
$node->get('emr_entity_metas')->detach($example_meta);
$node->save();

 

开发设置

 

  • 安装 Composer 依赖项:
composer install

 

composer install 之后,会自动触发一个后置命令钩子(drupal:site-setup)。它将确保在开发站点的必要符号链接正确设置。它还会在开发配置文件(如 behat.yml.dist)中执行令牌替换。

  • 这还将
  • ./build/modules/custom/entity_meta_relation 中创建主题的符号链接,以便它在测试站点中可用
  • 使用 ./runner.yml.dist 中的值设置 Drush 和 Drupal 的设置。

请注意:项目文件和目录通过使用 OpenEuropa Task Runner 的 Drupal 项目符号链接 命令在测试站点内部进行符号链接。

如果您在项目根目录中添加了新的文件或目录,则需要重新运行 drupal:site-setup,以确保它们被正确符号链接。

如果您不想重新运行完整的站点设置,则可以简单地运行

$ ./vendor/bin/run drupal:symlink-project

 

  • 通过以下方式安装测试站点:
./vendor/bin/run drupal:site-install

 

使用 Docker Compose

 

要求

 

配置

 

用法

 

docker-compose up

 

docker-compose up -d

 

docker-compose exec web composer install
docker-compose exec web ./vendor/bin/run drupal:site-install

使用默认配置,开发站点的文件应位于 build 目录,开发站点应可访问: http://127.0.0.1:8080/build

运行测试

运行 grumphp 检查:

docker-compose exec web ./vendor/bin/grumphp run

运行 phpunit 测试:

docker-compose exec web ./vendor/bin/phpunit

运行 behat 测试:

docker-compose exec web ./vendor/bin/behat

步骤调试

要从命令行启用步骤调试,将任何值传递给容器的 XDEBUG_SESSION 环境变量

docker-compose exec -e XDEBUG_SESSION=1 web <your command>

请注意,从 XDebug 3 开始,如果设置了变量但您的客户端没有监听调试连接,则会在控制台中输出连接错误消息。该错误消息将导致 PHPUnit 测试出现假阴性。

要从浏览器中开始步骤调试,请使用浏览器扩展或类似这些的收藏夹书签设置正确的cookie。

贡献

请阅读完整文档以了解我们的行为准则以及向我们提交拉取请求的流程。

版本控制

我们使用 SemVer 进行版本控制。有关可用版本,请参阅此存储库的 标签