thecodingmachine/easy-entity-reader

This Drupal 8 module helps developers access entity content.

Installs: 11 863

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 9

Forks: 2

Open Issues: 0

Type:drupal-module

1.0.x-dev 2018-01-19 15:56 UTC

This package is auto-updated.

Last update: 2024-11-07 00:48:16 UTC


README

This package targets Drupal 8.

It offers developers a friendly way of accessing their entities.

Latest Stable Version Total Downloads Latest Unstable Version License Scrutinizer Code Quality Build Status Coverage Status

Why?

Drupal has kind of a very verbose syntax to access entities.

Fed up of typing code like this?

$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:

// 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:

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:

{{ easy_entity(node).title }}