You are here

public function SearchApiAlterAddViewedEntity::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_viewed_entity.inc, line 72
Contains SearchApiAlterAddViewedEntity.

Class

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

Code

public function alterItems(array &$items) {

  // Prevent session information from being saved while indexing.
  drupal_save_session(FALSE);

  // Language handling.
  $languages = language_list();
  $global_language = array(
    'language' => $GLOBALS['language'],
    'language_url' => $GLOBALS['language_url'],
    'language_content' => $GLOBALS['language_content'],
  );

  // Force the current user to anonymous to prevent access bypass in search
  // indexes.
  $original_user = $GLOBALS['user'];
  $GLOBALS['user'] = drupal_anonymous_user();
  $type = $this->index
    ->getEntityType();
  $mode = empty($this->options['mode']) ? 'full' : $this->options['mode'];
  foreach ($items as &$item) {

    // Since we can't really know what happens in entity_view() and render(),
    // we use try/catch. This will at least prevent some errors, even though
    // it's no protection against fatal errors and the like.
    try {

      // Check if the global language switch is enabled.
      if (!empty($this->options['global_language_switch'])) {

        // Language handling. We need to overwrite the global language
        // configuration because parts of entity rendering won't rely on the
        // passed in language (for instance, URL aliases).
        if (isset($languages[$item->search_api_language])) {
          $GLOBALS['language'] = $languages[$item->search_api_language];
          $GLOBALS['language_url'] = $languages[$item->search_api_language];
          $GLOBALS['language_content'] = $languages[$item->search_api_language];
        }
        else {
          $GLOBALS['language'] = $global_language['language'];
          $GLOBALS['language_url'] = $global_language['language_url'];
          $GLOBALS['language_content'] = $global_language['language_content'];
        }
      }
      $render = entity_view($type, array(
        entity_id($type, $item) => $item,
      ), $mode, $item->search_api_language);
      $text = render($render);
      if (!$text) {
        $item->search_api_viewed = NULL;
        continue;
      }
      $item->search_api_viewed = $text;
    } catch (Exception $e) {
      $item->search_api_viewed = NULL;
    }
  }

  // Restore global language settings.
  if (!empty($this->options['global_language_switch'])) {
    $GLOBALS['language'] = $global_language['language'];
    $GLOBALS['language_url'] = $global_language['language_url'];
    $GLOBALS['language_content'] = $global_language['language_content'];
  }

  // Restore the user.
  $GLOBALS['user'] = $original_user;
  drupal_save_session(TRUE);
}