Skip to content

Commit 3323187

Browse files
author
Maarten Heip
committed
DRUPAL-HELPERS: Add entity helpers to retrieve values and translations
1 parent 62d3ecf commit 3323187

File tree

4 files changed

+361
-0
lines changed

4 files changed

+361
-0
lines changed

src/EntityTraitWrapper.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Intracto\DrupalHelpers;
4+
5+
use Intracto\DrupalHelpers\Traits\EntityHelperTrait;
6+
use Intracto\DrupalHelpers\Traits\ParagraphHelperTrait;
7+
8+
/**
9+
* Class EntityTraitWrapper.
10+
*
11+
* This class includes the helper traits so we can use it inside modules.
12+
*
13+
* @package Intracto\DrupalHelpers
14+
*/
15+
class EntityTraitWrapper {
16+
17+
use EntityHelperTrait;
18+
use ParagraphHelperTrait;
19+
20+
}

src/Traits/EntityHelperTrait.php

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
<?php
2+
3+
namespace Intracto\DrupalHelpers\Traits;
4+
5+
use Drupal\Core\Entity\EntityInterface;
6+
use Drupal\Core\Entity\FieldableEntityInterface;
7+
use Drupal\Core\Field\FieldItemListInterface;
8+
use Drupal\Core\TypedData\TypedDataInterface;
9+
10+
/**
11+
* Trait EntityTranslationTrait.
12+
*
13+
* Provides helper functions to deal with entity value retrieval.
14+
*
15+
* @package Intraco\DrupalHelpers
16+
*/
17+
trait EntityHelperTrait {
18+
19+
use EntityTranslationTrait;
20+
21+
/**
22+
* Gets the entity field value.
23+
*
24+
* @param \Drupal\Core\Entity\EntityInterface $entity
25+
* The entity for which to retrieve the field value.
26+
* @param string $field
27+
* The name of the field to return.
28+
*
29+
* @return null|string
30+
* The entity field value.
31+
*/
32+
public function getEntityFieldValue(EntityInterface $entity, string $field) : ?string {
33+
if (!$firstItem = $this->getFirstEntityFieldItem($entity, $field)) {
34+
return NULL;
35+
}
36+
37+
if (!$value = $firstItem->value) {
38+
return NULL;
39+
}
40+
41+
return $value;
42+
}
43+
44+
/**
45+
* Gets the entity field value.
46+
*
47+
* @param \Drupal\Core\Entity\EntityInterface $entity
48+
* The entity for which to retrieve the field value.
49+
* @param string $field
50+
* The name of the field to return.
51+
*
52+
* @return array
53+
* The entity field values.
54+
*/
55+
public function getEntityFieldValues(EntityInterface $entity, string $field) : array {
56+
if (!$list = $this->getEntityFieldList($entity, $field)) {
57+
return [];
58+
}
59+
60+
$values = [];
61+
62+
foreach ($list as $fieldItem) {
63+
if (!$value = $fieldItem->value) {
64+
continue;
65+
}
66+
67+
$values[] = $value;
68+
}
69+
70+
return $values;
71+
}
72+
73+
/**
74+
* Returns the referenced entities from a entity_reference field.
75+
*
76+
* @param \Drupal\Core\Entity\EntityInterface $entity
77+
* The entity for which to retrieve the field value.
78+
* @param string $field
79+
* The name of the field to return.
80+
* @param bool $translated
81+
* Use the current entity langcode to return translated entities?
82+
* @param bool $removeUntranslated
83+
* Skip a referenced entity that is not translated in the correct langcode.
84+
*
85+
* @return array
86+
*/
87+
public function getReferencedEntitiesByField(EntityInterface $entity, string $field, bool $translated = TRUE, bool $removeUntranslated = FALSE) : array {
88+
if (!$list = $this->getEntityFieldList($entity, $field)) {
89+
return [];
90+
}
91+
92+
$entities = [];
93+
94+
foreach ($list as $fieldItem) {
95+
$entity = $fieldItem->entity;
96+
97+
if (!$entity) {
98+
continue;
99+
}
100+
101+
$entities[] = $entity;
102+
}
103+
104+
if (!$translated) {
105+
return $entities;
106+
}
107+
108+
$activeLangcode = $entity->language()->getId();
109+
return $this->translateEntities($entities, $activeLangcode, $removeUntranslated);
110+
}
111+
112+
/**
113+
* Returns the referenced entities from a entity_reference field.
114+
*
115+
* @param \Drupal\Core\Entity\EntityInterface $entity
116+
* The entity for which to retrieve the field value.
117+
* @param string $field
118+
* The name of the field to return.
119+
* @param bool $translated
120+
* Use the current entity langcode to return translated entities?
121+
* @param bool $removeUntranslated
122+
* Skip a referenced entity that is not translated in the correct langcode.
123+
*
124+
* @return \Drupal\Core\Entity\EntityInterface|null
125+
* The referenced entity.
126+
*/
127+
public function getReferencedEntityByField(EntityInterface $entity, string $field, bool $translated = TRUE, bool $removeUntranslated = FALSE) : ?EntityInterface {
128+
if (!$fieldItem = $this->getFirstEntityFieldItem($entity, $field)) {
129+
return NULL;
130+
}
131+
132+
if (!$referencedEntity = $fieldItem->entity) {
133+
return NULL;
134+
}
135+
136+
if (!$translated) {
137+
return $referencedEntity;
138+
}
139+
140+
$activeLangcode = $entity->language()->getId();
141+
return $this->translateEntity($referencedEntity, $activeLangcode, $removeUntranslated);
142+
}
143+
144+
/**
145+
* Checks if an entity has a field and it has data.
146+
*
147+
* @param \Drupal\Core\Entity\EntityInterface $entity
148+
* The entity for which to retrieve the field value.
149+
* @param string $field
150+
* The name of the field to return.
151+
*
152+
* @return \Drupal\Core\Field\FieldItemListInterface|null
153+
* Whether or not entity has the field and has data for it.
154+
*/
155+
public function getEntityFieldList(EntityInterface $entity, string $field) : ?FieldItemListInterface {
156+
if (!$entity instanceof FieldableEntityInterface) {
157+
return NULL;
158+
}
159+
160+
if (!$entity->hasField($field)) {
161+
return NULL;
162+
}
163+
164+
$field = $entity->get($field);
165+
166+
if ($field->isEmpty()) {
167+
return NULL;
168+
}
169+
170+
return $field;
171+
}
172+
173+
/**
174+
* Returns the first item of a field list.
175+
*
176+
* @param \Drupal\Core\Entity\EntityInterface $entity
177+
* The entity for which to retrieve the field item.
178+
* @param string $field
179+
* The name of the field to return.
180+
*
181+
* @return \Drupal\Core\TypedData\TypedDataInterface|null
182+
* The field.
183+
*/
184+
public function getFirstEntityFieldItem(EntityInterface $entity, string $field) : ?TypedDataInterface {
185+
if (!$list = $this->getEntityFieldList($entity, $field)) {
186+
return NULL;
187+
}
188+
189+
return $list->first();
190+
}
191+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
namespace Intracto\DrupalHelpers\Traits;
4+
5+
use Drupal\Core\Entity\EntityInterface;
6+
use Drupal\Core\Entity\TranslatableInterface;
7+
8+
/**
9+
* Trait EntityTranslationTrait.
10+
*
11+
* Provides helper functions to deal with entity translations.
12+
*
13+
* @package Intraco\DrupalHelpers
14+
*/
15+
trait EntityTranslationTrait {
16+
17+
/**
18+
* Checks if an entity has a translations and returns the translated version.
19+
*
20+
* @param \Drupal\Core\Entity\EntityInterface $entity
21+
* An entity that might contain a translation.
22+
* @param string $langcode
23+
* The langcode in which to translate, leave empty to use current langcode.
24+
* @param bool $required
25+
* If an entity is not translated, returns NULL if set to TRUE.
26+
*
27+
* @return \Drupal\Core\Entity\EntityInterface|null
28+
* The translated entity or null.
29+
*/
30+
public function translateEntity(EntityInterface $entity, string $langcode = NULL, bool $required = FALSE) : ?EntityInterface {
31+
if ($required) {
32+
$returnEntity = NULL;
33+
}
34+
else {
35+
$returnEntity = $entity;
36+
}
37+
38+
if (!$langcode) {
39+
$langcode = \Drupal::languageManager()
40+
->getCurrentLanguage()
41+
->getId();
42+
}
43+
44+
if (!$entity instanceof TranslatableInterface) {
45+
return $returnEntity;
46+
}
47+
48+
if (!$entity->hasTranslation($langcode)) {
49+
return $returnEntity;
50+
}
51+
52+
return $entity->getTranslation($langcode);
53+
}
54+
55+
/**
56+
* Loops over an array of entities and returns translated entities.
57+
*
58+
* @param array $entities
59+
* An array of entities.
60+
* @param string $langcode
61+
* The langcode in which to translate, leave empty to use current langcode.
62+
* @param bool $removeUntranslated
63+
* Removes entities that are not translated.
64+
*
65+
* @return array
66+
* An array of translated entities
67+
*/
68+
public function translateEntities(array $entities, string $langcode = NULL, bool $removeUntranslated = FALSE) : array {
69+
// Get the langcode first so we don't have to fetch it in the loop.
70+
if (!$langcode) {
71+
$langcode = \Drupal::languageManager()
72+
->getCurrentLanguage()
73+
->getId();
74+
}
75+
76+
foreach ($entities as $key => &$entity) {
77+
if (!$entity instanceof EntityInterface) {
78+
continue;
79+
}
80+
81+
$entity = $this->translateEntity($entity, $langcode, $removeUntranslated);
82+
83+
// Unset an entity that is now a NULL.
84+
// It will only be a NULL if translation is required.
85+
// This is passed by the removeUntranslated variable.
86+
if (!$entity) {
87+
unset($entities[$key]);
88+
}
89+
}
90+
91+
return $entities;
92+
}
93+
94+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Intracto\DrupalHelpers\Traits;
4+
5+
use Drupal\Core\Entity\EntityInterface;
6+
use Drupal\node\NodeInterface;
7+
use Drupal\paragraphs\ParagraphInterface;
8+
9+
/**
10+
* Trait ParagraphHelperTrait
11+
*
12+
* @package Intracto\DrupalHelpers\Traits
13+
*/
14+
trait ParagraphHelperTrait {
15+
16+
/**
17+
* Gets the node parent.
18+
*
19+
* @param \Drupal\paragraphs\ParagraphInterface $paragraph
20+
* The paragraph entity.
21+
*
22+
* @return \Drupal\Core\Entity\EntityInterface|null
23+
* The entity interface.
24+
*/
25+
public function getNodeParent(ParagraphInterface $paragraph) : ?EntityInterface {
26+
return self::getParentOfType($paragraph, NodeInterface::class);
27+
}
28+
29+
/**
30+
* @param \Drupal\Core\Entity\EntityInterface $entity
31+
* The entity for which to find a parent.
32+
* @param string $type
33+
* The parent type to return.
34+
*
35+
* @return \Drupal\Core\Entity\EntityInterface|null
36+
* The entity interface.
37+
*/
38+
public function getParentOfType(EntityInterface $entity, string $type) : ?EntityInterface {
39+
if (!$entity instanceof ParagraphInterface) {
40+
return NULL;
41+
}
42+
43+
$parent = $entity->getParentEntity();
44+
45+
if ($parent instanceof $type) {
46+
return $parent;
47+
}
48+
49+
if ($parent instanceof EntityInterface) {
50+
return self::getParentOfType($parent, $type);
51+
}
52+
53+
return NULL;
54+
}
55+
56+
}

0 commit comments

Comments
 (0)