https://github.com/gertvdb/entity_field_helper
Helper for getting field values from entities.
https://github.com/gertvdb/entity_field_helper
drupal
Last synced: 5 months ago
JSON representation
Helper for getting field values from entities.
- Host: GitHub
- URL: https://github.com/gertvdb/entity_field_helper
- Owner: gertvdb
- License: mit
- Created: 2018-10-09T18:25:48.000Z (over 7 years ago)
- Default Branch: main
- Last Pushed: 2024-11-06T18:19:24.000Z (over 1 year ago)
- Last Synced: 2025-08-21T01:46:16.340Z (10 months ago)
- Topics: drupal
- Language: PHP
- Homepage:
- Size: 227 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Entity Field Helper
[](https://github.com/gertvdb/entity_field_helper/blob/8.x-1.x/LICENSE.md)
[](https://travis-ci.org/gertvdb/entity_field_helper)
[](https://coveralls.io/github/gertvdb/entity_field_helper?branch=8.x-1.x)
[](https://packagist.org/packages/gertvdb/entity_field_helper)
Description
-----------
This module provides a helper class for getting values from entities.
The main principle of the module is that the value functions will always
return NULL or the actual value when found. This makes it easy to handle
printing and sending data to the templates.
Installation
------------
To install this module, do the following:
With composer:
1. ```composer require gertvdb/entity_field_helper```
Examples
--------
You can find an example on how to use the entity field helper below.
The entity field helper classes are plugins so other modules can provide
extra helpers.
#### Getting a Known helper (Provided by module).
The entity field helper provides several plugin for most of the
field types in Drupal Core. The plugin manager contains shortcuts to get
those helpers.
##### Basic.
```
/** @var \Drupal\entity_field_helper\Plugin\EntityFieldHelperManager $pluginManager */
$pluginManager = \Drupal::service('plugin.manager.entity_field_helper');
// Get the node object.
$node = Node::load(1);
$processedTextHelper = $pluginManager->processedTextHelper();
$processedTextHelper->getValue($node, 'body');
```
##### Advanced.
```
/** @var \Drupal\entity_field_helper\Plugin\EntityFieldHelperManager $pluginManager */
$pluginManager = \Drupal::service('plugin.manager.entity_field_helper');
// Get the node object.
$node = Node::load(1);
$imageHelper = $pluginManager->imageHelper();
$visuals = $imageHelper->getValues($node, 'field_visuals');
$images = [];
$imageStyle = ImageStyle::load('slider');
foreach ($visuals as $visual) {
$image = $imageHelper->getImage($visual);
$imageWidth = $image->getWidth();
if ($imageWidth > 1000) {
$images[] = $imageHelper->getImageStyleUrl($visual, $imageStyle);
}
}
```
##### Custom helper.
```
namespace Drupal\entity_field_helper\Plugin\EntityFieldHelper;
use Drupal\entity_field_helper\Plugin\EntityFieldHelperBase;
/**
* Provides a General Entity Field Helper.
*
* @EntityFieldHelper(
* id = "my_custom_helper",
* name = "My Custom Helper",
* )
*/
final class MyCustomHelper extends EntityFieldHelperBase {}
```
```
/** @var \Drupal\entity_field_helper\Plugin\EntityFieldHelperManager $pluginManager */
$pluginManager = \Drupal::service('plugin.manager.entity_field_helper');
$myCustomHelper = $pluginManager->createInstance('my_custom_helper');
```