You are here

public function SearchApiAlterAddCombined::alterItems in Search API Combined Fields 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

./callback_add_combined.inc, line 141

Class

SearchApiAlterAddCombined
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'])) {
    foreach ($items as $item) {
      $wrapper = $this->index
        ->entityWrapper($item);
      $vals = array();
      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'] = 'integer';
            }
          }
          $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);
          $multivalue = TRUE === isset($field['multivalue']) ? $field['multivalue'] : 1;
          if (!$multivalue) {
            $values = array_shift($values);
          }
          $item->{$name} = $values;
        }
      }
    }
  }
}