From 7510476ce3038d5e69a1ff14fd235b8bcdb34286 Mon Sep 17 00:00:00 2001
From: Rasmus Werling <rasmus.werling@gmail.com>
Date: Tue, 17 May 2016 14:59:39 +0300
Subject: [PATCH] Issue #2660790 by Rade: Fixed bug where autocomplete field
 doesn't show all terms with the same name, updated test case.

---
 term_merge.module    | 25 ++++++++++++++++++-------
 term_merge.pages.inc | 14 +++++++++-----
 term_merge.test      |  5 +++--
 3 files changed, 30 insertions(+), 14 deletions(-)

diff --git a/term_merge.module b/term_merge.module
index 39c647d..21fffc7 100644
--- a/term_merge.module
+++ b/term_merge.module
@@ -962,19 +962,30 @@ function term_merge_form_term_trunk_widget_autocomplete(&$form, &$form_state, $v
  * is expected from a term trunk widget to provide in its value.
  */
 function term_merge_form_trunk_term_widget_autocomplete_validate($element, &$form_state, $form) {
-  $term = taxonomy_get_term_by_name($element['#value'], $form['#vocabulary']->machine_name);
-  if (!is_array($term) || empty($term)) {
-    // Seems like the user has entered a non existing name in the autocomplete
+  // Field value is "name (tid)", match the tid from parenthesis.
+  if (preg_match("/.+\((\d+)\)/", $element['#value'], $matches)) {
+    $tid = $matches[1];
+  }
+  else {
+    // Assume that the user didn't use the autocomplete but filled in a tid
+    // manually.
+    $tid = $element['#value'];
+  }
+
+  // Try loading the taxonomy term.
+  $term = taxonomy_term_load($tid);
+
+  if (empty($term)) {
+    // Seems like the user has entered a non existing name or tid in the autocomplete
     // textfield.
-    form_error($element, t('There are no terms with name %name in the %vocabulary vocabulary.', array(
-      '%name' => $element['#value'],
+    form_error($element, t('There are no terms matching %value in the %vocabulary vocabulary.', array(
+      '%value' => $element['#value'],
       '%vocabulary' => $form['#vocabulary']->name,
     )));
   }
   else {
-    // We have to substitute the term's name with its tid in order to make this
+    // We have to substitute the field value the term tid in order to make this
     // widget consistent with the interface.
-    $term = array_pop($term);
     form_set_value($element, $term->tid, $form_state);
   }
 }
diff --git a/term_merge.pages.inc b/term_merge.pages.inc
index 7b86c34..94b0d3b 100644
--- a/term_merge.pages.inc
+++ b/term_merge.pages.inc
@@ -263,12 +263,16 @@ function term_merge_form_term_trunk_widget_autocomplete_autocomplete($vocabulary
 
   $term_matches = array();
   foreach ($tags_return as $tid => $name) {
-    $n = $name;
-    // Term names containing commas or quotes must be wrapped in quotes.
-    if (strpos($name, ',') !== FALSE || strpos($name, '"') !== FALSE) {
-      $n = '"' . str_replace('"', '""', $name) . '"';
+    // Add both term name and tid to array key in order to allow multiple terms
+    // with same name to be displayed.
+    $key = "$name ($tid)";
+    // Strip things like starting/trailing white spaces, line breaks and tags.
+    $key = preg_replace('/\s\s+/', ' ', str_replace("\n", '', trim(decode_entities(strip_tags($key)))));
+    // Names containing commas or quotes must be wrapped in quotes.
+    if (strpos($key, ',') !== FALSE || strpos($key, '"') !== FALSE) {
+      $key = '"' . str_replace('"', '""', $key) . '"';
     }
-    $term_matches[$n] = check_plain($name . ' [tid: ' . $tid . ']');
+    $term_matches[$key] = check_plain($name . ' [tid: ' . $tid . ']');
   }
 
   drupal_json_output($term_matches);
diff --git a/term_merge.test b/term_merge.test
index 57aacd8..01d89b6 100644
--- a/term_merge.test
+++ b/term_merge.test
@@ -627,7 +627,8 @@ class TermMergeTermMergeWebTestCase extends TermMergeWebTestCase {
           // with valid suggestions.
           $response = $this->drupalGet('term-merge/autocomplete/term-trunk/' . $this->vocabulary->machine_name . '/' . drupal_strtoupper($terms['term_trunk']->name));
           $response = drupal_json_decode($response);
-          $this->assertTrue(isset($response[$terms['term_trunk']->name]), 'Autocomplete menu path replies with valid suggestions for term trunk autocomplete widget.');
+          $autocomplete_key = $terms['term_trunk']->name . ' (' . $terms['term_trunk']->tid . ')';
+          $this->assertTrue(isset($response[$autocomplete_key]), 'Autocomplete menu path replies with valid suggestions for term trunk autocomplete widget.');
 
           // Making sure for the term trunk autocomplete widget doesn't allow to
           // submit any of the selected term branches nor their children.
@@ -704,7 +705,7 @@ class TermMergeTermMergeWebTestCase extends TermMergeWebTestCase {
           break;
 
         case 'autocomplete':
-          $term_trunk_edit += array('term_trunk[tid]' => $terms['term_trunk']->name);
+          $term_trunk_edit += array('term_trunk[tid]' => $terms['term_trunk']->name . ' (' . $terms['term_trunk']->tid . ')');
           break;
       }
 
-- 
1.9.1

