https://github.com/thecodingmachine/easy-entity-reader
This Drupal 8 module helps developers access entity content.
https://github.com/thecodingmachine/easy-entity-reader
Last synced: 9 months ago
JSON representation
This Drupal 8 module helps developers access entity content.
- Host: GitHub
- URL: https://github.com/thecodingmachine/easy-entity-reader
- Owner: thecodingmachine
- Created: 2016-12-15T12:28:40.000Z (about 9 years ago)
- Default Branch: 1.0
- Last Pushed: 2018-01-19T15:56:50.000Z (about 8 years ago)
- Last Synced: 2025-04-11T18:59:46.949Z (10 months ago)
- Language: PHP
- Size: 15.6 KB
- Stars: 2
- Watchers: 8
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Easy Entity Adapter
===================
This package targets Drupal 8.
It offers developers a friendly way of accessing their entities.
[](https://packagist.org/packages/thecodingmachine/easy-entity-reader)
[](https://packagist.org/packages/thecodingmachine/easy-entity-reader)
[](https://packagist.org/packages/thecodingmachine/easy-entity-reader)
[](https://packagist.org/packages/thecodingmachine/easy-entity-reader)
[](https://scrutinizer-ci.com/g/thecodingmachine/easy-entity-reader/?branch=1.0)
[](https://travis-ci.org/thecodingmachine/easy-entity-reader)
[](https://coveralls.io/github/thecodingmachine/easy-entity-reader?branch=1.0)
Why?
----
Drupal has kind of a very verbose syntax to access entities.
Fed up of typing code like this?
```php
$entity->get('field_my_field')->getValue()[0]['value']
```
This package has the solution for you!
How does it works?
------------------
This package registers in the Drupal container a new service: `easy_entity_adapter.wrapper`.
This service can "wrap" an entity into another object that is way easier to access. You can access values of the "wrapped" entity directly (using the array access notation).
Here is a sample:
```php
// 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'];
```
Install
-------
Simply use:
```php
composer require thecodingmachine/easy.entity.adapter
```
Twig integration
----------------
From Twig, you can wrap an entity into the adapter using the `easy_entity` function.
For instance:
```twig
{{ easy_entity(node).title }}
```