You are here

function search_api_facetapi_facet_map_callback in Search API 7

Map callback for all search_api facet fields.

Parameters

array $values: The values to map.

array $options: An associative array containing:

  • field: Field information, as stored in the index, but with an additional "key" property set to the field's internal name.
  • index id: The machine name of the index for this facet.
  • map callback: (optional) A callback that will be called at the beginning, which allows initial mapping of filters. Only values not mapped by that callback will be processed by this method.
  • value callback: A callback used to map single values and the limits of ranges. The signature is the same as for this function, but all values will be single values.
  • missing label: (optional) The label used for the "missing" facet.

Return value

array An array mapping raw filter values to their labels.

1 call to search_api_facetapi_facet_map_callback()
SearchApiViewsFacetsBlockDisplay::render in contrib/search_api_views/includes/display_facet_block.inc
Render this display.
1 string reference to 'search_api_facetapi_facet_map_callback'
search_api_facetapi_facetapi_facet_info in contrib/search_api_facetapi/search_api_facetapi.module
Implements hook_facetapi_facet_info().

File

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

Code

function search_api_facetapi_facet_map_callback(array $values, array $options = array()) {
  $map = array();

  // See if we have an additional map callback.
  if (isset($options['map callback']) && is_callable($options['map callback'])) {
    $map = call_user_func($options['map callback'], $values, $options);
  }

  // Then look at all unmapped values and save information for them.
  $mappable_values = array();
  $ranges = array();
  foreach ($values as $value) {
    $value = (string) $value;
    if (isset($map[$value])) {
      continue;
    }
    if ($value == '!') {

      // The "missing" filter is usually always the same, but we allow an easy
      // override via the "missing label" map option.
      $map['!'] = isset($options['missing label']) ? $options['missing label'] : '(' . t('none') . ')';
      continue;
    }
    $length = strlen($value);
    if ($length > 5 && $value[0] == '[' && $value[$length - 1] == ']' && ($pos = strpos($value, ' TO '))) {

      // This is a range filter.
      $lower = trim(substr($value, 1, $pos));
      $upper = trim(substr($value, $pos + 4, -1));
      if ($lower != '*') {
        $mappable_values[$lower] = TRUE;
      }
      if ($upper != '*') {
        $mappable_values[$upper] = TRUE;
      }
      $ranges[$value] = array(
        'lower' => $lower,
        'upper' => $upper,
      );
    }
    else {

      // A normal, single-value filter.
      $mappable_values[$value] = TRUE;
    }
  }
  if ($mappable_values) {
    $map += call_user_func($options['value callback'], array_keys($mappable_values), $options);
  }
  foreach ($ranges as $value => $range) {
    $lower = isset($map[$range['lower']]) ? $map[$range['lower']] : $range['lower'];
    $upper = isset($map[$range['upper']]) ? $map[$range['upper']] : $range['upper'];
    if ($lower == '*' && $upper == '*') {
      $map[$value] = t('any');
    }
    elseif ($lower == '*') {
      $map[$value] = "≤ {$upper}";
    }
    elseif ($upper == '*') {
      $map[$value] = "≥ {$lower}";
    }
    else {
      $map[$value] = "{$lower} – {$upper}";
    }
  }
  return $map;
}