-
Notifications
You must be signed in to change notification settings - Fork 1
/
search_exclude_nid.module
74 lines (69 loc) · 2.32 KB
/
search_exclude_nid.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
/**
* @file
* Excludes single node pages from search results
*/
/**
* Implements hook_config_info().
*/
function search_exclude_nid_config_info() {
$prefixes['search_exclude_nid.settings'] = array(
'label' => t('Search Exclude by Node ID settings'),
'group' => t('Configuration'),
);
return $prefixes;
}
/**
* 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' => 'backdrop_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 = config_get('search_exclude_nid.settings', 'search_exclusion_nids');
if (count($excluded_nids)) {
// Remove the node IDs from the query.
$query->condition('n.nid', $excluded_nids, 'NOT IN');
}
}
}
}
}