From 26d07623b75d6db15b58df13b8f3097f33b96396 Mon Sep 17 00:00:00 2001
From: Pierre Buyle <buyle@pheromone.ca>
Date: Tue, 2 Sep 2014 15:18:41 -0400
Subject: [PATCH] Issue #2280441: Find translated entities (with title module).

---
 plugins/linkit_search/entity.class.php | 93 ++++++++++++++++++++++++----------
 1 file changed, 65 insertions(+), 28 deletions(-)

diff --git a/plugins/linkit_search/entity.class.php b/plugins/linkit_search/entity.class.php
index 6044f97..d2f0073 100644
--- a/plugins/linkit_search/entity.class.php
+++ b/plugins/linkit_search/entity.class.php
@@ -193,58 +193,87 @@ class LinkitSearchPluginEntity extends LinkitSearchPlugin {
   function getQueryInstance() {
     $this->query = new EntityFieldQuery();
     $this->query->entityCondition('entity_type', $this->plugin['entity_type']);
-
-    // Add the default sort on the enity label.
-    $this->query->propertyOrderBy($this->entity_field_label, 'ASC');
   }
 
   /**
    * Implements LinkitSearchPluginInterface::fetchResults().
    */
   public function fetchResults($search_string) {
+    global $language;
     // If the $search_string is not a string, something is wrong and an empty
     // array is returned.
     $matches = array();
 
-    // Get the EntityFieldQuery instance.
-    $this->getQueryInstance();
+    if (isset($this->entity_key_bundle) && isset($this->conf['bundles'])) {
+      $bundles_with_property = $this->conf['bundles'];
+    }
+    else {
+      $bundles_with_property = array_keys($this->entity_info['bundles']);
+    }
+    $bundles_with_field = array();
+    foreach ($bundles_with_property as $key => $bundle) {
+      if (field_info_instance($this->plugin['entity_type'], 'title_field', $bundle)){
+        $bundles_with_field[] = $bundle;
+        unset($bundles_with_property[$key]);
+      }
+    }
 
-    // Add the search condition to the query object.
-    $this->query->propertyCondition($this->entity_field_label,
-            '%' . db_like($search_string) . '%', 'LIKE')
+    $ids = array();
+    // Search for entities using the values of the label property.
+    if ($bundles_with_property) {
+      // Get the EntityFieldQuery instance.
+      $this->getQueryInstance();
+      // Add the default sort on the entity label.
+      $this->query->propertyOrderBy($this->entity_field_label, 'ASC');
+      // Add the search condition to the query object.
+      $this->query->propertyCondition($this->entity_field_label,
+        '%' . db_like($search_string) . '%', 'LIKE')
         ->addTag('linkit_entity_autocomplete')
         ->addTag('linkit_' . $this->plugin['entity_type'] . '_autocomplete');
 
-    // Add access tag for the query.
-    // There is also a runtime access check that uses entity_access().
-    $this->query->addTag($this->plugin['entity_type'] . '_access');
-
-    // Bundle check.
-    if (isset($this->entity_key_bundle) && isset($this->conf['bundles']) ) {
-      $bundles = array_filter($this->conf['bundles']);
-      if ($bundles) {
-        $this->query->propertyCondition($this->entity_key_bundle, $bundles, 'IN');
+      // Add access tag for the query.
+      // There is also a runtime access check that uses entity_access().
+      $this->query->addTag($this->plugin['entity_type'] . '_access');
+      // Bundle check.
+      $this->query->propertyCondition($this->entity_key_bundle, $bundles_with_property, 'IN');
+      // Execute the query.
+      $result = $this->query->execute();
+      if (isset($result[$this->plugin['entity_type']])) {
+        $ids += array_keys($result[$this->plugin['entity_type']]);
       }
     }
-
-    // Execute the query.
-    $result = $this->query->execute();
-
-    if (!isset($result[$this->plugin['entity_type']])) {
+    // Search for entities using the values of the title_field field.
+    if ($bundles_with_field) {
+      // Get the EntityFieldQuery instance.
+      $this->getQueryInstance();
+      // Add the default sort on the title_field field.
+      $this->query->fieldOrderBy('title_field', 'value', 'ASC');
+      // Add the search condition to the query object.
+      $this->query->fieldLanguageCondition('title_field', $language->language, '=', NULL, 1)
+        ->fieldCondition('title_field', 'value','%' . db_like($search_string) . '%', 'LIKE', NULL, 1)
+        ->addTag('linkit_entity_autocomplete')
+        ->addTag('linkit_' . $this->plugin['entity_type'] . '_autocomplete');
+      // Add access tag for the query.
+      // There is also a runtime access check that uses entity_access().
+      $this->query->addTag($this->plugin['entity_type'] . '_access');
+      // Bundle check.
+      $this->query->propertyCondition($this->entity_key_bundle, $bundles_with_field, 'IN');
+      // Execute the query.
+      $result = $this->query->execute();
+      if (isset($result[$this->plugin['entity_type']])) {
+        $ids += array_keys($result[$this->plugin['entity_type']]);
+      }
+    }
+    if (empty($ids)) {
       return array();
     }
-
-    $ids = array_keys($result[$this->plugin['entity_type']]);
-
     // Load all the entities with all the ids we got.
     $entities = entity_load($this->plugin['entity_type'], $ids);
-
     foreach ($entities AS $entity) {
       // Check the access againt the definded entity access callback.
       if (entity_access('view', $this->plugin['entity_type'], $entity) === FALSE) {
         continue;
       }
-
       $matches[] = array(
         'title' => $this->createLabel($entity),
         'description' => $this->createDescription($entity),
@@ -252,7 +281,15 @@ class LinkitSearchPluginEntity extends LinkitSearchPlugin {
         'group' => $this->createGroup($entity),
         'addClass' => $this->createRowClass($entity),
       );
-
+    }
+    // If two queries were executed, sort the matches again.
+    if ($bundles_with_property && $bundles_with_field) {
+      usort($matches, function($a, $b) {
+        if ($a['title'] == $b['title']) {
+          return 0;
+        }
+        return ($a['title'] < $b['title']) ? -1 : 1;
+      });
     }
     return $matches;
   }
-- 
1.9.1

