You are here

public function SearchApiAlterAddHierarchy::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_hierarchy.inc, line 92
Contains SearchApiAlterAddHierarchy.

Class

SearchApiAlterAddHierarchy
Adds all ancestors for hierarchical fields.

Code

public function alterItems(array &$items) {
  if (empty($this->options['fields'])) {
    return;
  }
  foreach ($items as $item) {
    $wrapper = $this->index
      ->entityWrapper($item, FALSE);
    $values = array();
    foreach ($this->options['fields'] as $field) {
      list($key, $prop) = explode(':', $field);
      if (!isset($wrapper->{$key})) {
        continue;
      }
      $child = $wrapper->{$key};
      $values += array(
        $key => array(),
      );
      $this
        ->extractHierarchy($child, $prop, $values[$key]);
    }
    foreach ($values as $key => $value) {
      $item->{$key} = array_values($value);
    }
  }
}