You are here

public function SearchApiAlterAddAggregation::alterItems in Search API 7

Alter items before indexing.

Items which are removed from the array won't be indexed, but will be marked as clean for future indexing. This could for instance be used to implement some sort of access filter for security purposes (e.g., don't index unpublished nodes or comments).

Parameters

array $items: An array of items to be altered, keyed by item IDs.

Overrides SearchApiAlterCallbackInterface::alterItems

File

includes/callback_add_aggregation.inc, line 180
Contains SearchApiAlterAddAggregation.

Class

SearchApiAlterAddAggregation
Search API data alteration callback that adds an URL field for all items.

Code

public function alterItems(array &$items) {
  if (!$items) {
    return;
  }
  if (isset($this->options['fields'])) {
    $types = $this
      ->getTypes('type');
    foreach ($items as $item) {
      $wrapper = $this->index
        ->entityWrapper($item);
      foreach ($this->options['fields'] as $name => $field) {
        if ($field['name']) {
          $required_fields = array();
          foreach ($field['fields'] as $f) {
            if (!isset($required_fields[$f])) {
              $required_fields[$f]['type'] = $types[$field['type']];
            }
          }
          $fields = search_api_extract_fields($wrapper, $required_fields);
          $values = array();
          foreach ($fields as $f) {
            if (isset($f['value'])) {
              $values[] = $f['value'];
            }
          }
          $values = $this
            ->flattenArray($values);
          $this->reductionType = $field['type'];
          $this->fulltextReductionSeparator = isset($field['separator']) ? $field['separator'] : "\n\n";
          $item->{$name} = array_reduce($values, array(
            $this,
            'reduce',
          ), NULL);
          if ($field['type'] == 'count' && !$item->{$name}) {
            $item->{$name} = 0;
          }
        }
      }
    }
  }
}