You are here

function apachesolr_convert_entity_to_documents in Apache Solr Search 6.3

Same name and namespace in other branches
  1. 8 apachesolr.index.inc \apachesolr_convert_entity_to_documents()
  2. 7 apachesolr.index.inc \apachesolr_convert_entity_to_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().

Parameters

object $entity: The entity for which we want a document.

string $entity_type: The type of entity we're processing.

string $env_id: The machine name of the environment.

Return value

array An associative array of documents that are sent to the Apache Solr server for indexing.

1 call to apachesolr_convert_entity_to_documents()
apachesolr_index_entity_to_documents in ./apachesolr.index.inc
Worker callback for apachesolr_index_entities().

File

./apachesolr.index.inc, line 246
Functions related to Apache Solr indexing operations.

Code

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);

  //Get the callback array to add stuff to the document
  $callbacks = apachesolr_entity_get_callback($entity_type, 'document callback', $bundle);
  $documents = array();
  foreach ($callbacks as $callback) {

    // Call a type-specific callback to add stuff to the document.
    $documents = array_merge($documents, $callback($document, $entity, $entity_type, $env_id));
  }

  //do this for all possible documents that were returned by the callbacks
  foreach ($documents as $document) {

    // Call an all-entity hook to add stuff to the document.
    module_invoke_all('apachesolr_index_document_build', $document, $entity, $entity_type, $env_id);

    // Call a type-specific hook to add stuff to the document.
    module_invoke_all('apachesolr_index_document_build_' . $entity_type, $document, $entity, $env_id);

    // Final processing to ensure that the document is properly structured.
    // All records must have a label field, which is used for user-friendly labeling.
    if (empty($document->label)) {
      $document->label = '';
    }

    // All records must have a "content" field, which is used for fulltext indexing.
    // If we don't have one, enter an empty value.  This does mean that the entity
    // will not be fulltext searchable.
    if (empty($document->content)) {
      $document->content = '';
    }

    // All records must have a "teaser" field, which is used for abbreviated
    // displays when no highlighted text is available.
    if (empty($document->teaser)) {
      $document->teaser = truncate_utf8($document->content, 300, TRUE);
    }
  }

  // Now allow modules to alter each other's additions for maximum flexibility.
  // Hook to allow modifications of the retrieved results
  foreach (module_implements('apachesolr_index_documents_alter') as $module) {
    $function = $module . '_apachesolr_index_documents_alter';
    $function($documents, $entity, $entity_type, $env_id);
  }
  return $documents;
}