forked from skilld-labs/google_cse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
google_cse.module
277 lines (247 loc) · 8.31 KB
/
google_cse.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
/**
* @file
* Display a Google Custom Search Engine (CSE) on your site.
*/
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function google_cse_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the google_cse module.
case 'help.page.google_cse':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Use Google Custom Search to search your site and/or any other sites.') . '</p>';
return $output;
default:
}
}
/**
* Builds a query array based on Google CSE settings.
*
* @todo Convert according https://www.drupal.org/node/2083471
*/
function google_cse_build_query($keys, $sitesearch = NULL, $here = TRUE) {
if (!\Drupal::config('google_cse.settings')->get('use_adv')) {
return array('query' => $keys);
}
return array(
'query' => $keys,
'cx' => \Drupal::config('google_cse.settings')->get('cx'),
'cof' => $here ? \Drupal::config('google_cse.settings')->get('cof_here') : \Drupal::config('google_cse.settings')->get('cof_google'),
'sitesearch' => isset($sitesearch) ? $sitesearch : google_cse_sitesearch_default(),
) + google_cse_advanced_settings();
}
/**
* Implements hook_search_page().
*/
function google_cse_search_page($results) {
if (!\Drupal::config('google_cse.settings')->get('use_adv')) {
$output['#theme'] = 'google_cse_results';
return $output;
}
if (!$results) {
// No results found.
// @FIXME
// theme() has been renamed to _theme() and should NEVER be called directly.
// Calling _theme() directly can alter the expected output and potentially
// introduce security issues (see https://www.drupal.org/node/2195739). You
// should use renderable arrays instead.
//
//
// @see https://www.drupal.org/node/2195739
// $output['search_results'] = array('#markup' => theme('google_cse_search_noresults'));
return $output;
}
if (!empty($_GET['page'])) {
$current_page = $_GET['page'];
$number_results = t('Results @from to @to of @total matches.', array(
'@from' => $current_page * 10,
'@to' => $current_page * 10 + 10,
'@total' => $GLOBALS['pager_total_items'][0],
));
$output['prefix']['#markup'] = $number_results . '<ol class="search-results">';
}
foreach ($results as $entry) {
$output[] = array(
'#theme' => 'search_result',
'#result' => $entry,
'#module' => 'google_cse',
);
}
if (!empty($_GET['page'])) {
// Important, add the pager.
// @FIXME
// theme() has been renamed to _theme() and should NEVER be called directly.
// Calling _theme() directly can alter the expected output and potentially
// introduce security issues (see https://www.drupal.org/node/2195739). You
// should use renderable arrays instead.
//
//
// @see https://www.drupal.org/node/2195739
// $output['suffix']['#markup'] = '</ol>' . theme('pager');
}
return $output;
}
/**
* Implements hook_theme().
*/
function google_cse_theme($existing, $type, $theme, $path) {
return array(
'google_cse_results' => array(
'variables' => array('form' => FALSE, 'path' => $path),
'file' => 'google_cse.theme.inc',
'template' => 'google_cse_results',
),
'google_cse_adv_results' => array(
'variables' => array('form' => FALSE, 'path' => $path),
'file' => 'google_cse.theme.inc',
'template' => 'google_cse_adv/templates/google_cse_adv_results',
),
// Shows a message when the search does not return any result.
'google_cse_search_noresults' => array(
'variables' => array(),
),
);
}
/**
* Return the Google CSE tab title, either a setting or a translation.
*/
function google_cse_results_tab() {
return ($var = \Drupal::config('google_cse.settings')->get('results_tab')) ? $var : t('Google');
}
/**
* Returns an array of any advanced settings which have been set.
*/
function google_cse_advanced_settings() {
$language = \Drupal::languageManager()->getCurrentLanguage();
$settings = array();
foreach (array('cr', 'gl', 'hl', 'ie', 'lr', 'oe', 'safe') as $parameter) {
$settings[$parameter] = \Drupal::config('google_cse.settings')->get('' . $parameter);
}
if (\Drupal::config('google_cse.settings')->get('locale_hl')) {
$settings['hl'] = $language->language;
}
if (\Drupal::config('google_cse.settings')->get('locale_lr')) {
$settings['lr'] = 'lang_' . $language->language;
}
return $settings;
}
/**
* Get the relevant language to use for the search.
*
* @return string
* The language.
*/
function google_cse_language() {
$language = \Drupal::languageManager()->getCurrentLanguage()->getId();
$config = \Drupal::config('google_cse.settings');
return $config->get('locale_hl') ? $language : $config->get('hl');
}
/**
* Returns SiteSearch options form item.
*/
function google_cse_sitesearch_form(&$form) {
if ($options = google_cse_sitesearch_options()) {
$form['sitesearch'] = array(
'#type' => \Drupal::config('google_cse.settings')->get('sitesearch_form'),
'#options' => $options,
'#default_value' => google_cse_sitesearch_default(),
);
if ($form['sitesearch']['#type'] == 'select' && isset($form['sa'])) {
$form['sa']['#weight'] = 10;
}
$form['#attributes']['class'][] = 'google-cse';
}
}
/**
* Returns SiteSearch options.
*/
function google_cse_sitesearch_options() {
static $options;
if (!isset($options)) {
$options = array();
if ($sites = preg_split('/[\n\r]+/', \Drupal::config('google_cse.settings')->get('sitesearch'), -1, PREG_SPLIT_NO_EMPTY)) {
$options[''] = ($var = \Drupal::config('google_cse.settings')->get('sitesearch_option')) ? $var : t('Search the web');
foreach ($sites as $site) {
$site = preg_split('/[\s]+/', trim($site), 2, PREG_SPLIT_NO_EMPTY);
// Select options will be HTML-escaped.
// Radio options will be XSS-filtered.
$options[$site[0]] = isset($site[1]) ? $site[1] : t('Search %sitesearch', array('%sitesearch' => $site[0]));
}
}
}
return $options;
}
/**
* Returns SiteSearch default value.
*/
function google_cse_sitesearch_default() {
$options = google_cse_sitesearch_options();
if (isset($_GET['sitesearch']) && isset($options[$_GET['sitesearch']])) {
return $_GET['sitesearch'];
}
elseif (\Drupal::config('google_cse.settings')->get('sitesearch_default')) {
// Return the key of the second element in the array.
return key(array_slice($options, 1, 1));
}
return '';
}
/**
* Adds custom submit handler for search form.
*/
function google_cse_form_search_form_alter(&$form, &$form_state, $form_id) {
$repo = \Drupal::service('search.search_page_repository');
if ($repo->getDefaultSearchPage() == 'google_cse_search_type') {
google_cse_sitesearch_form($form);
}
}
/**
* Adds custom submit handler for search block form.
*/
function google_cse_form_search_block_form_alter(&$form, &$form_state, $form_id) {
/** @var \Drupal\search\SearchPageRepositoryInterface $repo */
$repo = \Drupal::service('search.search_page_repository');
if ($repo->getDefaultSearchPage() == 'google_cse_search_type') {
$config = \Drupal::config('google_cse.settings');
google_cse_sitesearch_form($form);
$form['#attached']['library'] = [
'core/drupalSettings',
'google_cse/googleCseWatermarkLibrary',
];
$form['#attached']['drupalSettings'] = [
'googleCSE' => [
'cx' => $config->get('cx'),
'language' => google_cse_language(),
'resultsWidth' => intval($config->get('google_cse_results_width', 600)),
'domain' => $config->get('google_cse_domain', 'www.google.com'),
],
];
}
}
/**
* Implements hook_proxy_settings_info().
*/
function google_cse_proxy_settings_info() {
return array(
'google_cse_adv' => array(
'name' => 'Google Custom Search Engine',
),
);
}
/**
* Brief message to display when no results match the query.
*
* @see search_help()
*/
function theme_google_cse_search_noresults() {
return t('<h2>Sorry there were no results matching your enquiry.</h2>
<ul>
<li>Check the spelling of your keywords</li>
<li>Try a more specific enquiry (e.g. <em>"Penny Black"</em> instead of <em>"Stamps"</em>): "blue drop"</em></li>
<li>Be explicit (e.g. <em>"Second class stamp"</em> instead of <em>"Stamp"</em>)</li>
<li>Include spaces between keywords</li>
</ul>');
}