You are here

function search_api_facetapi_map_date in Search API 7

Constructs labels for date facet filter values.

Parameters

array $values: The date facet filter values, as used in URL parameters.

array $options: (optional) Options for creating the mapping. The following options are recognized:

  • format callback: A callback for creating a label for a timestamp. The function signature is like search_api_facetapi_format_timestamp(), receiving a timestamp and one of the FACETAPI_DATE_* constants as the parameters and returning a human-readable label.

Return value

array An array of labels for the given facet filters.

1 string reference to 'search_api_facetapi_map_date'
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 657
Integrates the Search API with the Facet API.

Code

function search_api_facetapi_map_date(array $values, array $options = array()) {
  $map = array();
  foreach ($values as $value) {

    // Ignore any filters passed directly from the server (range or missing). We
    // always create filters starting with a year.
    $value = "{$value}";
    if (!$value || !ctype_digit($value[0])) {
      continue;
    }

    // Get the granularity of the filter.
    $granularity = search_api_facetapi_date_get_granularity($value);
    if (!$granularity) {
      continue;
    }

    // Otherwise, parse the timestamp from the known format and format it as a
    // label.
    $format = search_api_facetapi_date_get_granularity_format($granularity);

    // Use the "!" modifier to make the date parsing independent of the current
    // date/time. (See #2678856.)
    $date = DateTime::createFromFormat('!' . $format, $value);
    if (!$date) {
      continue;
    }
    $format_callback = 'search_api_facetapi_format_timestamp';
    if (!empty($options['format callback']) && is_callable($options['format callback'])) {
      $format_callback = $options['format callback'];
    }
    $map[$value] = call_user_func($format_callback, $date
      ->format('U'), $granularity);
  }
  return $map;
}