-
Notifications
You must be signed in to change notification settings - Fork 0
/
express_help.module
316 lines (296 loc) · 11.7 KB
/
express_help.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php
/**
* Implements hook_menu().
*/
function express_help_menu() {
$items['admin/express/help'] = array(
'title' => 'Web Express Help',
'page callback' => '_express_help_list',
'access arguments' => array('administer nodes'),
'weight' => 500,
'type' => MENU_CALLBACK,
);
$items['admin/express/help/view'] = array(
'title' => 'Web Express Help',
'type' => MENU_CALLBACK,
);
$items['admin/express/help/admin'] = array(
'type' => MENU_CALLBACK,
'title' => 'Web Express Help Admin',
'page callback' => '_express_help_list_admin',
'access arguments' => array('administer site configuration'),
);
$items['admin/config/system/express-help'] = array(
'title' => 'Express Help',
'description' => 'Configure Express Help',
'page callback' => 'drupal_get_form',
'page arguments' => array('express_help_admin_settings'),
'file' => 'express_help.admin.inc',
'access arguments' => array('administer modules'),
);
return $items;
}
/**
* Implements hook_menu_alter().
*/
function express_help_menu_alter(&$items) {
// Send user to express help page instead of default help page
//$items['admin/help']['page callback'] = 'drupal_goto';
//$items['admin/help']['page arguments'] = array( 'admin/express/help');
unset($items['admin/help']);
}
/**
* Implements hook_navbar().
*/
function express_help_navbar() {
$items = array();
// responsive_preview has a weight of 200 and right aligned
// Help needs to be to the left of that?
$items['express_help'] = array(
'#type' => 'navbar_item',
'tab' => array(
'#type' => 'link',
'#title' => t('Help'),
'#href' => 'http://colorado.edu/webcentral',
'#options' => array(
'html' => TRUE,
'attributes' => array(
'title' => t('Web Express Help'),
'class' => array('navbar-icon navbar-icon-help'),
'target' => '_blank',
),
),
),
'#weight' => 100,
'#wrapper_attributes' => array(
'class' => array('pull-right'),
),
);
return $items;
}
/**
* Callback function for admin/express/help.
*/
function _express_help_list() {
$output = array();
// Get page/screen help
if ($cache = cache_get('express_help_links')) {
$express_help = $cache->data;
} else {
$express_help = module_invoke_all('express_help', $variables = NULL);
// Save this for 30 days - it should only be cleared on a code update
cache_set('express_help_links', $express_help, 'cache', time() + 60 * 60 * 24 * 30);
}
// Get form/field help
if ($cache = cache_get('express_help_field_links')) {
$help_fields = $cache->data;
} else {
$help_fields = module_invoke_all('express_help_fields', $variables = NULL);
// Save this for 30 days - it should only be cleared on a code update
cache_set('express_help_field_links', $help_fields, 'cache', time() + 60 * 60 * 24 * 30);
}
$help_list = array(
'items' => array(),
// Leave the title element empty to omit the ul title.
'title' => '',
'type' => 'ul',
'attributes' => array('class' => array(
'help-list',
)),
);
foreach ($express_help as $help) {
foreach ($help as $key => $value) {
$help_list['items'][$value['short_title']] = l($value['short_title'], $value['url'], array('attributes' => array('target'=>'_blank')));
}
}
foreach ($help_fields as $form_id => $form) {
foreach ($form['fields'] as $field_name => $field) {
$help_list['items'][$field['short_title']] = l($field['short_title'], $field['url'], array('attributes' => array('target'=>'_blank')));
}
}
ksort($help_list['items']);
$output['list']['#markup'] = theme('item_list', $help_list);
$output['more']['#markup'] = l('Find more Web Express help at www.colorado.edu/webcentral', 'http://www.colorado.edu/webcentral', array('attributes' => array('target'=>'_blank', 'class' => array('help-more-link'))));
$output['more']['#prefix'] = '<p>';
$output['more']['#suffix'] = '</p>';
return $output;
}
/**
* Callback function for admin/express/help/admin.
*/
function _express_help_list_admin() {
$modules_page_help = module_implements('express_help');
$modules_field_help = module_implements('express_help_fields');
$files = system_rebuild_module_data();
// Do page/screen level help first
$items = array();
foreach ($modules_page_help as $module) {
$items[] = check_plain($files[$module]->info['name']);
}
$output['page_help']['#markup'] = theme('item_list', array('items' => $items));
$output['page_help']['#prefix'] = t('<strong>These modules provide page/screen level help:</strong>');
// Now do field/fieldset level help
$items = array();
foreach ($modules_field_help as $module) {
$items[] = check_plain($files[$module]->info['name']);
}
$output['field_help']['#markup'] = theme('item_list', array('items' => $items));
$output['field_help']['#prefix'] = t('<strong>These modules provide field/fieldset level help:</strong>');
return $output;
}
/**
* Implements hook_preprocess_page().
*/
function express_help_preprocess_page(&$vars) {
$custom['admin/settings/feeds/rss/manage/%/edit'] = 'cu_rss_express_help_callback';
//variable_set('express_help_custom_paths', $custom);
// Get help paths from cache
if ($cache = cache_get('express_help_links')) {
$express_help = $cache->data;
} else {
$express_help = module_invoke_all('express_help', $variables = NULL);
// Save this for 30 days - it should only be cleared on a code update
cache_set('express_help_links', $express_help, 'cache', time() + 60 * 60 * 24 * 30);
}
// Get current path and change it to reflect help links.
$current_path = current_path();
if ((arg(0) == 'node') && (arg(2) == 'edit')) {
$node_type = str_replace('_', '-', $vars['node']->type);
$current_path = 'node/add/' . $node_type;
}
elseif (arg(0) == 'node' && arg(2) == 'webform-results') {
$current_path = 'node/%/webform-results';
}
elseif (arg(0) == 'admin' && arg(1) == 'structure' && arg(2) == 'quicktabs') {
$current_path = 'admin/structure/quicktabs/%';
}
elseif (arg(0) == 'node' && is_numeric(arg(1))) {
$current_path = explode('/', $current_path);
$current_path[1] = '%';
$current_path = implode('/', $current_path);
}
elseif (arg(0) == 'block' && arg(2) == 'edit') {
$bean_type = str_replace('_', '-', $vars['page']['content']['system_main']['bean']['#value']->type);
$current_path = 'block/edit/' . $bean_type;
}
// Call custom functions for checking help paths.
else {
if ($cache = cache_get('express_help_custom_paths')) {
$express_help_custom_paths = $cache->data;
} else {
$express_help_custom_paths = module_invoke_all('express_help_custom_paths', $variables = NULL);
// Save this for 30 days - it should only be cleared on a code update
cache_set('express_help_custom_paths', $express_help_custom_paths, 'cache', time() + 60 * 60 * 24 * 30);
}
foreach ($express_help_custom_paths as $custom_path_key => $custom_path_callback) {
if (function_exists($custom_path_callback)) {
$current_path = $custom_path_callback($custom_path_key, $current_path);
}
}
}
// If there is a $key for the current path, add the help link variable
if (!empty($express_help[$current_path])) {
$vars['express_help'] = '';
foreach ($express_help[$current_path] as $key => $value) {
$title = $express_help[$current_path][$key]['title'];
$url = $express_help[$current_path][$key]['url'];
// Add base url if url is a fragment/relative.
$parsed_url = parse_url($url);
if (!in_array('host', $parsed_url) && $base_help_url = variable_get('express_help_base_url', 0)) {
// Add beginning slash if not one there.
if (substr($parsed_url['path'], 0, 1) != '/') {
$parsed_url['path'] = '/' . $parsed_url['path'];
}
$url = $base_help_url . $parsed_url['path'];
}
if (isset($title) && isset($url)) {
$help_link = l($title, $url, array('attributes' => array('target' => '_blank')));
$vars['express_help'] .= $help_link . '<br/>';
}
}
}
}
/**
* Implements hook_form_alter().
*/
function express_help_form_alter(&$form, &$form_state, $form_id) {
// Get help paths from cache
if ($cache = cache_get('express_help_field_links')) {
$help_fields = $cache->data;
} else {
$combined_keys = array();
// Get all help field hooks
$help_fields = module_invoke_all('express_help_fields', $variables = NULL);
foreach ($help_fields as $key => $help_field) {
// Check for combined form keys
if (strpos($key, ',') !== FALSE) {
$fields = $help_field['fields'];
// Save combined keys so we can remove them later
$combined_keys[] = $key;
$form_keys = array();
$form_keys = explode(',', $key);
foreach ($form_keys as $form_key) {
// If form already has some field help, merge them
if (!empty($help_fields[$form_key])) {
$help_fields[$form_key]['fields'] = array_merge($help_fields[$form_key]['fields'], $fields);
} else {
$help_fields[$form_key]['fields'] = $fields;
}
}
}
}
// Remove combined keys because we don't need them anymore.
foreach ($combined_keys as $combined_key) {
unset($help_fields[$combined_key]);
}
// Save this for 30 days - it should only be cleared on a code update
cache_set('express_help_field_links', $help_fields, 'cache', time() + 60 * 60 * 24 * 30);
}
// If the form_id is a key in our field help array, proceed with alter
if (array_key_exists($form_id, $help_fields)) {
foreach ($form as $key => $field) {
if (!empty($help_fields[$form_id]['fields'][$key])) {
// Add base url if url is a fragment/relative.
$url = $help_fields[$form_id]['fields'][$key]['url'];
$parsed_url = parse_url($url);
if (!in_array('host', $parsed_url) && $base_help_url = variable_get('express_help_base_url', 0)) {
// Add beginning slash if not one there.
if (substr($parsed_url['path'], 0, 1) != '/') {
$parsed_url['path'] = '/' . $parsed_url['path'];
}
$url = $base_help_url . $parsed_url['path'];
}
$help_link = l('<span class="element-invisible1">' . $help_fields[$form_id]['fields'][$key]['title'] . '</span>', $url, array('attributes' => array('target'=>'_blank', 'class' => 'field-help-link'), 'html' => TRUE));
// If description is set at this level (i.e. a fieldset), add help
if ($form[$key]['#type'] == 'fieldset') {
if (isset($form[$key]['#description'])) {
$form[$key]['#description'] = $form[$key]['#description'] . ' ' . $help_link;
} else {
$form[$key]['#description'] = $help_link;
}
}
// otherwise add help here
else {
if (isset($form[$key][LANGUAGE_NONE][0]['#description'])) {
$form[$key][LANGUAGE_NONE][0]['#description'] = $form[$key][LANGUAGE_NONE][0]['#description'] . ' ' . $help_link;
} else {
$form[$key][LANGUAGE_NONE][0]['#description'] = $help_link;
}
if (isset($form[$key][LANGUAGE_NONE]['#description'])) {
$form[$key][LANGUAGE_NONE]['#description'] = $form[$key][LANGUAGE_NONE]['#description'] . ' ' . $help_link;
} else {
$form[$key][LANGUAGE_NONE]['#description'] = $help_link;
}
if (isset($form[$key]['#description'])) {
$form[$key]['#description'] = $form[$key]['#description'] . ' ' . $help_link;
} else {
$form[$key]['#description'] = $help_link;
}
}
}
}
}
}
//function express_help_field_widget_form_alter(&$element, &$form_state, $context) {
//
//}