You are here

function search_api_search_api_query_alter in Search API 7

Implements hook_search_api_query_alter().

Adds node access to the query, if enabled.

Parameters

SearchApiQueryInterface $query: The SearchApiQueryInterface object representing the search query.

File

./search_api.module, line 2211
Provides a flexible framework for implementing search services.

Code

function search_api_search_api_query_alter(SearchApiQueryInterface $query) {
  global $user;
  $index = $query
    ->getIndex();

  // Only add node access if the necessary fields are indexed in the index, and
  // unless disabled explicitly by the query.
  $type = $index
    ->getEntityType();
  if (!empty($index->options['data_alter_callbacks']["search_api_alter_{$type}_access"]['status']) && !$query
    ->getOption('search_api_bypass_access')) {
    $account = $query
      ->getOption('search_api_access_account', $user);
    if (is_numeric($account)) {
      $account = user_load($account);
    }
    if (is_object($account)) {
      try {
        _search_api_query_add_node_access($account, $query, $type);
      } catch (SearchApiException $e) {
        watchdog_exception('search_api', $e);
      }
    }
    else {
      $account = $query
        ->getOption('search_api_access_account', '(' . t('none') . ')');
      if (is_object($account)) {
        $account = $account->uid;
      }
      if (!is_scalar($account)) {
        $account = var_export($account, TRUE);
      }
      watchdog('search_api', 'An illegal user UID was given for node access: @uid.', array(
        '@uid' => $account,
      ), WATCHDOG_WARNING);
    }
  }
}