You are here

function _search_api_facetapi_facet_create_label in Search API 7

Creates a human-readable label for single facet filter values.

Parameters

array $values: The values for which labels should be returned.

array $options: An associative array containing the following information about the facet:

  • 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 facet values to their labels.

2 string references to '_search_api_facetapi_facet_create_label'
SearchApiViewsFacetsBlockDisplay::render in contrib/search_api_views/includes/display_facet_block.inc
Render this display.
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 462
Integrates the Search API with the Facet API.

Code

function _search_api_facetapi_facet_create_label(array $values, array $options) {
  $field = $options['field'];
  $map = array();
  $n = count($values);

  // For entities, we can simply use the entity labels.
  if (isset($field['entity_type'])) {
    $type = $field['entity_type'];
    $entities = entity_load($type, $values);
    foreach ($entities as $id => $entity) {
      $label = entity_label($type, $entity);
      if ($label !== FALSE) {
        $map[$id] = $label;
      }
    }
    if (count($map) == $n) {
      return $map;
    }
  }

  // Then, we check whether there is an options list for the field.
  $index = search_api_index_load($options['index id']);
  $wrapper = $index
    ->entityWrapper();
  $values = drupal_map_assoc($values);
  foreach (explode(':', $field['key']) as $part) {
    if (!isset($wrapper->{$part})) {
      $wrapper = NULL;
      break;
    }
    $wrapper = $wrapper->{$part};
    while (($info = $wrapper
      ->info()) && search_api_is_list_type($info['type'])) {
      $wrapper = $wrapper[0];
    }
  }
  if ($wrapper && ($options_list = $wrapper
    ->optionsList('view'))) {

    // We have no use for empty strings, as then the facet links would be
    // invisible.
    $map += array_intersect_key(array_filter($options_list, 'strlen'), $values);
    if (count($map) == $n) {
      return $map;
    }
  }

  // As a "last resort" we try to create a label based on the field type, for
  // all values that haven't got a mapping yet.
  foreach (array_diff_key($values, $map) as $value) {
    switch ($field['type']) {
      case 'boolean':
        $map[$value] = $value ? t('true') : t('false');
        break;
      case 'date':
        $v = is_numeric($value) ? $value : strtotime($value);
        $map[$value] = format_date($v, 'short');
        break;
      case 'duration':
        $map[$value] = format_interval($value);
        break;
    }
  }
  return $map;
}