From 3323187153a5e4336d619c6a77d373605c9448a7 Mon Sep 17 00:00:00 2001 From: Maarten Heip Date: Mon, 28 Jan 2019 15:16:46 +0100 Subject: [PATCH] DRUPAL-HELPERS: Add entity helpers to retrieve values and translations --- src/EntityTraitWrapper.php | 20 +++ src/Traits/EntityHelperTrait.php | 191 ++++++++++++++++++++++++++ src/Traits/EntityTranslationTrait.php | 94 +++++++++++++ src/Traits/ParagraphHelperTrait.php | 56 ++++++++ 4 files changed, 361 insertions(+) create mode 100644 src/EntityTraitWrapper.php create mode 100644 src/Traits/EntityHelperTrait.php create mode 100644 src/Traits/EntityTranslationTrait.php create mode 100644 src/Traits/ParagraphHelperTrait.php diff --git a/src/EntityTraitWrapper.php b/src/EntityTraitWrapper.php new file mode 100644 index 0000000..bb1aee3 --- /dev/null +++ b/src/EntityTraitWrapper.php @@ -0,0 +1,20 @@ +getFirstEntityFieldItem($entity, $field)) { + return NULL; + } + + if (!$value = $firstItem->value) { + return NULL; + } + + return $value; + } + + /** + * Gets the entity field value. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity for which to retrieve the field value. + * @param string $field + * The name of the field to return. + * + * @return array + * The entity field values. + */ + public function getEntityFieldValues(EntityInterface $entity, string $field) : array { + if (!$list = $this->getEntityFieldList($entity, $field)) { + return []; + } + + $values = []; + + foreach ($list as $fieldItem) { + if (!$value = $fieldItem->value) { + continue; + } + + $values[] = $value; + } + + return $values; + } + + /** + * Returns the referenced entities from a entity_reference field. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity for which to retrieve the field value. + * @param string $field + * The name of the field to return. + * @param bool $translated + * Use the current entity langcode to return translated entities? + * @param bool $removeUntranslated + * Skip a referenced entity that is not translated in the correct langcode. + * + * @return array + */ + public function getReferencedEntitiesByField(EntityInterface $entity, string $field, bool $translated = TRUE, bool $removeUntranslated = FALSE) : array { + if (!$list = $this->getEntityFieldList($entity, $field)) { + return []; + } + + $entities = []; + + foreach ($list as $fieldItem) { + $entity = $fieldItem->entity; + + if (!$entity) { + continue; + } + + $entities[] = $entity; + } + + if (!$translated) { + return $entities; + } + + $activeLangcode = $entity->language()->getId(); + return $this->translateEntities($entities, $activeLangcode, $removeUntranslated); + } + + /** + * Returns the referenced entities from a entity_reference field. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity for which to retrieve the field value. + * @param string $field + * The name of the field to return. + * @param bool $translated + * Use the current entity langcode to return translated entities? + * @param bool $removeUntranslated + * Skip a referenced entity that is not translated in the correct langcode. + * + * @return \Drupal\Core\Entity\EntityInterface|null + * The referenced entity. + */ + public function getReferencedEntityByField(EntityInterface $entity, string $field, bool $translated = TRUE, bool $removeUntranslated = FALSE) : ?EntityInterface { + if (!$fieldItem = $this->getFirstEntityFieldItem($entity, $field)) { + return NULL; + } + + if (!$referencedEntity = $fieldItem->entity) { + return NULL; + } + + if (!$translated) { + return $referencedEntity; + } + + $activeLangcode = $entity->language()->getId(); + return $this->translateEntity($referencedEntity, $activeLangcode, $removeUntranslated); + } + + /** + * Checks if an entity has a field and it has data. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity for which to retrieve the field value. + * @param string $field + * The name of the field to return. + * + * @return \Drupal\Core\Field\FieldItemListInterface|null + * Whether or not entity has the field and has data for it. + */ + public function getEntityFieldList(EntityInterface $entity, string $field) : ?FieldItemListInterface { + if (!$entity instanceof FieldableEntityInterface) { + return NULL; + } + + if (!$entity->hasField($field)) { + return NULL; + } + + $field = $entity->get($field); + + if ($field->isEmpty()) { + return NULL; + } + + return $field; + } + + /** + * Returns the first item of a field list. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity for which to retrieve the field item. + * @param string $field + * The name of the field to return. + * + * @return \Drupal\Core\TypedData\TypedDataInterface|null + * The field. + */ + public function getFirstEntityFieldItem(EntityInterface $entity, string $field) : ?TypedDataInterface { + if (!$list = $this->getEntityFieldList($entity, $field)) { + return NULL; + } + + return $list->first(); + } +} \ No newline at end of file diff --git a/src/Traits/EntityTranslationTrait.php b/src/Traits/EntityTranslationTrait.php new file mode 100644 index 0000000..7c27ccb --- /dev/null +++ b/src/Traits/EntityTranslationTrait.php @@ -0,0 +1,94 @@ +getCurrentLanguage() + ->getId(); + } + + if (!$entity instanceof TranslatableInterface) { + return $returnEntity; + } + + if (!$entity->hasTranslation($langcode)) { + return $returnEntity; + } + + return $entity->getTranslation($langcode); + } + + /** + * Loops over an array of entities and returns translated entities. + * + * @param array $entities + * An array of entities. + * @param string $langcode + * The langcode in which to translate, leave empty to use current langcode. + * @param bool $removeUntranslated + * Removes entities that are not translated. + * + * @return array + * An array of translated entities + */ + public function translateEntities(array $entities, string $langcode = NULL, bool $removeUntranslated = FALSE) : array { + // Get the langcode first so we don't have to fetch it in the loop. + if (!$langcode) { + $langcode = \Drupal::languageManager() + ->getCurrentLanguage() + ->getId(); + } + + foreach ($entities as $key => &$entity) { + if (!$entity instanceof EntityInterface) { + continue; + } + + $entity = $this->translateEntity($entity, $langcode, $removeUntranslated); + + // Unset an entity that is now a NULL. + // It will only be a NULL if translation is required. + // This is passed by the removeUntranslated variable. + if (!$entity) { + unset($entities[$key]); + } + } + + return $entities; + } + +} \ No newline at end of file diff --git a/src/Traits/ParagraphHelperTrait.php b/src/Traits/ParagraphHelperTrait.php new file mode 100644 index 0000000..12a0527 --- /dev/null +++ b/src/Traits/ParagraphHelperTrait.php @@ -0,0 +1,56 @@ +getParentEntity(); + + if ($parent instanceof $type) { + return $parent; + } + + if ($parent instanceof EntityInterface) { + return self::getParentOfType($parent, $type); + } + + return NULL; + } + +} \ No newline at end of file