You are here

public function SearchApiRangesAlter::alterItems in Search API ranges 7

Index the min and max for the selected fields.

Overrides SearchApiAlterCallbackInterface::alterItems

File

includes/callback_search_api_ranges.inc, line 96

Class

SearchApiRangesAlter
Search API ranges data alteration callback that indexes the min and max of selected numeric fields.

Code

public function alterItems(array &$items) {
  if (!$items) {
    return;
  }
  if (!empty($this->options['fields'])) {
    foreach ($this->options['fields'] as $name => $field) {
      $required_fields[$name] = array(
        'type' => search_api_extract_inner_type($this->index->options['fields'][$name]['type']),
      );
    }
    foreach ($items as $item) {
      $wrapper = $this->index
        ->entityWrapper($item);
      $fields = search_api_extract_fields($wrapper, $required_fields);
      foreach ($fields as $name => $f) {
        $name = str_replace(':', '_', $name);
        if (!empty($f['value'])) {
          if (!is_array($f['value'])) {
            $f['value'] = array(
              $f['value'],
            );
          }
          $item->{$name . $this->min_suffix} = min($f['value']);
          $item->{$name . $this->max_suffix} = max($f['value']);
        }
      }
    }
  }
}