forked from grasmash/drupal-usasearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usasearch.module
127 lines (107 loc) · 3.87 KB
/
usasearch.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/**
* @file
* Replaces Drupal search, redirects all search queries to USASearch service.
*/
/**
* Implements hook_menu().
*/
function usasearch_menu() {
$items = array();
$items['admin/config/search/usasearch'] = array(
'title' => 'USASearch settings',
'description' => 'Contains settings for customizing site search to use USASearch',
'page callback' => 'drupal_get_form',
'page arguments' => array('usasearch_admin'),
'access arguments' => array('administer search'),
'file' => 'usasearch.admin.inc',
);
return $items;
}
/**
* Implements hook_form_alter().
*
* Changes elements in the search block, search theme form, search page form.
*/
function usasearch_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'search_block_form' || $form_id == 'search_theme_form' || $form_id == 'search_form') {
$affiliate_name = check_plain(variable_get('usasearch_affiliate_name', ''));
if ($affiliate_name) {
// Create attributes array for textfield.
$attributes = array(
'class' => array('usagov-search-autocomplete',
'ui-autocomplete-input',
'ui-corner-all'),
'autocomplete' => 'off',
'type' => 'text',
// Adding the id attribute can cause issues that rely on default Drupal
// ids for form elements. It does not appear to be necessary for
// typeahead to function. So, we leave it out.
// 'id' => 'query',
);
// Apply attriubutes to the correct form element.
switch ($form_id) {
case 'search_form':
$form['basic']['keys']['#attributes'] = $attributes;
break;
default:
$form[$form_id]['#attributes'] = $attributes;
break;
}
}
}
}
/**
* Implements hook_preprocess_page().
*
* Outputs required javascript and css references.
*/
function usasearch_preprocess_page(&$variables) {
$inline_js = '';
// Check for usasearch_affiliate_name variable, if set output javascript and
// css references. Detailed info about this USASearch feature here:
// @see http://usasearch.howto.gov/post/18861028503/how-to-add-our-code-to-your-website.
$affiliate_name = check_plain(variable_get('usasearch_affiliate_name', ''));
if ($affiliate_name) {
$action_domain = check_plain(variable_get('usasearch_action_domain', 'http://search.usa.gov'));
$inline_js .= "var usasearch_config = { siteHandle:'$affiliate_name' };\n";
if (variable_get('usasearch_autocomplete', TRUE)) {
$inline_js .= "var script = document.createElement('script');\n";
$inline_js .= "script.type = 'text/javascript';\n";
$inline_js .= "script.src = '$action_domain/javascripts/remote.loader.js';\n";
$inline_js .= "document.getElementsByTagName('head')[0].appendChild(script);\n";
}
}
// Output contents of $inline_js into one combined inline javascript tag using
// drupal_add_js function.
drupal_add_js($inline_js, array('type' => 'inline', 'scope' => 'footer'));
}
/**
* Returns an array of subsites.
*
* @return array
* The available subsites.
*/
function usasearch_affiliates_list() {
$subsites_string = variable_get('usasearch_allowed_affiliates', '');
module_load_include('module', 'list');
$subsites = list_extract_allowed_values($subsites_string, 'list text', FALSE);
$default_affiliate = variable_get('usasearch_affiliate_name');
if ($default_affiliate && empty($subsites[$default_affiliate])) {
$subsites = array($default_affiliate => t('Default')) + $subsites;
}
return $subsites;
}
/**
* Verifies that a given subsite is in list of configured subsites.
*
* @param string $affiliate_id
* The affiliate id to verify.
*
* @return bool
* TRUE if the given subsite is in the list of configured subsites.
*/
function usasearch_is_affiliate($affiliate_id) {
$subsites = usasearch_affiliates_list();
return array_key_exists($affiliate_id, $subsites);
}