1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Handle the 'node view' override task.
|
6
|
*
|
7
|
* This plugin overrides node/%node and reroutes it to the page manager, where
|
8
|
* a list of tasks can be used to service this request based upon criteria
|
9
|
* supplied by access plugins.
|
10
|
*/
|
11
|
|
12
|
/**
|
13
|
* Specialized implementation of hook_page_manager_task_tasks().
|
14
|
*
|
15
|
* See api-task.html for more information.
|
16
|
*/
|
17
|
function page_manager_search_page_manager_tasks() {
|
18
|
if (!module_exists('search')) {
|
19
|
return;
|
20
|
}
|
21
|
|
22
|
return array(
|
23
|
// This is a 'page' task and will fall under the page admin UI.
|
24
|
'task type' => 'page',
|
25
|
'title' => t('Search'),
|
26
|
|
27
|
// There are multiple search pages, let's override each of them
|
28
|
// separately.
|
29
|
'subtasks' => TRUE,
|
30
|
'subtask callback' => 'page_manager_search_subtask',
|
31
|
'subtasks callback' => 'page_manager_search_subtasks',
|
32
|
|
33
|
// Menu hooks so that we can alter the node/%node menu entry to point to us.
|
34
|
'hook menu alter' => 'page_manager_search_menu_alter',
|
35
|
|
36
|
// This is task uses 'context' handlers and must implement these to give the
|
37
|
// handler data it needs.
|
38
|
'handler type' => 'context',
|
39
|
'get arguments' => 'page_manager_search_get_arguments',
|
40
|
'get context placeholders' => 'page_manager_search_get_contexts',
|
41
|
'access callback' => 'page_manager_search_access_check',
|
42
|
);
|
43
|
}
|
44
|
|
45
|
/**
|
46
|
* Callback defined by page_manager_search_page_manager_tasks().
|
47
|
*
|
48
|
* Alter the search tabs to work with page manager. The search flow is
|
49
|
* quite odd, and tracing through the code takes hours to realize
|
50
|
* that the tab you click on does not normally actually handle
|
51
|
* the search. This tries to account for that.
|
52
|
*
|
53
|
* Note to module authors: This tends to work a lot better with modules
|
54
|
* that override their own search pages if their _alter runs *before*
|
55
|
* this one.
|
56
|
*/
|
57
|
function page_manager_search_menu_alter(&$items, $task) {
|
58
|
// We are creating two sets of tabs. One set is for searching without
|
59
|
// keywords. A second set is for searching *with* keywords. This
|
60
|
// is necessary because search/node/% and search/node need to be
|
61
|
// different due to the way the search menu items function.
|
62
|
$default_info = search_get_default_module_info();
|
63
|
if (empty($default_info)) {
|
64
|
// Nothing to do.
|
65
|
return;
|
66
|
}
|
67
|
|
68
|
// Go through each search module item.
|
69
|
foreach (search_get_info() as $module => $info) {
|
70
|
if (variable_get('page_manager_search_disabled_' . $module, TRUE)) {
|
71
|
continue;
|
72
|
}
|
73
|
|
74
|
$path = 'search/' . $info['path'];
|
75
|
$callback = $items["$path/%menu_tail"]['page callback'];
|
76
|
|
77
|
if ($callback == 'search_view' || variable_get('page_manager_override_anyway', FALSE)) {
|
78
|
$items["$path"]['page callback'] = 'page_manager_search_page';
|
79
|
$items["$path"]['file path'] = $task['path'];
|
80
|
$items["$path"]['file'] = $task['file'];
|
81
|
|
82
|
$items["$path/%menu_tail"]['page callback'] = 'page_manager_search_page';
|
83
|
$items["$path/%menu_tail"]['file path'] = $task['path'];
|
84
|
$items["$path/%menu_tail"]['file'] = $task['file'];
|
85
|
}
|
86
|
else {
|
87
|
// Automatically disable this task if it cannot be enabled.
|
88
|
variable_set('page_manager_search_disabled_' . $module, TRUE);
|
89
|
if (!empty($GLOBALS['page_manager_enabling_search'])) {
|
90
|
drupal_set_message(t('Page manager module is unable to enable @path because some other module already has overridden with %callback.', array('%callback' => $callback, '@path' => $path)), 'error');
|
91
|
}
|
92
|
}
|
93
|
}
|
94
|
}
|
95
|
|
96
|
/**
|
97
|
* Entry point for our overridden search page.
|
98
|
*/
|
99
|
function page_manager_search_page($type) {
|
100
|
// Get the arguments and construct a keys string out of them.
|
101
|
$args = func_get_args();
|
102
|
ctools_include('menu');
|
103
|
|
104
|
// We have to remove the $type.
|
105
|
array_shift($args);
|
106
|
|
107
|
// And implode() it all back together.
|
108
|
$keys = $args ? implode('/', $args) : '';
|
109
|
|
110
|
// Allow other modules to alter the search keys.
|
111
|
drupal_alter(array('search_keys', 'search_' . $type . '_keys'), $keys);
|
112
|
|
113
|
// Load my task plugin.
|
114
|
$task = page_manager_get_task('search');
|
115
|
$subtask = page_manager_get_task_subtask($task, $type);
|
116
|
|
117
|
// Load the node into a context.
|
118
|
ctools_include('context');
|
119
|
ctools_include('context-task-handler');
|
120
|
$contexts = ctools_context_handler_get_task_contexts($task, $subtask, array($keys));
|
121
|
|
122
|
$output = ctools_context_handler_render($task, $subtask, $contexts, array($keys));
|
123
|
if ($output !== FALSE) {
|
124
|
return $output;
|
125
|
}
|
126
|
|
127
|
$function = 'search_view';
|
128
|
foreach (module_implements('page_manager_override') as $module) {
|
129
|
$call = $module . '_page_manager_override';
|
130
|
if (($rc = $call('search')) && function_exists($rc)) {
|
131
|
$function = $rc;
|
132
|
break;
|
133
|
}
|
134
|
}
|
135
|
|
136
|
// Otherwise, fall back.
|
137
|
// Put the $type back on the arguments.
|
138
|
module_load_include('inc', 'search', 'search.pages');
|
139
|
array_unshift($args, $type);
|
140
|
return call_user_func_array($function, $args);
|
141
|
}
|
142
|
|
143
|
/**
|
144
|
* Callback to get arguments provided by this task handler.
|
145
|
*
|
146
|
* Since this is the node view and there is no UI on the arguments, we
|
147
|
* create dummy arguments that contain the needed data.
|
148
|
*/
|
149
|
function page_manager_search_get_arguments($task, $subtask_id) {
|
150
|
return array(
|
151
|
array(
|
152
|
'keyword' => 'keywords',
|
153
|
'identifier' => t('Keywords'),
|
154
|
'id' => 1,
|
155
|
'name' => 'string',
|
156
|
'settings' => array('use_tail' => TRUE),
|
157
|
),
|
158
|
);
|
159
|
}
|
160
|
|
161
|
/**
|
162
|
* Callback to get context placeholders provided by this handler.
|
163
|
*/
|
164
|
function page_manager_search_get_contexts($task, $subtask_id) {
|
165
|
return ctools_context_get_placeholders_from_argument(page_manager_search_get_arguments($task, $subtask_id));
|
166
|
}
|
167
|
|
168
|
/**
|
169
|
* Callback to enable/disable the page from the UI.
|
170
|
*/
|
171
|
function page_manager_search_enable($cache, $status) {
|
172
|
variable_set('page_manager_search_disabled_' . $cache->subtask_id, $status);
|
173
|
|
174
|
// Set a global flag so that the menu routine knows it needs
|
175
|
// to set a message if enabling cannot be done.
|
176
|
if (!$status) {
|
177
|
$GLOBALS['page_manager_enabling_search'] = TRUE;
|
178
|
}
|
179
|
}
|
180
|
|
181
|
/**
|
182
|
* Task callback to get all subtasks.
|
183
|
*
|
184
|
* Return a list of all subtasks.
|
185
|
*/
|
186
|
function page_manager_search_subtasks($task) {
|
187
|
$return = array();
|
188
|
foreach (search_get_info() as $module => $info) {
|
189
|
if ($info['path']) {
|
190
|
// We don't pass the $info because the subtask build could be called
|
191
|
// singly without the $info when just the subtask needs to be built.
|
192
|
$return[$module] = page_manager_search_build_subtask($task, $module);
|
193
|
}
|
194
|
}
|
195
|
|
196
|
return $return;
|
197
|
}
|
198
|
|
199
|
/**
|
200
|
* Callback to return a single subtask.
|
201
|
*/
|
202
|
function page_manager_search_subtask($task, $subtask_id) {
|
203
|
return page_manager_search_build_subtask($task, $subtask_id);
|
204
|
}
|
205
|
|
206
|
/**
|
207
|
* Build a subtask array for a given page.
|
208
|
*/
|
209
|
function page_manager_search_build_subtask($task, $module) {
|
210
|
$search_info = search_get_info();
|
211
|
$info = $search_info[$module];
|
212
|
$path = 'search/' . $info['path'];
|
213
|
$subtask = array(
|
214
|
'name' => $module,
|
215
|
'admin title' => $info['title'],
|
216
|
'admin path' => "$path/!keywords",
|
217
|
'admin description' => t('Search @type', array('@type' => $info['title'])),
|
218
|
'admin type' => t('System'),
|
219
|
'row class' => empty($page->disabled) ? 'page-manager-enabled' : 'page-manager-disabled',
|
220
|
'storage' => t('In code'),
|
221
|
'disabled' => variable_get('page_manager_search_disabled_' . $module, TRUE),
|
222
|
// This works for both enable AND disable.
|
223
|
'enable callback' => 'page_manager_search_enable',
|
224
|
);
|
225
|
|
226
|
return $subtask;
|
227
|
}
|
228
|
|
229
|
/**
|
230
|
* Callback to determine if a page is accessible.
|
231
|
*
|
232
|
* @param $task
|
233
|
* The task plugin.
|
234
|
* @param $subtask_id
|
235
|
* The subtask id
|
236
|
* @param $contexts
|
237
|
* The contexts loaded for the task.
|
238
|
*
|
239
|
* @return
|
240
|
* TRUE if the current user can access the page.
|
241
|
*/
|
242
|
function page_manager_search_access_check($task, $subtask_id, $contexts) {
|
243
|
$context = reset($contexts);
|
244
|
return _search_menu_access($context->data);
|
245
|
}
|