thecodingmachine / easy-entity-reader
此 Drupal 8 模块帮助开发者访问实体内容。
1.0.x-dev
2018-01-19 15:56 UTC
Requires
- php: >=7.0
- drupal/core: ~8.0
Requires (Dev)
- drupal/color_field: ^2.0
- drupal/field_collection: ~1.0
This package is auto-updated.
Last update: 2024-09-07 00:32:37 UTC
README
本包针对 Drupal 8。
它为开发者提供了一种方便访问实体内容的方法。
为什么选择它?
Drupal 访问实体时语法相当繁琐。
厌倦了输入这样的代码吗?
$entity->get('field_my_field')->getValue()[0]['value']
这个包就是为你准备的解决方案!
它是如何工作的?
该包在 Drupal 容器中注册了一个新的服务:easy_entity_adapter.wrapper
。
此服务可以将实体“包装”为另一个更容易访问的对象。您可以直接访问“包装”实体的值(使用数组访问记法)。
以下是一个示例
// Let's assume you have a $entity variable containing an entity. $wrapper = \Drupal::get('easy_entity_adapter.wrapper'); $easyEntity = $wrapper->wrap($entity); // Now, you can access parts of your entity very easily. $title = $easyEntity['title']; // $title is directly a string $references = $easyEntity['my_custom_references']; // If the cardinality of the 'my_custom_references' is > 1, then the $references is automatically an array. // Even better, referenced nodes are automatically fetched and converted into wrapped entities. // So you can do something like: $titleOfTheReferencedNode = $easyEntity['my_custom_references'][0]['title'];
安装
简单使用
composer require thecodingmachine/easy.entity.adapter
Twig 集成
从 Twig,您可以使用 easy_entity
函数将实体包装到适配器中。
例如
{{ easy_entity(node).title }}