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+ }
0 commit comments