From cd3611464ea1ba4c97ab0594c89d1045c902065f Mon Sep 17 00:00:00 2001
From: Markus Kalkbrenner <markus.kalkbrenner@bio.logis.de>
Date: Thu, 15 May 2014 16:38:44 +0200
Subject: [PATCH] Issue #1954302 by mkalkbrenner: Added Isolate some re-usable
 code from apachesolr_index_entity_to_documents().

---
 CHANGELOG.txt        |  1 +
 apachesolr.index.inc | 56 ++++++++++++++++++++++++++++++++++++++++++----------
 2 files changed, 47 insertions(+), 10 deletions(-)

diff --git a/CHANGELOG.txt b/CHANGELOG.txt
index d307856..5be1166 100644
--- a/CHANGELOG.txt
+++ b/CHANGELOG.txt
@@ -52,6 +52,7 @@ Apache Solr integration 6.x-3.x, xxxx-xx-xx
 #1909230 by wonder95: Fixed Bundle types ignored when marking content for reindexing.
 #1827320 by drumm: Fixed Upgrading from 6.x-3.x does not need apachesolr_update_7000-15.
 #1693092 by killua99, Nick_vh: Fixed function apachesolr_entity_update() and $status_callback()($id, $type) broken the node.
+#1954302 by mkalkbrenner: Added Isolate some re-usable code from apachesolr_index_entity_to_documents().
 
 Apache Solr integration 6.x-3.0-rc, 2012-10-22
 ------------------------------
diff --git a/apachesolr.index.inc b/apachesolr.index.inc
index d8a38f8..b9a8b2f 100644
--- a/apachesolr.index.inc
+++ b/apachesolr.index.inc
@@ -160,11 +160,12 @@ function apachesolr_index_status($env_id) {
  * displayed to underprivileged users in search results. The index user defaults
  * to a user ID of "0", which is the anonymous user.
  *
- * After the entity is loaded, it is converted to an array via the callback
- * specified in the entity type's info array. The array that the entity is
- * converted to is the model of the document sent to the Apache Solr server for
- * indexing. This function allows develoeprs to modify the document by
- * implementing the following hooks:
+ * After the entity is loaded, it will be handed over to
+ * apachesolr_convert_entity_to_documents() to be converted to an array via
+ * the callback specified in the entity type's info array. The array that the
+ * entity is converted to is the model of the document sent to the Apache Solr
+ * server for indexing. This function allows developers to modify the document
+ * by implementing the following hooks:
  * - apachesolr_index_document_build()
  * - apachesolr_index_document_build_ENTITY_TYPE()
  * - apachesolr_index_documents_alter()
@@ -220,6 +221,44 @@ function apachesolr_index_entity_to_documents($item, $env_id) {
     return FALSE;
   }
 
+  $documents = apachesolr_convert_entity_to_documents($entity, $entity_type, $env_id);
+
+  // Restore the user.
+  $user = $saved_user;
+  session_save_session(TRUE);
+
+  return $documents;
+}
+
+/**
+ * The given entity is converted to an array via the callback
+ * specified in the entity type's info array. The array that the entity is
+ * converted to is the model of the document sent to the Apache Solr server for
+ * indexing. This function allows developers to modify the document by
+ * implementing the following hooks:
+ * - apachesolr_index_document_build()
+ * - apachesolr_index_document_build_ENTITY_TYPE()
+ * - apachesolr_index_documents_alter()
+ *
+ * This function's code has been isolated from
+ * apachesolr_index_entity_to_documents() to a separate function to be re-used
+ * by apachesolr_multilingual_apachesolr_index_documents_alter().
+ *
+ * @param object $entity
+ *   The entity for which we want a document.
+ * @param string $entity_type
+ *   The type of entity we're processing.
+ * @param string $env_id
+ *   The machine name of the environment.
+ *
+ * @return array
+ *   An associative array of documents that are sent to the Apache Solr server
+ *   for indexing.
+ */
+function apachesolr_convert_entity_to_documents($entity, $entity_type, $env_id) {
+  // See _apachesolr_index_process_entity_get_document().
+  $bundle = $entity->type;
+
   // Create a new document, and do the bare minimum on it.
   $document = _apachesolr_index_process_entity_get_document($entity, $entity_type);
 
@@ -270,9 +309,6 @@ function apachesolr_index_entity_to_documents($item, $env_id) {
     $function($documents, $entity, $entity_type, $env_id);
   }
 
-  // Restore the user.
-  $user = $saved_user;
-  session_save_session(TRUE);
   return $documents;
 
 }
-- 
1.8.5.2 (Apple Git-48)

