You are here

search_api_combined.module in Search API Combined Fields 7

File

search_api_combined.module
View source
<?php

/**
 * Implements hook_search_api_alter_callback_info()
 * Add our own callback for Search API
 */
function search_api_combined_search_api_alter_callback_info() {
  $callbacks['search_api_alter_add_combined'] = array(
    'name' => t('Combined fields'),
    'description' => t('Gives you the ability to define additional fields, combining data from one or more other fields.'),
    'class' => 'SearchApiAlterAddCombined',
  );
  return $callbacks;
}

/**
 * Implements hook_facetapi_facet_info_alter to chance the field and type of the combined facets to
 * match the selected imitation
 *
 * @param array $facet_info
 * @param array $searcher_info
 */
function search_api_combined_facetapi_facet_info_alter(array &$facet_info, array $searcher_info) {
  $processor_info = search_api_get_processors();
  $index = search_api_index_load($searcher_info['instance']);
  $options = empty($index->options) ? array() : $index->options;
  $callbacks = empty($options['data_alter_callbacks']) ? array() : $options['data_alter_callbacks'];
  if (TRUE === isset($callbacks['search_api_alter_add_combined']['settings']['fields']) && 1 == $callbacks['search_api_alter_add_combined']['status']) {
    foreach ($callbacks['search_api_alter_add_combined']['settings']['fields'] as $key => $combined) {
      if (FALSE === empty($combined['imitate']) && TRUE === isset($facet_info[$combined['imitate']])) {
        $facet_info[$key]['field type'] = $facet_info[$combined['imitate']]['field type'];
        $facet_info[$key]['map options']['field'] = array_merge($facet_info[$key]['map options']['field'], $facet_info[$combined['imitate']]['map options']['field']);
      }

      // Carry over callback functions from facets being imitated
      foreach (array(
        'values callback',
        'hierarchy callback',
        'map callback',
        'min callback',
        'max callback',
      ) as $callback) {
        if (isset($facet_info[$combined['imitate']]) && $facet_info[$key][$callback] != $facet_info[$combined['imitate']][$callback]) {
          $facet_info[$key][$callback] = $facet_info[$combined['imitate']][$callback];
        }
      }
    }
  }
}

Functions

Namesort descending Description
search_api_combined_facetapi_facet_info_alter Implements hook_facetapi_facet_info_alter to chance the field and type of the combined facets to match the selected imitation
search_api_combined_search_api_alter_callback_info Implements hook_search_api_alter_callback_info() Add our own callback for Search API