You are here

function apachesolr_entity_fields in Apache Solr Search 8

Same name and namespace in other branches
  1. 6.3 apachesolr.module \apachesolr_entity_fields()
  2. 7 apachesolr.module \apachesolr_entity_fields()

Returns array containing information about node fields that should be indexed

3 calls to apachesolr_entity_fields()
apachesolr_entity_field_facets in ./apachesolr.module
Returns an array of facets for the provided entity type's fields.
apachesolr_field_name_map in ./apachesolr.module
Try to map a schema field name to a human-readable description.
field_apachesolr_index_document_build in ./apachesolr.module
Implements hook_apachesolr_index_document_build().

File

./apachesolr.module, line 2055
Integration with the Apache Solr search application.

Code

function apachesolr_entity_fields($entity_type = 'node') {
  $fields =& drupal_static(__FUNCTION__, array());
  if (!isset($fields[$entity_type])) {
    $fields[$entity_type] = array();
    $mappings = module_invoke_all('apachesolr_field_mappings');
    foreach (array_keys($mappings) as $key) {

      // Set all values with defaults.
      $defaults = array(
        'dependency plugins' => array(
          'bundle',
          'role',
        ),
        'map callback' => FALSE,
        'name callback' => '',
        'hierarchy callback' => FALSE,
        'indexing_callback' => '',
        'index_type' => 'string',
        'facets' => FALSE,
        'facet missing allowed' => FALSE,
        'facet mincount allowed' => FALSE,
        // Field API allows any field to be multi-valued.
        'multiple' => TRUE,
      );
      if ($key !== 'per-field') {
        $mappings[$key] += $defaults;
      }
      else {
        foreach (array_keys($mappings[$key]) as $field_key) {
          $mappings[$key][$field_key] += $defaults;
        }
      }
    }

    // Allow other modules to add or alter mappings.
    drupal_alter('apachesolr_field_mappings', $mappings, $entity_type);
    $modules = system_get_info('module');
    $instances = field_info_instances($entity_type);
    foreach (field_info_fields() as $field_name => $field) {
      $row = array();
      if (isset($field['bundles'][$entity_type]) && (isset($mappings['per-field'][$field_name]) || isset($mappings[$field['type']]))) {

        // Find the mapping.
        if (isset($mappings['per-field'][$field_name])) {
          $row = $mappings['per-field'][$field_name];
        }
        else {
          $row = $mappings[$field['type']];
        }

        // The field info array.
        $row['field'] = $field;

        // Cardinality: The number of values the field can hold. Legal values
        // are any positive integer or FIELD_CARDINALITY_UNLIMITED.
        if ($row['field']['cardinality'] != 1) {
          $row['multiple'] = TRUE;
        }

        // @todo: for fields like taxonomy we are indexing multiple Solr fields
        // per entity field, but are keying on a single Solr field name here.
        $function = !empty($row['name callback']) ? $row['name callback'] : NULL;
        if ($function && is_callable($function)) {
          $row['name'] = $function($field);
        }
        else {
          $row['name'] = $field['field_name'];
        }
        $row['module_name'] = $modules[$field['module']]['name'];

        // Set display name
        $display_name = array();
        foreach ($field['bundles'][$entity_type] as $bundle) {
          $field_display = isset($instances[$bundle][$field_name]['display']) ? $instances[$bundle][$field_name]['display'] : array();
          if (empty($field_display['search_index']) || isset($field_display['search_index']['type']) && $field_display['search_index']['type'] != 'hidden') {
            $row['display_name'] = $instances[$bundle][$field_name]['label'];
            $row['bundles'][] = $bundle;
          }
        }

        // Only add to the $fields array if some instances are displayed for the search index.
        if (!empty($row['bundles'])) {

          // Use the Solr index key as the array key.
          $fields[$entity_type][apachesolr_index_key($row)][] = $row;
        }
      }
    }
  }
  return $fields[$entity_type];
}