You are here

function search_exclude_nid_query_alter in Search exclude nid 8

Same name and namespace in other branches
  1. 7 search_exclude_nid.module \search_exclude_nid_query_alter()

File

./search_exclude_nid.module, line 32

Code

function search_exclude_nid_query_alter(Drupal\Core\Database\Query\AlterableInterface $query) {
  if ($query
    ->hasTag('node_access') && $query
    ->hasTag('pager')) {
    $tables = $query
      ->getTables();
    foreach ($tables as $table) {
      if ($table['table'] == 'search_index') {

        // Filter the excluded nids to make sure we dont break the query.
        $config = \Drupal::config('search_exclude_nid.settings');
        $excluded_nids = $config
          ->get('excluded_nids');
        if (count($excluded_nids)) {

          // Remove the node IDs from the query.
          $query
            ->condition('n.nid', $excluded_nids, 'NOT IN');
        }
      }
    }
  }
}