You are here

function search_api_facetapi_facetapi_facet_info in Search API 7

Implements hook_facetapi_facet_info().

File

contrib/search_api_facetapi/search_api_facetapi.module, line 79
Integrates the Search API with the Facet API.

Code

function search_api_facetapi_facetapi_facet_info(array $searcher_info) {
  $facet_info = array();
  if ('search_api' == $searcher_info['adapter']) {
    $index = search_api_index_load($searcher_info['instance']);
    if (!empty($index->options['fields'])) {
      $wrapper = $index
        ->entityWrapper();
      $bundle_key = NULL;
      if ($index
        ->getEntityType() && ($entity_info = entity_get_info($index
        ->getEntityType())) && !empty($entity_info['bundle keys']['bundle'])) {
        $bundle_key = $entity_info['bundle keys']['bundle'];
      }

      // Some type-specific settings. Allowing to set some additional callbacks
      // (and other settings) in the map options allows for easier overriding by
      // other modules.
      $type_settings = array(
        'taxonomy_term' => array(
          'hierarchy callback' => 'search_api_facetapi_get_taxonomy_hierarchy',
        ),
        'date' => array(
          'query type' => 'date',
          'map options' => array(
            'map callback' => 'search_api_facetapi_map_date',
          ),
        ),
      );

      // Iterate through the indexed fields to set the facetapi settings for
      // each one.
      foreach ($index
        ->getFields() as $key => $field) {
        $field['key'] = $key;

        // Determine which, if any, of the field type-specific options will be
        // used for this field.
        if (isset($field['entity_type'])) {
          $type = $inner_type = $field['entity_type'];
        }
        else {
          $type = $field['type'];
          $inner_type = search_api_extract_inner_type($type);
        }
        $type_settings += array(
          $inner_type => array(),
        );
        $facet_info[$key] = $type_settings[$inner_type] + array(
          'label' => $field['name'],
          'description' => t('Filter by @type.', array(
            '@type' => $field['name'],
          )),
          'allowed operators' => array(
            FACETAPI_OPERATOR_AND => TRUE,
            FACETAPI_OPERATOR_OR => _search_api_facetapi_index_support_feature($index, 'search_api_facets_operator_or'),
          ),
          'dependency plugins' => array(
            'role',
          ),
          'facet missing allowed' => TRUE,
          'facet mincount allowed' => TRUE,
          'map callback' => 'search_api_facetapi_facet_map_callback',
          'map options' => array(),
          'field type' => $type,
        );
        if ($inner_type === 'date') {
          $facet_info[$key]['description'] .= ' ' . t('(Caution: This may perform very poorly for large result sets.)');
        }
        $facet_info[$key]['map options'] += array(
          'field' => $field,
          'index id' => $index->machine_name,
          'value callback' => '_search_api_facetapi_facet_create_label',
        );

        // Find out whether this property is a Field API field.
        if (strpos($key, ':') === FALSE) {
          if (isset($wrapper->{$key})) {
            $property_info = $wrapper->{$key}
              ->info();
            if (!empty($property_info['field'])) {
              $facet_info[$key]['field api name'] = $key;
            }
          }
        }

        // Add bundle information, if applicable.
        if ($bundle_key) {
          if ($key === $bundle_key) {

            // Set entity type this field contains bundle information for.
            $facet_info[$key]['field api bundles'][] = $index
              ->getEntityType();
          }
          else {

            // Add "bundle" as possible dependency plugin.
            $facet_info[$key]['dependency plugins'][] = 'bundle';
          }
        }
      }
    }
  }
  return $facet_info;
}