This repository was archived by the owner on May 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcm_tools.module
661 lines (599 loc) · 21.1 KB
/
cm_tools.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
<?php
/**
* @file
* The Computerminds tools module.
*
* We'll try to keep this file as lean as possible, and do as much as possible
* in includes.
*
* This file contains hook implementations and helpers likely to be used on
* every page request.
*/
include_once 'cm_tools.element.inc';
/**
* Include .inc files as necessary.
*
* This fuction is helpful for including .inc files for your module. The
* general case is including cm_tools helper functions like this:
*
* @code
* cm_tools_include('profile');
* @endcode
*
* @param string $file
* The base file name to be included.
* @param string $module
* Optional module containing the include.
* @param string $dir
* Optional subdirectory containing the include file.
*/
function cm_tools_include($file, $module = 'cm_tools', $dir = 'includes') {
static $used = array();
$dir = '/' . ($dir ? $dir . '/' : '');
if (!isset($used[$module][$dir][$file])) {
require_once DRUPAL_ROOT . '/' . drupal_get_path('module', $module) . "$dir$file.inc";
$used[$module][$dir][$file] = TRUE;
}
}
/**
* Include .inc files in a form context.
*
* This is a variant of cm_tools_include that will save information in the
* the form_state so that cached forms will properly include things.
*/
function cm_tools_form_include(&$form_state, $file, $module = 'cm_tools', $dir = 'includes') {
$dir = '/' . ($dir ? $dir . '/' : '');
form_load_include($form_state, 'inc', $module, $dir . $file);
}
/**
* Implements hook_permission().
*/
function cm_tools_permission() {
return array(
'preserve during sanitization' => array(
'title' => t('Preserve during SQL Sanitization'),
'description' => t('Do not change users with this permission when sanitizing the database using Drush.'),
),
);
}
/**
* Implements hook_menu().
*/
function cm_tools_menu() {
$items['cm_tools/monitoring/%'] = array(
'page callback' => 'cm_tools_monitoring_page_callback',
'access callback' => 'cm_tools_monitoring_access_callback',
'access arguments' => array(2),
);
return $items;
}
/**
* Implements hook_init().
*/
function cm_tools_init() {
// Our monitoring page is never in maintenance mode.
if ($_GET['q'] === 'cm_tools/monitoring/' . cm_tools_get_uptime_path_token()) {
$GLOBALS['conf']['maintenance_mode'] = 0;
}
}
/**
* Access callback for the CM Tools monitoring page.
*
* @param string $token
* The token to check.
*
* @return bool
* TRUE if the token matches the one stored for this site. FALSE otherwise.
*/
function cm_tools_monitoring_access_callback($token) {
return $token === cm_tools_get_uptime_path_token();
}
/**
* Page callback for the monitoring page.
*/
function cm_tools_monitoring_page_callback() {
// @todo: We could check to see if Memcache etc. are configured/functioning.
drupal_page_is_cacheable(FALSE);
// Print something unlikely to be printed by e.g. Apache error pages.
print sha1(__FUNCTION__ . cm_tools_get_uptime_path_token());
print "\n";
print date('c');
drupal_exit();
}
/**
* Get a stable token for use in a path that is unique to this site.
*
* @return string
* The path token.
*/
function cm_tools_get_uptime_path_token() {
if ($token = variable_get('cm_tools_get_uptime_path_token')) {
return $token;
}
else {
$token = drupal_random_key(64);
variable_set('cm_tools_get_uptime_path_token', $token);
return $token;
}
}
/**
* Inserts one or more suggestions into a theme_hook_suggestions array.
*
* @param array $haystack
* The $vars['theme_hook_suggestions'] array. It is modified by reference.
* @param string|array $insert_value
* String value after which to insert $suggestions. May also be an array of
* suggested values in which case the first one to be found will be used.
* @param string|array $suggestions
* A single new suggestion, or array of new suggestions to insert.
* @param bool $insert_before
* (Optional) Change the behaviour of this function to insert *before*
* $insert_value instead of after it.
* @param bool|true $append_on_fail
* (Optional) By default this function will append the $suggestions on the end
* of $haystack if $insert_value could not be found. Pass FALSE here to stop
* this and make this function return FALSE in this case.
*
* @return bool
* Success, either TRUE or FALSE.
*/
function cm_tools_insert_theme_hook_suggestion(array &$haystack, $insert_value, $suggestions, $insert_before = FALSE, $append_on_fail = TRUE) {
if (!is_array($insert_value)) {
$insert_value = array($insert_value);
}
$success = FALSE;
foreach ($insert_value as $v) {
if (cm_tools_array_insert_at_value($haystack, $v, $suggestions, $insert_before, FALSE)) {
$success = TRUE;
break;
}
}
if (!$success && $append_on_fail) {
if (!is_array($suggestions)) {
$suggestions = array($suggestions);
}
$haystack = array_merge(array_values($haystack), array_values($suggestions));
$success = TRUE;
}
return $success;
}
/**
* Renames an array key while leaving it in exactly the same place in the array.
*
* @param array $haystack
* The array. This is modified by reference.
* @param int|string $key
* The key to rename.
* @param int|string $new_key
* The new name for the key. If this already exists in the array it will be
* overwritten.
*
* @return bool
* Success, either TRUE or FALSE.
*/
function cm_tools_array_rename_key(array &$haystack, $key, $new_key) {
$success = FALSE;
if (isset($haystack[$key])) {
$insertion = array(
$new_key => $haystack[$key],
);
if ($success = cm_tools_array_insert_at_key($haystack, $key, $insertion)) {
if ($key !== $new_key) {
unset($haystack[$key]);
}
}
}
return $success;
}
/**
* Inserts a new element into an array, just after the one with the given value.
*
* The insert value is found by *strict* comparison. If multiple values are
* found the insertion will be made next to the first occurance only.
*
* @param array $haystack
* The array. This is modified by reference.
* @param mixed $insert_value
* Array value after which to insert $insertion. A *strict* comparison is
* performed when searching for this value.
* @param mixed $insertions
* Array of new values to insert into the array. If $preserve_keys is TRUE and
* an element already exists in the array with the same key as one you are
* inserting, the old one(s) will removed. If this parameter is not an array
* it will be inserted into the array with the lowest available numeric key.
* @param bool $insert_before
* (Optional) Change the behaviour of this function to insert *before*
* $insert_value instead of after it.
* @param bool $preserve_keys
* (Optional) By default this function ignores keys, giving everything a new
* numeric index. Pass TRUE here to indicate that all keys should be
* preserved.
*
* @return bool
* Success, either TRUE or FALSE.
*/
function cm_tools_array_insert_at_value(array &$haystack, $insert_value, $insertions, $insert_before = FALSE, $preserve_keys = FALSE) {
$success = FALSE;
$insert_key = array_search($insert_value, $haystack, TRUE);
if ($insert_key !== FALSE) {
$success = cm_tools_array_insert_at_key($haystack, $insert_key, $insertions, $insert_before, $preserve_keys);
}
return $success;
}
/**
* Inserts a new element into an array, just after the one with the given key.
*
* The insert key is found by *strict* comparison.
*
* @param array $haystack
* The array. This is modified by reference.
* @param int|string|array $insert_key
* Array key after which to insert $insertion. This can also be an array of
* suggested keys, in which case each will be looked for in turn and the
* first existing one will be used. A *strict* comparison is performed when
* searching for this key.
* @param mixed $insertions
* Array of new values to insert into the array. Unless $preserve_keys is
* FALSE if an element already exists in the array with the same key as one
* you are inserting, the old one(s) will removed. If this parameter is not an
* array it will be inserted into the array with the lowest available numeric
* key.
* @param bool $insert_before
* (Optional) Change the behaviour of this function to insert *before*
* $insert_key instead of after it.
* @param bool|true $preserve_keys
* (Optional) By default this function always preserves keys, even for
* numerically-indexed arrays. Pass FALSE here if using a numerically-indexed
* array and you don't want to overwrite elements.
*
* @return bool
* Success, either TRUE or FALSE.
*/
function cm_tools_array_insert_at_key(array &$haystack, $insert_key, $insertions, $insert_before = FALSE, $preserve_keys = TRUE) {
$success = FALSE;
if (!is_array($insert_key)) {
$insert_key = array($insert_key);
}
$haystack_keys = array_keys($haystack);
$offset = FALSE;
while (count($insert_key) && $offset === FALSE) {
$loc = array_shift($insert_key);
$offset = array_search($loc, $haystack_keys, TRUE);
}
if ($offset !== FALSE) {
if ($insert_before) {
$offset = $offset - 1;
}
$success = cm_tools_array_insert_at_offset($haystack, $offset + 1, $insertions, $preserve_keys);
}
return $success;
}
/**
* Inserts a new element at an offset in an array.
*
* @param array $haystack
* The array. This is modified by reference.
* @param int $offset
* Offset after which to insert $insertion.
* @param mixed $insertions
* Array of new values to insert into the array. If $preserve_keys is TRUE and
* an element already exists in the array with the same key as one you are
* inserting, the old one(s) will removed. If this parameter is not an array
* it will be inserted into the array with the lowest available numeric key.
* @param bool $preserve_keys
* (Optional) By default this function ignores keys, giving everything a new
* numeric index. Pass TRUE here to indicate that all keys should be
* preserved.
*
* @return bool
* Success, either TRUE or FALSE.
*/
function cm_tools_array_insert_at_offset(array &$haystack, $offset, $insertions, $preserve_keys = FALSE) {
$success = FALSE;
if (!is_int($offset)) {
return FALSE;
}
if (!$preserve_keys) {
$haystack = array_values($haystack);
}
// Insertions not given as an array. Give it the lowest available numeric key.
if (!is_array($insertions)) {
$numeric_keys = array_filter(array_keys($haystack), 'is_numeric');
$unique_key = !empty($numeric_keys) ? max($numeric_keys) + 1 : 0;
$insertions = array($unique_key => $insertions);
}
if (!$preserve_keys) {
$insertions = array_values($insertions);
}
// Wipe out any keys we're about to overwrite, and ensure we modify the
// $offset to take into account the changed $haystack.
if ($preserve_keys) {
$o = 0;
foreach (array_keys($haystack) as $k) {
if (isset($insertions[$k])) {
if ($o < $offset) {
$offset--;
}
unset($haystack[$k]);
}
$o++;
}
}
$before = array_slice($haystack, 0, $offset, TRUE);
$after = array_slice($haystack, $offset, count($haystack), TRUE);
if (!$preserve_keys) {
$haystack = array_merge($before, $insertions, $after);
$success = TRUE;
}
else {
$keys = array_merge(array_keys($before), array_keys($insertions), array_keys($after));
$values = array_merge($before, $insertions, $after);
$haystack = array_combine($keys, $values);
$success = TRUE;
}
return $success;
}
/**
* Remove element(s) with given value(s) from an array.
*
* @param array $haystack
* The array. This is modified by reference.
* @param string|array $values
* A string value or array of values to remove from the array.
*/
function cm_tools_array_remove_values(array &$haystack, $values) {
// Make values an array.
if (is_string($values)) {
$values = array($values);
}
foreach ($values as $value_to_remove) {
$key = array_search($value_to_remove, $haystack);
if ($key !== FALSE) {
unset($haystack[$key]);
}
}
}
/**
* Sort an array by values using a user-defined comparison function.
*
* However, unlike usort, equal values maintain their order.
*
* The comparison function must return an integer less than, equal to, or
* greater than zero if the first argument is considered to be
* respectively less than, equal to, or greater than the second.
*
* @param array $array
* The input array.
* @param callable $cmp_function
* A comparision function that will abide by the usual semantics of a
* comparision callback. See @usort.
*
* @return int
* The result of the sort.
*/
function cm_tools_stable_usort(array &$array, callable $cmp_function) {
$index = 0;
foreach ($array as &$item) {
$item = array($index++, $item);
}
$result = usort($array, function ($a, $b) use ($cmp_function) {
$result = call_user_func($cmp_function, $a[1], $b[1]);
return $result == 0 ? $a[0] - $b[0] : $result;
});
foreach ($array as &$item) {
$item = $item[1];
}
return $result;
}
/**
* Sort an array by user-defined comparison function, preserving indexes.
*
* However, unlike uasort, equal values maintain their order.
*
* The comparison function must return an integer less than, equal to, or
* greater than zero if the first argument is considered to be
* respectively less than, equal to, or greater than the second.
*
* @param array $array
* The input array.
* @param callable $cmp_function
* A comparision function that will abide by the usual semantics of a
* comparision callback. See @usort.
*
* @return int
* The result of the sort.
*/
function cm_tools_stable_uasort(array &$array, callable $cmp_function) {
$index = 0;
foreach ($array as &$item) {
$item = array($index++, $item);
}
$result = uasort($array, function ($a, $b) use ($cmp_function) {
$result = call_user_func($cmp_function, $a[1], $b[1]);
return $result == 0 ? $a[0] - $b[0] : $result;
});
foreach ($array as &$item) {
$item = $item[1];
}
return $result;
}
/**
* Implements hook_theme().
*/
function cm_tools_theme($existing, $type, $theme, $path) {
$items = array(
'cm_tools_html_wrapper' => array(
'render element' => 'element',
'file' => 'cm_tools.theme.inc',
),
);
return $items;
}
/**
* Implements hook_preprocess_entity().
*
* We add in a full and comprehensive selection of theme hook suggestions,
* consistent across entities. This transforms the default suggestions from:
*
* 1. entitytype--id (eg. node--34.tpl.php)
* 2. entitytype--bundle (eg. node--article.tpl.php)
* 3. entitytype (eg. node.tpl.php)
*
* To a more flexible:
*
* 1. entitytype--id--viewmode (eg. node--34--teaser.tpl.php)
* 2. entitytype--id (eg. node--34.tpl.php)
* 3. entitytype--bundle--viewmode (eg. node--article--teaser.tpl.php)
* 4. entitytype--viewmode (eg. node--teaser.tpl.php)
* 5. entitytype--bundle (eg. node--article.tpl.php)
* 6. entitytype (eg. node.tpl.php)
*
* We also some complementary classes to the entity template classes array,
* in the event that only css is required.
*/
function cm_tools_preprocess_entity(&$vars, $hook, $suggestion_prefix = 'entity__') {
$entity_type = $vars['entity_type'];
$view_mode = isset($vars['view_mode']) ? $vars['view_mode'] : 'full';
// Add a full selection of theme hook suggestions.
if (isset($vars[$entity_type]) && is_object($vars[$entity_type])) {
$entity = $vars[$entity_type];
list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
if (empty($bundle)) {
$bundle = $entity_type;
}
$entity_type = isset($vars['entity_type_theme_name']) ? $vars['entity_type_theme_name'] : $entity_type;
// Ensure theme_hook_suggestions for id, bundle and view_mode.
$suggestion = "{$entity_type}__{$bundle}";
if (!in_array($suggestion, $vars['theme_hook_suggestions']) && !in_array($suggestion_prefix . $suggestion, $vars['theme_hook_suggestions'])) {
array_unshift($vars['theme_hook_suggestions'], $suggestion_prefix . $suggestion);
}
$suggestion = "{$entity_type}__{$view_mode}";
if (!in_array($suggestion, $vars['theme_hook_suggestions']) && !in_array($suggestion_prefix . $suggestion, $vars['theme_hook_suggestions'])) {
cm_tools_insert_theme_hook_suggestion($vars['theme_hook_suggestions'], "{$entity_type}__{$bundle}", $suggestion_prefix . $suggestion);
}
$suggestion = "{$entity_type}__{$bundle}__{$view_mode}";
if (!in_array($suggestion, $vars['theme_hook_suggestions']) && !in_array($suggestion_prefix . $suggestion, $vars['theme_hook_suggestions'])) {
cm_tools_insert_theme_hook_suggestion($vars['theme_hook_suggestions'], "{$entity_type}__{$view_mode}", $suggestion_prefix . $suggestion);
}
$suggestion = "{$entity_type}__{$id}";
if (!in_array($suggestion, $vars['theme_hook_suggestions']) && !in_array($suggestion_prefix . $suggestion, $vars['theme_hook_suggestions'])) {
$vars['theme_hook_suggestions'][] = $suggestion_prefix . $suggestion;
}
$suggestion = "{$entity_type}__{$id}__{$view_mode}";
if (!in_array($suggestion, $vars['theme_hook_suggestions']) && !in_array($suggestion_prefix . $suggestion, $vars['theme_hook_suggestions'])) {
cm_tools_insert_theme_hook_suggestion($vars['theme_hook_suggestions'], "{$entity_type}__{$id}", $suggestion_prefix . $suggestion);
}
}
// Add a class indicating view mode.
$classes = array();
$classes[] = drupal_clean_css_identifier("{$entity_type}-{$view_mode}");
// Also add entity-bundle-view_mode combo.
$classes[] = drupal_clean_css_identifier("{$entity_type}-{$bundle}-{$view_mode}");
if (isset($vars['classes_array'])) {
$vars['classes_array'] = array_merge($vars['classes_array'], $classes);
}
elseif (isset($vars['attributes_array'])) {
$vars['attributes_array'] = array_merge_recursive($vars['attributes_array'], array('class' => $classes));
}
elseif (isset($vars['classes']) && is_string($vars['classes'])) {
$vars['classes'] .= ' ' . implode(' ', $classes);
}
}
/**
* Implements hook_preprocess_comment().
*/
function cm_tools_preprocess_comment(&$vars, $hook) {
$vars['entity_type'] = 'comment';
if (isset($vars['elements']['#view_mode'])) {
$vars['view_mode'] = $vars['elements']['#view_mode'];
}
cm_tools_preprocess_entity($vars, $hook, '');
}
/**
* Implements hook_preprocess_node().
*/
function cm_tools_preprocess_node(&$vars, $hook) {
$vars['entity_type'] = 'node';
cm_tools_preprocess_entity($vars, $hook, '');
}
/**
* Implements hook_preprocess_taxonomy_term().
*/
function cm_tools_preprocess_taxonomy_term(&$vars, $hook) {
$vars['entity_type'] = 'taxonomy_term';
$vars[$vars['entity_type']] = $vars['term'];
cm_tools_preprocess_entity($vars, $hook, '');
}
/**
* Implements hook_preprocess_taxonomy_vocabulary().
*/
function cm_tools_preprocess_taxonomy_vocabulary(&$vars, $hook) {
$vars['entity_type'] = 'taxonomy_vocabulary';
cm_tools_preprocess_entity($vars, $hook, '');
}
/**
* Implements hook_preprocess_user_profile().
*/
function cm_tools_preprocess_user_profile(&$vars, $hook) {
$vars['entity_type'] = 'user';
$vars['entity_type_theme_name'] = 'user_profile';
if (!isset($vars['view_mode'])) {
$vars['view_mode'] = $vars['elements']['#view_mode'];
}
cm_tools_preprocess_entity($vars, $hook, '');
}
/**
* Implements hook_views_plugins().
*/
function cm_tools_views_plugins() {
module_load_include('inc', 'cm_tools', 'cm_tools.views');
return _cm_tools_views_plugins();
}
/**
* Implements hook_preprocess_page().
*/
function cm_tools_preprocess_page(&$vars) {
// Allow drupal_add_region_content() to work everywhere, not just on the
// maintenance page as per Drupal core.
foreach (drupal_add_region_content() as $region => $contents) {
// Standardize each item to a render array.
foreach ($contents as $k => $content) {
if (!is_array($content)) {
$contents[$k] = array(
'#type' => 'markup',
'#markup' => $content,
);
}
}
// And add in the extra content.
if (isset($vars['page'][$region])) {
if (is_string($vars['page'][$region])) {
$vars['page'][$region] .= drupal_render($contents);
}
else {
$vars['page'][$region] = array_merge($vars['page'][$region], $contents);
}
}
else {
$vars['page'][$region] = $contents;
}
}
}
/**
* Add an external JS file in a non-blocking manner, once per page.
*
* Ideally, use as an #attached callback to be caching-friendly, like so:
*
* $render_array['#attached']['cm_tools_add_async_js'][] = array(<url>, <id>);
*
* @param string $script
* The URL of the script to load (schemeless/protocol relative are
* best, for avoiding mixed http/https content warnings).
* @param string $id
* The id to give the script tag (to prevent multiple loads).
*/
function cm_tools_add_async_js($script = '', $id = '') {
drupal_add_js('!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="' . $script . '";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","' . $id . '");',
array('type' => 'inline', 'scope' => 'footer', 'weight' => 5)
);
}