You are here

search_exclude_nid.module in Search exclude nid 7

Same filename and directory in other branches
  1. 8 search_exclude_nid.module

Excludes single node pages from search results

File

search_exclude_nid.module
View source
<?php

/**
 * @file
 * Excludes single node pages from search results
 */

/**
 * Implements hook_menu().
 */
function search_exclude_nid_menu() {
  $items = array();
  $items['admin/config/search/search_exclusion'] = array(
    'file' => 'search_exclude_nid.admin.inc',
    'title' => 'Hide nodes from search',
    'description' => 'Excludes nodes from the site search based on their node ID. Handy if you have duplicated content somewhere on listing pages for instance.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'search_exclude_nid_search_exclusion_form',
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      'administer search exclude nids',
    ),
  );

  // Path with autocomplete function for nodes.
  $items['search_exclusion/autocomplete'] = array(
    'file' => 'search_exclude_nid.admin.inc',
    'title' => 'Autocomplete for nodes',
    'page callback' => 'search_exclude_nid_nodes_autocomplete',
    'access arguments' => array(
      'administer search exclude nids',
    ),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function search_exclude_nid_permission() {
  return array(
    'administer search exclude nids' => array(
      'title' => t('Administer search exclude nid'),
      'description' => t('Access to the configuration page of the module and the autocomplete function to lookup the node id to a node title.'),
      'restrict access' => TRUE,
    ),
  );
}

/**
 * Implements hook_query_alter().
 */
function search_exclude_nid_query_alter(QueryAlterableInterface $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.
        $excluded_nids = variable_get('search_exclude_nid_search_exclusion_nids', array());
        if (count($excluded_nids)) {

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