You are here

apachesolr_entityreference_content.module in Apachesolr Entityreference Content 7

Functionality for including content from referenced entity field contents in Apache Solr index.

File

apachesolr_entityreference_content.module
View source
<?php

/**
 * @file
 * Functionality for including content from referenced entity field contents in
 * Apache Solr index.
 */

/**
 * Implements hook_apachesolr_field_mappings().
 */
function apachesolr_entityreference_content_apachesolr_field_mappings() {
  $mappings['entityreference'] = array(
    'indexing_callback' => 'apachesolr_entityreference_content_indexing_callback',
    'query types' => array(
      'term',
    ),
  );
  return $mappings;
}

/**
 * Indexing callback for entityreference fields.
 */
function apachesolr_entityreference_content_indexing_callback($entity, $field_name, $index_key, $field_info) {
  $fields = array();
  if (!empty($entity->{$field_name})) {
    $entity_type = $field_info['field']['settings']['target_type'];
    $referenced_entities = array();
    if (isset($entity->{$field_name}[$entity->language])) {
      $referenced_entities = $entity->{$field_name}[$entity->language];
    }
    elseif (isset($entity->{$field_name}[LANGUAGE_NONE])) {
      $referenced_entities = $entity->{$field_name}[LANGUAGE_NONE];
    }

    // Iterates over all references.
    foreach ($referenced_entities as $reference) {

      // Index the fields from the referenced entities.
      apachesolr_entityreference_content_add_entity_fields($fields, $entity_type, $reference['entity']);
    }
  }
  return $fields;
}

/**
 * Callback function to add content from a referenced entity to SOLR index.
 *
 * @param array &$fields
 *   Array of field to be indexed, passed by reference.
 * @param string $type
 *   The entity type.
 * @param object $entity
 *   Paragraph entity object.
 */
function apachesolr_entityreference_content_add_entity_fields(array &$fields, $type, $entity) {

  // Get information about the entity type.
  $info = entity_get_info($type);

  // Is the entity type fieldable?
  if ($info['fieldable']) {
    module_load_include('inc', 'apachesolr', 'apachesolr.index');

    // This function is modeled after field_apachesolr_index_document_build()
    // Need to include apachesolr.index.inc for field data.
    $indexed_fields = apachesolr_entity_fields($type);
    foreach ($indexed_fields as $index_key => $entity_fields) {
      foreach ($entity_fields as $field_info) {
        $field_name = $field_info['field']['field_name'];

        // See if the node has fields that can be indexed.
        if (isset($entity->{$field_name})) {

          // Got a field.
          $functions = $field_info['indexing_callback'];
          if (!is_array($functions)) {
            $functions = array(
              $functions,
            );
          }
          foreach ($functions as $function) {
            if ($function && function_exists($function)) {

              // NOTE: This function should always return an array.  One
              // entity field may be indexed to multiple Solr fields.
              $new_fields = $function($entity, $field_name, $index_key, $field_info);
              foreach ($new_fields as $new_field) {
                $fields[] = $new_field;
              }
            }
          }
        }
      }
    }
  }
}

Functions

Namesort descending Description
apachesolr_entityreference_content_add_entity_fields Callback function to add content from a referenced entity to SOLR index.
apachesolr_entityreference_content_apachesolr_field_mappings Implements hook_apachesolr_field_mappings().
apachesolr_entityreference_content_indexing_callback Indexing callback for entityreference fields.