You are here

function sarnia_entity_load in Sarnia 7

Implements hook_entity_load().

Add Solr properties to Search API Index entities' fields if they're managed by Sarnia.

SearchApiIndex::getFields() returns only fields that are in both $index->options['fields'] and the EntityAPI properties. This means that we have to inject the Solr properties both in hook_entity_load() and in hook_entity_property_info().

See also

SearchApiIndex::getFields(), sarnia_entity_property_info()

http://drupal.org/node/1308638

http://drupalcode.org/project/search_api.git/commit/b5ed6b

http://drupalcode.org/project/search_api.git/commit/b53fec

File

./sarnia.module, line 830

Code

function sarnia_entity_load($entities, $type) {
  if ($type == 'search_api_index') {
    foreach ($entities as $entity) {

      // If this entity is associated with a sarnia type, override the index 'fields'.
      if ($entity
        ->server() && ($sarnia_type = sarnia_entity_type_load_by_index($entity->machine_name))) {
        $prev_fields = array();
        if (isset($entity->options['fields'])) {
          $prev_fields = $entity->options['fields'];
        }
        $entity->options['fields'] = array();
        foreach ($entity
          ->server()
          ->getRemoteFields() as $key => $field) {

          // Set a default boost factor.
          $boost = '1.0';
          if (!empty($prev_fields[$key]) && !empty($prev_fields[$key]['boost'])) {

            // Preserve the boost factor if the user set it in the UI.
            $boost = $prev_fields[$key]['boost'];
          }
          $entity->options['fields'][$key] = array(
            'name' => $key,
            'indexed' => $field
              ->isIndexed(),
            'type' => 'none',
            'boost' => $boost,
          );
        }
        foreach ($entity
          ->server()
          ->getFulltextFields() as $key => $field) {
          $entity->options['fields'][$key]['type'] = 'text';
        }

        // Set the type of date fields.
        foreach ($entity
          ->server()
          ->getRemoteFields() as $key => $field) {
          if ($field
            ->getType() == 'tdate') {
            $entity->options['fields'][$key]['type'] = 'date';
          }
        }
        ksort($entity->options['fields']);
      }
    }
  }
}