You are here

public function SearchApiAlterLanguageControl::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_language_control.inc, line 102
Contains SearchApiAlterLanguageControl.

Class

SearchApiAlterLanguageControl
Search API data alteration callback that filters out items based on their bundle.

Code

public function alterItems(array &$items) {
  foreach ($items as $i => &$item) {

    // Set item language, if a custom field was selected.
    if ($field = $this->options['lang_field']) {
      $wrapper = $this->index
        ->entityWrapper($item);
      if (isset($wrapper->{$field})) {
        try {
          $item->search_api_language = $wrapper->{$field}
            ->value();
        } catch (EntityMetadataWrapperException $e) {

          // Something went wrong while accessing the language field. Probably
          // doesn't really matter.
        }
      }
    }

    // Filter out items according to language, if any were selected.
    if ($languages = $this->options['languages']) {
      if (empty($languages[$item->search_api_language])) {
        unset($items[$i]);
      }
    }
  }
}