From 6e7dfbc0b71fd53198ea088109ef01968906b894 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Tue, 3 Jun 2014 00:31:57 -0500
Subject: [PATCH] Issue #2247211 by blueminds, amateescu, xjm: Fixed
 Autocomplete widget doesn't work on entity labels without a " (entity_id)"
 suffix.

---
 .../src/Plugin/Type/Selection/SelectionInterface.php        | 5 +++--
 .../src/Plugin/entity_reference/selection/SelectionBase.php | 6 +++++-
 .../src/Tests/EntityReferenceIntegrationTest.php            | 6 ++++--
 3 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/core/modules/entity_reference/src/Plugin/Type/Selection/SelectionInterface.php b/core/modules/entity_reference/src/Plugin/Type/Selection/SelectionInterface.php
index 458c1cb18b20..27e1f214fc9f 100644
--- a/core/modules/entity_reference/src/Plugin/Type/Selection/SelectionInterface.php
+++ b/core/modules/entity_reference/src/Plugin/Type/Selection/SelectionInterface.php
@@ -24,8 +24,9 @@ interface SelectionInterface {
    * Returns a list of referenceable entities.
    *
    * @return array
-   *   An array of referenceable entities. Keys are entity IDs and
-   *   values are (safe HTML) labels to be displayed to the user.
+   *   A nested array of entities, the first level is keyed by the
+   *   entity bundle, which contains an array of entity labels (safe HTML),
+   *   keyed by the entity ID.
    */
   public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0);
 
diff --git a/core/modules/entity_reference/src/Plugin/entity_reference/selection/SelectionBase.php b/core/modules/entity_reference/src/Plugin/entity_reference/selection/SelectionBase.php
index 9d67882e5a5f..650486c69271 100644
--- a/core/modules/entity_reference/src/Plugin/entity_reference/selection/SelectionBase.php
+++ b/core/modules/entity_reference/src/Plugin/entity_reference/selection/SelectionBase.php
@@ -220,7 +220,11 @@ public function validateReferenceableEntities(array $ids) {
    * {@inheritdoc}
    */
   public function validateAutocompleteInput($input, &$element, &$form_state, $form, $strict = TRUE) {
-    $entities = $this->getReferenceableEntities($input, '=', 6);
+    $bundled_entities = $this->getReferenceableEntities($input, '=', 6);
+    $entities = array();
+    foreach ($bundled_entities as $entities_list) {
+      $entities += $entities_list;
+    }
     $params = array(
       '%value' => $input,
       '@value' => $input,
diff --git a/core/modules/entity_reference/src/Tests/EntityReferenceIntegrationTest.php b/core/modules/entity_reference/src/Tests/EntityReferenceIntegrationTest.php
index 3a892b2652f7..cd6a7b5cd195 100644
--- a/core/modules/entity_reference/src/Tests/EntityReferenceIntegrationTest.php
+++ b/core/modules/entity_reference/src/Tests/EntityReferenceIntegrationTest.php
@@ -79,7 +79,8 @@ public function testSupportedEntityTypesAndWidgets() {
         'name' => $entity_name,
         'user_id' => mt_rand(0, 128),
         $this->fieldName . '[0][target_id]' => $referenced_entities[0]->label() . ' (' . $referenced_entities[0]->id() . ')',
-        $this->fieldName . '[1][target_id]' => $referenced_entities[1]->label() . ' (' . $referenced_entities[1]->id() . ')',
+        // Test an input of the entity label without a ' (entity_id)' suffix.
+        $this->fieldName . '[1][target_id]' => $referenced_entities[1]->label(),
       );
       $this->drupalPostForm($this->entityType . '/add', $edit, t('Save'));
       $this->assertFieldValues($entity_name, $referenced_entities);
@@ -97,7 +98,8 @@ public function testSupportedEntityTypesAndWidgets() {
 
       $entity_name = $this->randomName();
       $target_id = $referenced_entities[0]->label() . ' (' . $referenced_entities[0]->id() . ')';
-      $target_id .= ', ' . $referenced_entities[1]->label() . ' (' . $referenced_entities[1]->id() . ')';
+      // Test an input of the entity label without a ' (entity_id)' suffix.
+      $target_id .= ', ' . $referenced_entities[1]->label();
       $edit = array(
         'name' => $entity_name,
         'user_id' => mt_rand(0, 128),
-- 
GitLab