-
Notifications
You must be signed in to change notification settings - Fork 0
/
google_cse_alt.theme.inc
executable file
·143 lines (118 loc) · 4.59 KB
/
google_cse_alt.theme.inc
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
<?php
/**
* @file
* Themeable functions for Google Custom Search Engine.
*/
/**
* Return the CSE tag for the desired display mode.
*/
function google_cse_alt_get_cse_tag($id) {
$config_name = _google_cse_alt_config_name();
$display = config_get($config_name, "google_cse_alt_custom_results_display_{$id}");
$attributes = filter_xss(config_get($config_name, "google_cse_alt_tag_attributes_{$id}"));
switch ($display) {
case 'overlay':
case 'compact':
case 'full-width':
return '<gcse:search ' . $attributes . '></gcse:search>';
case 'two-page':
return '<gcse:searchbox-only ' . $attributes . '></gcse:searchbox-only><gcse:searchresults-only ' . $attributes . '></gcse:searchresults-only>';
case 'two-column':
return '<gcse:searchbox ' . $attributes . '></gcse:searchbox><gcse:searchresults ' . $attributes . '></gcse:searchresults>';
case 'results-only':
return '<gcse:searchresults-only ' . $attributes . '></gcse:searchresults-only>';
case 'google-hosted':
return '<gcse:searchbox-only ' . $attributes . '></gcse:searchbox-only>';
default:
watchdog('google_cse_alt', 'Invalid custom result display %display',
array('%display' => $display), WATCHDOG_CRITICAL);
}
}
/**
* Theme/customize the search results page.
*/
function template_preprocess_google_cse_alt_results(&$variables) {
$config_name = _google_cse_alt_config_name();
$id = $variables['cse_id'];
$results_searchbox_form = $variables['form'] ?
backdrop_get_form('google_cse_alt_results_searchbox_form', $id) : '';
// $fooform = $results_searchbox_form;
// dpm($fooform,'$fooform');
// this would be a place to append $id to to $results_searchbox_form['#form_id'] to give it a unique ID, but we still need to get the id into the Javascript processor in google_cse_alt_results.js.
$variables['results_searchbox_form'] = $results_searchbox_form;
$variables['cse_tag'] = google_cse_alt_get_cse_tag($id);
$variables['noscript'] = t('You must enable JavaScript to view the search results.');
$variables['prefix'] =
filter_xss_admin(config_get($config_name, "google_cse_alt_results_prefix_{$id}"));
$variables['suffix'] =
filter_xss_admin(config_get($config_name, "google_cse_alt_results_suffix_{$id}"));
if (google_cse_alt_validate_request($id)) {
backdrop_add_js(
backdrop_get_path('module', 'google_cse_alt') . '/js/google_cse_alt_results.js',
array('scope' => 'footer'));
backdrop_add_css(config_get($config_name, "google_cse_alt_custom_css_{$id}"),
'external');
}
}
/**
* Validate GET parameters to avoid displaying inappropriate search results.
*/
function google_cse_alt_validate_request($id) {
$config_name = _google_cse_alt_config_name();
return (empty($_GET['cx']) ||
$_GET['cx'] ==config_get($config_name, "google_cse_alt_cx_{$id}"));
}
/**
* Form builder for the searchbox forms.
*/
function google_cse_alt_results_searchbox_form($form, &$form_state, $id = '') {
$config_name = _google_cse_alt_config_name();
$form = array();
$form['#attributes'] = array('class' => "google-cse-alt-id-{$id}");
if (config_get($config_name, "google_cse_alt_results_display_{$id}") == 'here') {
$cof = config_get($config_name, "google_cse_alt_cof_here_{$id}");
}
else {
$form['#action'] = '//' .
config_get($config_name, 'google_cse_alt_domain') . '/cse';
$cof = config_get($config_name, "google_cse_alt_cof_google_{$id}");
}
$form['#method'] = 'get';
$form['cx'] = array(
'#type' => 'hidden',
'#value' => config_get($config_name, "google_cse_alt_cx_{$id}"),
);
$form['cof'] = array(
'#type' => 'hidden',
'#value' => $cof,
);
$form['query'] = array(
'#type' => 'textfield',
'#title' => config_get($config_name, "google_cse_alt_results_searchbox_title_{$id}"),
'#size' => config_get($config_name, "google_cse_alt_results_searchbox_width_{$id}"),
'#default_value' => isset($_GET['query']) ? $_GET['query'] : '',
);
$placeholder = config_get($config_name, "google_cse_alt_placeholder_{$id}");
if (!empty($placeholder)) {
$form['query']['#attributes'] = array('placeholder' => $placeholder);
}
$glyph = config_get($config_name, "google_cse_alt_glyph_{$id}");
if (empty($glyph)) {
$form['sa'] = array(
'#type' => 'submit',
'#value' => "Search",
);
}
else {
$form['sa'] = array(
'#type' => 'submit',
'#value' => " ",
);
$form['glyph'] = array(
'#markup' => strip_tags($glyph, '<i><span>'),
'#prefix' => '<span class="google-cse-alt-search-glyph">',
'#suffix' => '</span>',
);
}
return $form;
}