From 2410341f67d754829d3ba06a7f5c911d67396918 Mon Sep 17 00:00:00 2001 From: Aman Pilgrim Date: Fri, 8 Nov 2024 10:58:16 +1300 Subject: [PATCH 1/3] Restrict reference by ID to numeric values on integer fields #241 introduced regression when using entity labels with a postgres database SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for type bigint --- .../Fields/Drupal8/EntityReferenceHandler.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Drupal/Driver/Fields/Drupal8/EntityReferenceHandler.php b/src/Drupal/Driver/Fields/Drupal8/EntityReferenceHandler.php index 5ea3217..36b7d70 100644 --- a/src/Drupal/Driver/Fields/Drupal8/EntityReferenceHandler.php +++ b/src/Drupal/Driver/Fields/Drupal8/EntityReferenceHandler.php @@ -36,12 +36,18 @@ public function expand($values) { $target_bundle_key = $entity_definition->getKey('bundle'); } + // Determine the id key type (can be an integer or string). + $id_definition = \Drupal::service('entity_field.manager')->getBaseFieldDefinitions($entity_type_id)[$id_key]; + $id_type = $id_definition->getType(); + 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); + // Provide for the use of numeric entity ids. + if ($id_type === 'integer' && is_numeric($value)) { + $query->condition($id_key, $value); + } else { + $query->condition($label_key, $value); + } $query->accessCheck(FALSE); if ($target_bundles && $target_bundle_key) { $query->condition($target_bundle_key, $target_bundles, 'IN'); From 55e54bb435e0c83f423841e3e2e0dc22da1f3f82 Mon Sep 17 00:00:00 2001 From: Vojislav Jovanovic Date: Wed, 25 Dec 2024 14:40:17 +0100 Subject: [PATCH 2/3] Search both by label and by ID when the value is integer --- .../Driver/Fields/Drupal8/EntityReferenceHandler.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Drupal/Driver/Fields/Drupal8/EntityReferenceHandler.php b/src/Drupal/Driver/Fields/Drupal8/EntityReferenceHandler.php index 36b7d70..6d52fcb 100644 --- a/src/Drupal/Driver/Fields/Drupal8/EntityReferenceHandler.php +++ b/src/Drupal/Driver/Fields/Drupal8/EntityReferenceHandler.php @@ -43,9 +43,14 @@ public function expand($values) { foreach ((array) $values as $value) { $query = \Drupal::entityQuery($entity_type_id); // Provide for the use of numeric entity ids. - if ($id_type === 'integer' && is_numeric($value)) { - $query->condition($id_key, $value); - } else { + if ($id_type === 'integer' && ctype_digit($value)) { + $or = $query->orConditionGroup(); + $or + ->condition($id_key, $value) + ->condition($label_key, $value); + $query->condition($or); + } + else { $query->condition($label_key, $value); } $query->accessCheck(FALSE); From ed71966738bbbafa9206ca8003ae7d31d6bb1515 Mon Sep 17 00:00:00 2001 From: Vojislav Jovanovic Date: Wed, 25 Dec 2024 14:41:57 +0100 Subject: [PATCH 3/3] Remove redundant IF block --- src/Drupal/Driver/Fields/Drupal8/EntityReferenceHandler.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Drupal/Driver/Fields/Drupal8/EntityReferenceHandler.php b/src/Drupal/Driver/Fields/Drupal8/EntityReferenceHandler.php index 6d52fcb..913a400 100644 --- a/src/Drupal/Driver/Fields/Drupal8/EntityReferenceHandler.php +++ b/src/Drupal/Driver/Fields/Drupal8/EntityReferenceHandler.php @@ -26,10 +26,6 @@ public function expand($values) { $label_key = 'name'; } - if (!$label_key && $entity_type_id == 'user') { - $label_key = 'name'; - } - // Determine target bundle restrictions. $target_bundle_key = NULL; if ($target_bundles = $this->getTargetBundles()) {