From 75e2e2e0dab87e2025290b9074f53254ee539a66 Mon Sep 17 00:00:00 2001 From: Ilias Dimopoulos Date: Thu, 13 Oct 2022 14:06:18 +0300 Subject: [PATCH 1/3] Allow properties to be preserved after expanding the values. --- .../Fields/Drupal8/EntityReferenceHandler.php | 34 ++++++++++++------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/src/Drupal/Driver/Fields/Drupal8/EntityReferenceHandler.php b/src/Drupal/Driver/Fields/Drupal8/EntityReferenceHandler.php index 5ea3217..cd56ef7 100644 --- a/src/Drupal/Driver/Fields/Drupal8/EntityReferenceHandler.php +++ b/src/Drupal/Driver/Fields/Drupal8/EntityReferenceHandler.php @@ -11,12 +11,9 @@ class EntityReferenceHandler extends AbstractHandler { * {@inheritdoc} */ public function expand($values) { - $return = []; $entity_type_id = $this->fieldInfo->getSetting('target_type'); $entity_definition = \Drupal::entityTypeManager()->getDefinition($entity_type_id); - $id_key = $entity_definition->getKey('id'); - // Determine label field key. if ($entity_type_id !== 'user') { $label_key = $entity_definition->getKey('label'); @@ -36,24 +33,37 @@ public function expand($values) { $target_bundle_key = $entity_definition->getKey('bundle'); } - foreach ((array) $values as $value) { - $query = \Drupal::entityQuery($entity_type_id); - $or = $query->orConditionGroup(); - $or->condition($id_key, $value) - ->condition($label_key, $value); - $query->condition($or); + // The values can either be a direct label reference or a complex array + // containing multiple properties of the field. For example, the file field + // contains a target_id, a description and a display property. If the + // target_id exists as a property, we assume that the other properties are + // also present. Retrieve all labels and load the entities. + $labels = array_map(function ($value) { + return is_array($value) && isset($value['target_id']) ? $value['target_id'] : $value; + }, $values); + + foreach ((array) $labels as $index => $label) { + $query = \Drupal::entityQuery($entity_type_id)->condition($label_key, $label); $query->accessCheck(FALSE); if ($target_bundles && $target_bundle_key) { $query->condition($target_bundle_key, $target_bundles, 'IN'); } if ($entities = $query->execute()) { - $return[] = array_shift($entities); + $entity_id = array_shift($entities); + // Replace the entity IDs in the original array so that other properties + // are not lost. + if (is_array($values[$index]) && isset($values[$index]['target_id'])) { + $values[$index]['target_id'] = $entity_id; + } + else { + $values[$index] = $entity_id; + } } else { - throw new \Exception(sprintf("No entity '%s' of type '%s' exists.", $value, $entity_type_id)); + throw new \Exception(sprintf("No entity '%s' of type '%s' exists.", $label, $entity_type_id)); } } - return $return; + return $values; } /** From 8a91bf485ae713c6d4d710eebf3335654cd5017d Mon Sep 17 00:00:00 2001 From: Ilias Dimopoulos Date: Thu, 13 Oct 2022 14:40:27 +0300 Subject: [PATCH 2/3] Use the main property key instead of hardcoding 'target_id'. --- .../Driver/Fields/Drupal8/EntityReferenceHandler.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Drupal/Driver/Fields/Drupal8/EntityReferenceHandler.php b/src/Drupal/Driver/Fields/Drupal8/EntityReferenceHandler.php index cd56ef7..4518dbf 100644 --- a/src/Drupal/Driver/Fields/Drupal8/EntityReferenceHandler.php +++ b/src/Drupal/Driver/Fields/Drupal8/EntityReferenceHandler.php @@ -33,13 +33,15 @@ public function expand($values) { $target_bundle_key = $entity_definition->getKey('bundle'); } + // The values can either be a direct label reference or a complex array // containing multiple properties of the field. For example, the file field // contains a target_id, a description and a display property. If the // target_id exists as a property, we assume that the other properties are // also present. Retrieve all labels and load the entities. - $labels = array_map(function ($value) { - return is_array($value) && isset($value['target_id']) ? $value['target_id'] : $value; + $main_property = $this->fieldInfo->getMainPropertyName(); + $labels = array_map(function ($value) use ($main_property){ + return is_array($value) && isset($value[$main_property]) ? $value[$main_property] : $value; }, $values); foreach ((array) $labels as $index => $label) { @@ -52,8 +54,8 @@ public function expand($values) { $entity_id = array_shift($entities); // Replace the entity IDs in the original array so that other properties // are not lost. - if (is_array($values[$index]) && isset($values[$index]['target_id'])) { - $values[$index]['target_id'] = $entity_id; + if (is_array($values[$index]) && isset($values[$index][$main_property])) { + $values[$index][$main_property] = $entity_id; } else { $values[$index] = $entity_id; From efb23b7fc3693f93fd49a8b72a3e42cd66e12277 Mon Sep 17 00:00:00 2001 From: Ilias Dimopoulos Date: Thu, 13 Oct 2022 15:24:06 +0300 Subject: [PATCH 3/3] Fix codesniffs. --- src/Drupal/Driver/Fields/Drupal8/EntityReferenceHandler.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Drupal/Driver/Fields/Drupal8/EntityReferenceHandler.php b/src/Drupal/Driver/Fields/Drupal8/EntityReferenceHandler.php index 4518dbf..ef33ede 100644 --- a/src/Drupal/Driver/Fields/Drupal8/EntityReferenceHandler.php +++ b/src/Drupal/Driver/Fields/Drupal8/EntityReferenceHandler.php @@ -33,14 +33,13 @@ public function expand($values) { $target_bundle_key = $entity_definition->getKey('bundle'); } - // The values can either be a direct label reference or a complex array // containing multiple properties of the field. For example, the file field // contains a target_id, a description and a display property. If the // target_id exists as a property, we assume that the other properties are // also present. Retrieve all labels and load the entities. $main_property = $this->fieldInfo->getMainPropertyName(); - $labels = array_map(function ($value) use ($main_property){ + $labels = array_map(function ($value) use ($main_property) { return is_array($value) && isset($value[$main_property]) ? $value[$main_property] : $value; }, $values);