1
|
<?php
|
2
|
/**
|
3
|
* @file
|
4
|
* List of common helper functions for use in Bootstrap based themes.
|
5
|
*/
|
6
|
|
7
|
define('BOOTSTRAP_VERSION_MAJOR', 3);
|
8
|
define('BOOTSTRAP_VERSION_MINOR', 3);
|
9
|
define('BOOTSTRAP_VERSION_PATCH', 5);
|
10
|
define('BOOTSTRAP_VERSION', BOOTSTRAP_VERSION_MAJOR . '.' . BOOTSTRAP_VERSION_MINOR . '.' . BOOTSTRAP_VERSION_PATCH);
|
11
|
|
12
|
/**
|
13
|
* @defgroup subtheme_helper_functions Helper Functions
|
14
|
* @ingroup subtheme
|
15
|
*
|
16
|
* List of common helper functions for use in Drupal Bootstrap based themes.
|
17
|
*
|
18
|
* @{
|
19
|
*/
|
20
|
|
21
|
/**
|
22
|
* Converts an element description into a tooltip based on certain criteria.
|
23
|
*
|
24
|
* @param array $element
|
25
|
* An element render array, passed by reference.
|
26
|
* @param array $target
|
27
|
* The target element render array the tooltip is to be attached to, passed
|
28
|
* by reference. If not set, it will default to the $element passed.
|
29
|
* @param bool $input_only
|
30
|
* Toggle determining whether or not to only convert input elements.
|
31
|
* @param int $length
|
32
|
* The length of characters to determine if description is "simple".
|
33
|
*/
|
34
|
function bootstrap_element_smart_description(&$element, &$target = NULL, $input_only = TRUE, $length = NULL) {
|
35
|
// Determine if tooltips are enabled.
|
36
|
static $enabled;
|
37
|
if (!isset($enabled)) {
|
38
|
$enabled = bootstrap_setting('tooltip_enabled') && bootstrap_setting('forms_smart_descriptions');
|
39
|
}
|
40
|
|
41
|
// Immediately return if "simple" tooltip descriptions are not enabled.
|
42
|
if (!$enabled) {
|
43
|
return;
|
44
|
}
|
45
|
|
46
|
// Allow a different element to attach the tooltip.
|
47
|
if (!isset($target)) {
|
48
|
$target = &$element;
|
49
|
}
|
50
|
|
51
|
// Retrieve the length limit for smart descriptions.
|
52
|
if (!isset($length)) {
|
53
|
$length = (int) bootstrap_setting('forms_smart_descriptions_limit');
|
54
|
// Disable length checking by setting it to FALSE if empty.
|
55
|
if (empty($length)) {
|
56
|
$length = FALSE;
|
57
|
}
|
58
|
}
|
59
|
|
60
|
// Retrieve the allowed tags for smart descriptions. This is primarily used
|
61
|
// for display purposes only (i.e. non-UI/UX related elements that wouldn't
|
62
|
// require a user to "click", like a link).
|
63
|
$allowed_tags = array_filter(array_unique(array_map('trim', explode(',', bootstrap_setting('forms_smart_descriptions_allowed_tags') . ''))));
|
64
|
|
65
|
// Disable length checking by setting it to FALSE if empty.
|
66
|
if (empty($allowed_tags)) {
|
67
|
$allowed_tags = FALSE;
|
68
|
}
|
69
|
|
70
|
$html = FALSE;
|
71
|
$type = !empty($element['#type']) ? $element['#type'] : FALSE;
|
72
|
|
73
|
// Return if element or target shouldn't have "simple" tooltip descriptions.
|
74
|
if (($input_only && !isset($target['#input']))
|
75
|
// Ignore text_format elements.
|
76
|
// @see https://www.drupal.org/node/2478339
|
77
|
|| $type === 'text_format'
|
78
|
|
79
|
// Ignore if the actual element has no #description set.
|
80
|
|| empty($element['#description'])
|
81
|
|
82
|
// Ignore if the target element already has a "data-toggle" attribute set.
|
83
|
|| !empty($target['#attributes']['data-toggle'])
|
84
|
|
85
|
// Ignore if the target element is #disabled.
|
86
|
|| isset($target['#disabled'])
|
87
|
|
88
|
// Ignore if either the actual element or target element has an explicit
|
89
|
// #smart_description property set to FALSE.
|
90
|
|| (isset($element['#smart_description']) && !$element['#smart_description'])
|
91
|
|| (isset($target['#smart_description']) && !$target['#smart_description'])
|
92
|
|
93
|
// Ignore if the description is not "simple".
|
94
|
|| !_bootstrap_is_simple_string($element['#description'], $length, $allowed_tags, $html)
|
95
|
) {
|
96
|
// Set the both the actual element and the target element
|
97
|
// #smart_description property to FALSE.
|
98
|
$element['#smart_description'] = FALSE;
|
99
|
$target['#smart_description'] = FALSE;
|
100
|
return;
|
101
|
}
|
102
|
|
103
|
// Default property (on the element itself).
|
104
|
$property = 'attributes';
|
105
|
|
106
|
// Add the tooltip to the #label_attributes property for 'checkbox'
|
107
|
// and 'radio' elements.
|
108
|
if ($type === 'checkbox' || $type === 'radio') {
|
109
|
$property = 'label_attributes';
|
110
|
}
|
111
|
// Add the tooltip to the #wrapper_attributes property for 'checkboxes'
|
112
|
// and 'radios' elements.
|
113
|
elseif ($type === 'checkboxes' || $type === 'radios') {
|
114
|
$property = 'wrapper_attributes';
|
115
|
}
|
116
|
// Add the tooltip to the #input_group_attributes property for elements
|
117
|
// that have valid input groups set.
|
118
|
elseif ((!empty($element['#field_prefix']) || !empty($element['#field_suffix'])) && (!empty($element['#input_group']) || !empty($element['#input_group_button']))) {
|
119
|
$property = 'input_group_attributes';
|
120
|
}
|
121
|
|
122
|
// Retrieve the proper attributes array.
|
123
|
$attributes = &_bootstrap_get_attributes($target, $property);
|
124
|
|
125
|
// Set the tooltip attributes.
|
126
|
$attributes['title'] = $allowed_tags !== FALSE ? filter_xss($element['#description'], $allowed_tags) : $element['#description'];
|
127
|
$attributes['data-toggle'] = 'tooltip';
|
128
|
if ($html || $allowed_tags === FALSE) {
|
129
|
$attributes['data-html'] = 'true';
|
130
|
}
|
131
|
|
132
|
// Remove the element description so it isn't (re-)rendered later.
|
133
|
unset($element['#description']);
|
134
|
}
|
135
|
|
136
|
/**
|
137
|
* Retrieves CDN assets for the active provider, if any.
|
138
|
*
|
139
|
* @param string|array $type
|
140
|
* The type of asset to retrieve: "css" or "js", defaults to an array
|
141
|
* array containing both if not set.
|
142
|
* @param string $provider
|
143
|
* The name of a specific CDN provider to use, defaults to the active provider
|
144
|
* set in the theme settings.
|
145
|
* @param string $theme
|
146
|
* The name of a specific theme the settings should be retrieved from,
|
147
|
* defaults to the active theme.
|
148
|
*
|
149
|
* @return array
|
150
|
* If $type is a string or an array with only one (1) item in it, the assets
|
151
|
* are returned as an indexed array of files. Otherwise, an associative array
|
152
|
* is returned keyed by the type.
|
153
|
*/
|
154
|
function bootstrap_get_cdn_assets($type = NULL, $provider = NULL, $theme = NULL) {
|
155
|
$original_type = $type;
|
156
|
$assets = array();
|
157
|
$types = array();
|
158
|
|
159
|
// If no type is set, return all CSS and JS.
|
160
|
if (!isset($type)) {
|
161
|
$types = array('css', 'js');
|
162
|
}
|
163
|
elseif (isset($type)) {
|
164
|
if (!is_array($type)) {
|
165
|
$type = array($type);
|
166
|
}
|
167
|
$types = $type;
|
168
|
}
|
169
|
|
170
|
// Ensure default arrays exist for the requested types.
|
171
|
foreach ($types as $type) {
|
172
|
$assets[$type] = array();
|
173
|
}
|
174
|
|
175
|
// Retrieve the CDN provider from the theme.
|
176
|
if (!isset($provider)) {
|
177
|
$provider = bootstrap_setting('cdn_provider', $theme);
|
178
|
}
|
179
|
|
180
|
// Load the CDN provider data.
|
181
|
bootstrap_include('bootstrap', 'includes/cdn.inc');
|
182
|
if (!empty($provider) && ($data = bootstrap_cdn_provider($provider))) {
|
183
|
// Alter the assets based on provider.
|
184
|
$function = 'bootstrap_bootstrap_cdn_provider_' . $provider . '_assets_alter';
|
185
|
if (function_exists($function)) {
|
186
|
$function($data, $theme);
|
187
|
}
|
188
|
// @todo Use drupal_alter() once CDN is in bootstrap_core companion module.
|
189
|
// drupal_alter('bootstrap_cdn_provider_' . $provider . '_assets', $data, $theme);
|
190
|
|
191
|
// Iterate over each type.
|
192
|
foreach ($types as $type) {
|
193
|
if (variable_get("preprocess_$type", FALSE) && !empty($data['min'][$type])) {
|
194
|
$assets[$type] = $data['min'][$type];
|
195
|
}
|
196
|
elseif (!empty($data[$type])) {
|
197
|
$assets[$type] = $data[$type];
|
198
|
}
|
199
|
}
|
200
|
}
|
201
|
|
202
|
return is_string($original_type) ? $assets[$original_type] : $assets;
|
203
|
}
|
204
|
|
205
|
/**
|
206
|
* Return information from the .info file of a theme (and possible base themes).
|
207
|
*
|
208
|
* @param string $theme_key
|
209
|
* The machine name of the theme.
|
210
|
* @param string $key
|
211
|
* The key name of the item to return from the .info file. This value can
|
212
|
* include "][" to automatically attempt to traverse any arrays.
|
213
|
* @param bool $base_themes
|
214
|
* Recursively search base themes, defaults to TRUE.
|
215
|
*
|
216
|
* @return string|array|false
|
217
|
* A string or array depending on the type of value and if a base theme also
|
218
|
* contains the same $key, FALSE if no $key is found.
|
219
|
*/
|
220
|
function bootstrap_get_theme_info($theme_key = NULL, $key = NULL, $base_themes = TRUE) {
|
221
|
// If no $theme_key is given, use the current theme if we can determine it.
|
222
|
if (!isset($theme_key)) {
|
223
|
$theme_key = !empty($GLOBALS['theme_key']) ? $GLOBALS['theme_key'] : FALSE;
|
224
|
}
|
225
|
if ($theme_key) {
|
226
|
$themes = list_themes();
|
227
|
if (!empty($themes[$theme_key])) {
|
228
|
$theme = $themes[$theme_key];
|
229
|
// If a key name was specified, return just that array.
|
230
|
if ($key) {
|
231
|
$value = FALSE;
|
232
|
// Recursively add base theme values.
|
233
|
if ($base_themes && isset($theme->base_themes)) {
|
234
|
foreach (array_keys($theme->base_themes) as $base_theme) {
|
235
|
$value = bootstrap_get_theme_info($base_theme, $key);
|
236
|
}
|
237
|
}
|
238
|
if (!empty($themes[$theme_key])) {
|
239
|
$info = $themes[$theme_key]->info;
|
240
|
// Allow array traversal.
|
241
|
$keys = explode('][', $key);
|
242
|
foreach ($keys as $parent) {
|
243
|
if (isset($info[$parent])) {
|
244
|
$info = $info[$parent];
|
245
|
}
|
246
|
else {
|
247
|
$info = FALSE;
|
248
|
}
|
249
|
}
|
250
|
if (is_array($value)) {
|
251
|
if (!empty($info)) {
|
252
|
if (!is_array($info)) {
|
253
|
$info = array($info);
|
254
|
}
|
255
|
$value = drupal_array_merge_deep($value, $info);
|
256
|
}
|
257
|
}
|
258
|
else {
|
259
|
if (!empty($info)) {
|
260
|
if (empty($value)) {
|
261
|
$value = $info;
|
262
|
}
|
263
|
else {
|
264
|
if (!is_array($value)) {
|
265
|
$value = array($value);
|
266
|
}
|
267
|
if (!is_array($info)) {
|
268
|
$info = array($info);
|
269
|
}
|
270
|
$value = drupal_array_merge_deep($value, $info);
|
271
|
}
|
272
|
}
|
273
|
}
|
274
|
}
|
275
|
return $value;
|
276
|
}
|
277
|
// If no info $key was specified, just return the entire info array.
|
278
|
return $theme->info;
|
279
|
}
|
280
|
}
|
281
|
return FALSE;
|
282
|
}
|
283
|
|
284
|
/**
|
285
|
* Includes a theme file.
|
286
|
*
|
287
|
* @param string $theme
|
288
|
* Name of the theme to use for base path.
|
289
|
* @param string $path
|
290
|
* Path relative to $theme.
|
291
|
*/
|
292
|
function bootstrap_include($theme, $path) {
|
293
|
static $themes = array();
|
294
|
if (!isset($themes[$theme])) {
|
295
|
$themes[$theme] = drupal_get_path('theme', $theme);
|
296
|
}
|
297
|
if ($themes[$theme] && ($file = DRUPAL_ROOT . '/' . $themes[$theme] . '/' . $path) && file_exists($file)) {
|
298
|
include_once $file;
|
299
|
}
|
300
|
}
|
301
|
|
302
|
/**
|
303
|
* Retrieves a setting for the current theme or for a given theme.
|
304
|
*
|
305
|
* This is a wrapper for theme_get_setting(), ensuring to use deprecated
|
306
|
* setting values instead.
|
307
|
*
|
308
|
* @param string $name
|
309
|
* The name of the setting to be retrieved.
|
310
|
* @param string $theme
|
311
|
* The name of a given theme; defaults to the currently active theme.
|
312
|
* @param string $prefix
|
313
|
* The prefix used on the $name of the setting, this will be appended with
|
314
|
* "_" automatically if set.
|
315
|
*
|
316
|
* @return mixed
|
317
|
* The value of the requested setting, NULL if the setting does not exist.
|
318
|
*
|
319
|
* @see theme_get_setting()
|
320
|
*
|
321
|
* @todo Refactor in 7.x-4.x and get rid of the deprecated settings.
|
322
|
*/
|
323
|
function bootstrap_setting($name, $theme = NULL, $prefix = 'bootstrap') {
|
324
|
$prefix = !empty($prefix) ? $prefix . '_' : '';
|
325
|
$setting = theme_get_setting($prefix . $name, $theme);
|
326
|
switch ($prefix . $name) {
|
327
|
case 'bootstrap_cdn_provider':
|
328
|
$deprecated = theme_get_setting('bootstrap_cdn', $theme);
|
329
|
if (isset($deprecated)) {
|
330
|
$setting = empty($deprecated) ? '' : 'jsdelivr';
|
331
|
}
|
332
|
break;
|
333
|
|
334
|
case 'bootstrap_cdn_jsdelivr_version':
|
335
|
$deprecated = theme_get_setting('bootstrap_cdn', $theme);
|
336
|
if (isset($deprecated)) {
|
337
|
$setting = empty($deprecated) ? BOOTSTRAP_VERSION : $deprecated;
|
338
|
}
|
339
|
break;
|
340
|
|
341
|
case 'bootstrap_cdn_jsdelivr_theme':
|
342
|
$deprecated = theme_get_setting('bootstrap_bootswatch', $theme);
|
343
|
if (isset($deprecated)) {
|
344
|
$setting = empty($deprecated) ? 'bootstrap' : $deprecated;
|
345
|
}
|
346
|
break;
|
347
|
|
348
|
case 'bootstrap_forms_smart_descriptions':
|
349
|
$deprecated = theme_get_setting('bootstrap_tooltip_descriptions', $theme);
|
350
|
if (isset($deprecated)) {
|
351
|
$setting = (int) !empty($deprecated);
|
352
|
}
|
353
|
break;
|
354
|
|
355
|
case 'bootstrap_forms_smart_descriptions_limit':
|
356
|
$deprecated = theme_get_setting('bootstrap_tooltip_descriptions_length', $theme);
|
357
|
if (isset($deprecated)) {
|
358
|
$setting = (int) !empty($deprecated);
|
359
|
}
|
360
|
break;
|
361
|
|
362
|
}
|
363
|
return $setting;
|
364
|
}
|
365
|
|
366
|
/**
|
367
|
* Retrieves an element's "attributes" array.
|
368
|
*
|
369
|
* @param array $element
|
370
|
* The individual renderable array element. It is possible to also pass the
|
371
|
* $variables parameter in [pre]process functions and it will logically
|
372
|
* determine the correct path to that particular theme hook's attribute array.
|
373
|
* Passed by reference.
|
374
|
* @param string $property
|
375
|
* Determines which attributes array to retrieve. By default, this is the
|
376
|
* normal attributes, but can be "wrapper_attributes" or
|
377
|
* "input_group_attributes".
|
378
|
*
|
379
|
* @return array
|
380
|
* The attributes array. Passed by reference.
|
381
|
*/
|
382
|
function &_bootstrap_get_attributes(&$element, $property = 'attributes') {
|
383
|
// Attempt to retrieve a renderable element attributes first.
|
384
|
if (
|
385
|
isset($element['#type']) ||
|
386
|
isset($element['#theme']) ||
|
387
|
isset($element['#pre_render']) ||
|
388
|
isset($element['#markup']) ||
|
389
|
isset($element['#theme_wrappers']) ||
|
390
|
isset($element["#$property"])
|
391
|
) {
|
392
|
if (!isset($element["#$property"])) {
|
393
|
$element["#$property"] = array();
|
394
|
}
|
395
|
return $element["#$property"];
|
396
|
}
|
397
|
// Treat $element as if it were a [pre]process function $variables parameter
|
398
|
// and look for a renderable "element".
|
399
|
elseif (isset($element['element'])) {
|
400
|
if (!isset($element['element']["#$property"])) {
|
401
|
$element['element']["#$property"] = array();
|
402
|
}
|
403
|
return $element['element']["#$property"];
|
404
|
}
|
405
|
|
406
|
// If all else fails, create (if needed) a default "attributes" array. This
|
407
|
// will, at the very least, either work or cause an error that can be tracked.
|
408
|
if (!isset($element[$property])) {
|
409
|
$element[$property] = array();
|
410
|
}
|
411
|
|
412
|
return $element[$property];
|
413
|
}
|
414
|
|
415
|
/**
|
416
|
* Retrieves an element's "class" array.
|
417
|
*
|
418
|
* @param array $element
|
419
|
* The individual renderable array element. It is possible to also pass the
|
420
|
* $variables parameter in [pre]process functions and it will logically
|
421
|
* determine the correct path to that particular theme hook's classes array.
|
422
|
* Passed by reference.
|
423
|
* @param string $property
|
424
|
* Determines which attributes array to retrieve. By default, this is the
|
425
|
* normal attributes, but can be "wrapper_attributes" or
|
426
|
* "input_group_attributes".
|
427
|
*
|
428
|
* @return array
|
429
|
* The classes array. Passed by reference.
|
430
|
*/
|
431
|
function &_bootstrap_get_classes(&$element, $property = 'attributes') {
|
432
|
$attributes = &_bootstrap_get_attributes($element, $property);
|
433
|
|
434
|
if (!isset($attributes['class'])) {
|
435
|
$attributes['class'] = array();
|
436
|
}
|
437
|
// Contrib modules have a very bad habit of frequently adding classes as
|
438
|
// strings, convert them to a proper array.
|
439
|
// @see https://www.drupal.org/node/2269653
|
440
|
elseif (!is_array($attributes['class'])) {
|
441
|
$attributes['class'] = explode(' ', $attributes['class']);
|
442
|
}
|
443
|
|
444
|
// Ensure classes are not duplicated.
|
445
|
$attributes['class'] = array_unique($attributes['class']);
|
446
|
return $attributes['class'];
|
447
|
}
|
448
|
|
449
|
/**
|
450
|
* Adds a class to an element's render array.
|
451
|
*
|
452
|
* @param string|array $class
|
453
|
* An individual class or an array of classes to add.
|
454
|
* @param array $element
|
455
|
* The individual renderable array element. It is possible to also pass the
|
456
|
* $variables parameter in [pre]process functions and it will logically
|
457
|
* determine the correct path to that particular theme hook's classes array.
|
458
|
* Passed by reference.
|
459
|
* @param string $property
|
460
|
* Determines which attributes array to retrieve. By default, this is the
|
461
|
* normal attributes, but can be "wrapper_attributes" or
|
462
|
* "input_group_attributes".
|
463
|
*/
|
464
|
function _bootstrap_add_class($class, &$element, $property = 'attributes') {
|
465
|
// Retrieve the element's classes.
|
466
|
$classes = &_bootstrap_get_classes($element, $property);
|
467
|
|
468
|
// Convert the class to an array.
|
469
|
if (!is_array($class)) {
|
470
|
$class = array($class);
|
471
|
}
|
472
|
|
473
|
// Iterate over all classes to add.
|
474
|
foreach ($class as $_class) {
|
475
|
// Ensure the class to add does not yet already exist.
|
476
|
if (!in_array($_class, $classes)) {
|
477
|
$classes[] = $_class;
|
478
|
}
|
479
|
}
|
480
|
}
|
481
|
|
482
|
/**
|
483
|
* Removes a class from an element's render array.
|
484
|
*
|
485
|
* @param string|array $class
|
486
|
* An individual class or an array of classes to remove.
|
487
|
* @param array $element
|
488
|
* The individual renderable array element. It is possible to also pass the
|
489
|
* $variables parameter in [pre]process functions and it will logically
|
490
|
* determine the correct path to that particular theme hook's classes array.
|
491
|
* Passed by reference.
|
492
|
* @param string $property
|
493
|
* Determines which attributes array to retrieve. By default, this is the
|
494
|
* normal attributes, but can be "wrapper_attributes" or
|
495
|
* "input_group_attributes".
|
496
|
*/
|
497
|
function _bootstrap_remove_class($class, &$element, $property = 'attributes') {
|
498
|
// Retrieve the element's classes.
|
499
|
$classes = &_bootstrap_get_classes($element, $property);
|
500
|
|
501
|
// Convert the class to an array.
|
502
|
if (!is_array($class)) {
|
503
|
$class = array($class);
|
504
|
}
|
505
|
|
506
|
// Iterate over all classes to add.
|
507
|
foreach ($class as $_class) {
|
508
|
$key = array_search($_class, $classes);
|
509
|
if ($key !== FALSE) {
|
510
|
unset($classes[$key]);
|
511
|
}
|
512
|
}
|
513
|
}
|
514
|
|
515
|
/**
|
516
|
* Returns a list of base themes for active or provided theme.
|
517
|
*
|
518
|
* @param string $theme_key
|
519
|
* The machine name of the theme to check, if not set the active theme name
|
520
|
* will be used.
|
521
|
* @param bool $include_theme_key
|
522
|
* Whether to append the returned list with $theme_key.
|
523
|
*
|
524
|
* @return array
|
525
|
* An indexed array of base themes.
|
526
|
*/
|
527
|
function _bootstrap_get_base_themes($theme_key = NULL, $include_theme_key = FALSE) {
|
528
|
static $themes;
|
529
|
if (!isset($theme_key)) {
|
530
|
$theme_key = $GLOBALS['theme_key'];
|
531
|
}
|
532
|
if (!isset($themes[$theme_key])) {
|
533
|
$themes[$theme_key] = array_unique(array_filter((array) bootstrap_get_theme_info($theme_key, 'base theme')));
|
534
|
}
|
535
|
if ($include_theme_key) {
|
536
|
$themes[$theme_key][] = $theme_key;
|
537
|
}
|
538
|
return $themes[$theme_key];
|
539
|
}
|
540
|
|
541
|
/**
|
542
|
* Wrapper for the core file_scan_directory() function.
|
543
|
*
|
544
|
* Finds all files that match a given mask in a given directory and then caches
|
545
|
* the results. A general site cache clear will force new scans to be initiated
|
546
|
* for already cached directories.
|
547
|
*
|
548
|
* @param string $dir
|
549
|
* The base directory or URI to scan, without trailing slash.
|
550
|
* @param string $mask
|
551
|
* The preg_match() regular expression of the files to find.
|
552
|
* @param array $options
|
553
|
* Additional options to pass to file_scan_directory().
|
554
|
*
|
555
|
* @return array
|
556
|
* An associative array (keyed on the chosen key) of objects with 'uri',
|
557
|
* 'filename', and 'name' members corresponding to the matching files.
|
558
|
*
|
559
|
* @see file_scan_directory()
|
560
|
*/
|
561
|
function _bootstrap_file_scan_directory($dir, $mask, array $options = array()) {
|
562
|
$files = &drupal_static(__FUNCTION__, array());
|
563
|
|
564
|
// Generate a unique cache identifier for all parameters passed as a change
|
565
|
// in any of them would return different results.
|
566
|
$cid = 'theme_registry:bootstrap:files:' . drupal_hash_base64(serialize(func_get_args()));
|
567
|
|
568
|
// Load from DB cache or scan filesystem if files are not statically cached.
|
569
|
if (!isset($files[$cid])) {
|
570
|
if (($cache = cache_get($cid)) && isset($cache->data)) {
|
571
|
$files[$cid] = $cache->data;
|
572
|
}
|
573
|
else {
|
574
|
$files[$cid] = file_scan_directory($dir, $mask, $options);
|
575
|
cache_set($cid, $files[$cid]);
|
576
|
}
|
577
|
}
|
578
|
|
579
|
return $files[$cid];
|
580
|
}
|
581
|
|
582
|
/**
|
583
|
* Filters HTML to prevent cross-site-scripting (XSS) vulnerabilities.
|
584
|
*
|
585
|
* Very similar to core's filter_xss(). It does, however, include the addition
|
586
|
* of the "span", "div" and "i" elements which are commonly used in Bootstrap.
|
587
|
*
|
588
|
* @param string $string
|
589
|
* The string with raw HTML in it. It will be stripped of everything that can
|
590
|
* cause an XSS attack.
|
591
|
* @param array $allowed_tags
|
592
|
* An array of allowed tags.
|
593
|
*
|
594
|
* @return string
|
595
|
* An XSS safe version of $string, or an empty string if $string is not
|
596
|
* valid UTF-8.
|
597
|
*
|
598
|
* @see filter_xss()
|
599
|
* @see filter_xss_admin()
|
600
|
*
|
601
|
* @deprecated Use filter_xss() or filter_xss_admin() instead.
|
602
|
* Will be removed in a future release.
|
603
|
*/
|
604
|
function _bootstrap_filter_xss($string, array $allowed_tags = NULL) {
|
605
|
if (is_null($allowed_tags)) {
|
606
|
$allowed_tags = array(
|
607
|
// Inline elements.
|
608
|
'a',
|
609
|
'cite',
|
610
|
'em',
|
611
|
'i',
|
612
|
'span',
|
613
|
'strong',
|
614
|
|
615
|
// Block elements.
|
616
|
'blockquote',
|
617
|
'code',
|
618
|
'div',
|
619
|
'ul',
|
620
|
'ol',
|
621
|
'li',
|
622
|
'dl',
|
623
|
'dt',
|
624
|
'dd',
|
625
|
);
|
626
|
}
|
627
|
return filter_xss($string, $allowed_tags);
|
628
|
}
|
629
|
|
630
|
/**
|
631
|
* Returns a list of available Bootstrap Glyphicons.
|
632
|
*
|
633
|
* @param string $version
|
634
|
* The specific version of glyphicons to return. If not set, the latest
|
635
|
* BOOTSTRAP_VERSION will be used.
|
636
|
*
|
637
|
* @return array
|
638
|
* An associative array of icons keyed by their classes.
|
639
|
*/
|
640
|
function _bootstrap_glyphicons($version = NULL) {
|
641
|
static $versions;
|
642
|
if (!isset($versions)) {
|
643
|
$versions = array();
|
644
|
$versions['3.0.0'] = array(
|
645
|
// Class => Name.
|
646
|
'glyphicon-adjust' => 'adjust',
|
647
|
'glyphicon-align-center' => 'align-center',
|
648
|
'glyphicon-align-justify' => 'align-justify',
|
649
|
'glyphicon-align-left' => 'align-left',
|
650
|
'glyphicon-align-right' => 'align-right',
|
651
|
'glyphicon-arrow-down' => 'arrow-down',
|
652
|
'glyphicon-arrow-left' => 'arrow-left',
|
653
|
'glyphicon-arrow-right' => 'arrow-right',
|
654
|
'glyphicon-arrow-up' => 'arrow-up',
|
655
|
'glyphicon-asterisk' => 'asterisk',
|
656
|
'glyphicon-backward' => 'backward',
|
657
|
'glyphicon-ban-circle' => 'ban-circle',
|
658
|
'glyphicon-barcode' => 'barcode',
|
659
|
'glyphicon-bell' => 'bell',
|
660
|
'glyphicon-bold' => 'bold',
|
661
|
'glyphicon-book' => 'book',
|
662
|
'glyphicon-bookmark' => 'bookmark',
|
663
|
'glyphicon-briefcase' => 'briefcase',
|
664
|
'glyphicon-bullhorn' => 'bullhorn',
|
665
|
'glyphicon-calendar' => 'calendar',
|
666
|
'glyphicon-camera' => 'camera',
|
667
|
'glyphicon-certificate' => 'certificate',
|
668
|
'glyphicon-check' => 'check',
|
669
|
'glyphicon-chevron-down' => 'chevron-down',
|
670
|
'glyphicon-chevron-left' => 'chevron-left',
|
671
|
'glyphicon-chevron-right' => 'chevron-right',
|
672
|
'glyphicon-chevron-up' => 'chevron-up',
|
673
|
'glyphicon-circle-arrow-down' => 'circle-arrow-down',
|
674
|
'glyphicon-circle-arrow-left' => 'circle-arrow-left',
|
675
|
'glyphicon-circle-arrow-right' => 'circle-arrow-right',
|
676
|
'glyphicon-circle-arrow-up' => 'circle-arrow-up',
|
677
|
'glyphicon-cloud' => 'cloud',
|
678
|
'glyphicon-cloud-download' => 'cloud-download',
|
679
|
'glyphicon-cloud-upload' => 'cloud-upload',
|
680
|
'glyphicon-cog' => 'cog',
|
681
|
'glyphicon-collapse-down' => 'collapse-down',
|
682
|
'glyphicon-collapse-up' => 'collapse-up',
|
683
|
'glyphicon-comment' => 'comment',
|
684
|
'glyphicon-compressed' => 'compressed',
|
685
|
'glyphicon-copyright-mark' => 'copyright-mark',
|
686
|
'glyphicon-credit-card' => 'credit-card',
|
687
|
'glyphicon-cutlery' => 'cutlery',
|
688
|
'glyphicon-dashboard' => 'dashboard',
|
689
|
'glyphicon-download' => 'download',
|
690
|
'glyphicon-download-alt' => 'download-alt',
|
691
|
'glyphicon-earphone' => 'earphone',
|
692
|
'glyphicon-edit' => 'edit',
|
693
|
'glyphicon-eject' => 'eject',
|
694
|
'glyphicon-envelope' => 'envelope',
|
695
|
'glyphicon-euro' => 'euro',
|
696
|
'glyphicon-exclamation-sign' => 'exclamation-sign',
|
697
|
'glyphicon-expand' => 'expand',
|
698
|
'glyphicon-export' => 'export',
|
699
|
'glyphicon-eye-close' => 'eye-close',
|
700
|
'glyphicon-eye-open' => 'eye-open',
|
701
|
'glyphicon-facetime-video' => 'facetime-video',
|
702
|
'glyphicon-fast-backward' => 'fast-backward',
|
703
|
'glyphicon-fast-forward' => 'fast-forward',
|
704
|
'glyphicon-file' => 'file',
|
705
|
'glyphicon-film' => 'film',
|
706
|
'glyphicon-filter' => 'filter',
|
707
|
'glyphicon-fire' => 'fire',
|
708
|
'glyphicon-flag' => 'flag',
|
709
|
'glyphicon-flash' => 'flash',
|
710
|
'glyphicon-floppy-disk' => 'floppy-disk',
|
711
|
'glyphicon-floppy-open' => 'floppy-open',
|
712
|
'glyphicon-floppy-remove' => 'floppy-remove',
|
713
|
'glyphicon-floppy-save' => 'floppy-save',
|
714
|
'glyphicon-floppy-saved' => 'floppy-saved',
|
715
|
'glyphicon-folder-close' => 'folder-close',
|
716
|
'glyphicon-folder-open' => 'folder-open',
|
717
|
'glyphicon-font' => 'font',
|
718
|
'glyphicon-forward' => 'forward',
|
719
|
'glyphicon-fullscreen' => 'fullscreen',
|
720
|
'glyphicon-gbp' => 'gbp',
|
721
|
'glyphicon-gift' => 'gift',
|
722
|
'glyphicon-glass' => 'glass',
|
723
|
'glyphicon-globe' => 'globe',
|
724
|
'glyphicon-hand-down' => 'hand-down',
|
725
|
'glyphicon-hand-left' => 'hand-left',
|
726
|
'glyphicon-hand-right' => 'hand-right',
|
727
|
'glyphicon-hand-up' => 'hand-up',
|
728
|
'glyphicon-hd-video' => 'hd-video',
|
729
|
'glyphicon-hdd' => 'hdd',
|
730
|
'glyphicon-header' => 'header',
|
731
|
'glyphicon-headphones' => 'headphones',
|
732
|
'glyphicon-heart' => 'heart',
|
733
|
'glyphicon-heart-empty' => 'heart-empty',
|
734
|
'glyphicon-home' => 'home',
|
735
|
'glyphicon-import' => 'import',
|
736
|
'glyphicon-inbox' => 'inbox',
|
737
|
'glyphicon-indent-left' => 'indent-left',
|
738
|
'glyphicon-indent-right' => 'indent-right',
|
739
|
'glyphicon-info-sign' => 'info-sign',
|
740
|
'glyphicon-italic' => 'italic',
|
741
|
'glyphicon-leaf' => 'leaf',
|
742
|
'glyphicon-link' => 'link',
|
743
|
'glyphicon-list' => 'list',
|
744
|
'glyphicon-list-alt' => 'list-alt',
|
745
|
'glyphicon-lock' => 'lock',
|
746
|
'glyphicon-log-in' => 'log-in',
|
747
|
'glyphicon-log-out' => 'log-out',
|
748
|
'glyphicon-magnet' => 'magnet',
|
749
|
'glyphicon-map-marker' => 'map-marker',
|
750
|
'glyphicon-minus' => 'minus',
|
751
|
'glyphicon-minus-sign' => 'minus-sign',
|
752
|
'glyphicon-move' => 'move',
|
753
|
'glyphicon-music' => 'music',
|
754
|
'glyphicon-new-window' => 'new-window',
|
755
|
'glyphicon-off' => 'off',
|
756
|
'glyphicon-ok' => 'ok',
|
757
|
'glyphicon-ok-circle' => 'ok-circle',
|
758
|
'glyphicon-ok-sign' => 'ok-sign',
|
759
|
'glyphicon-open' => 'open',
|
760
|
'glyphicon-paperclip' => 'paperclip',
|
761
|
'glyphicon-pause' => 'pause',
|
762
|
'glyphicon-pencil' => 'pencil',
|
763
|
'glyphicon-phone' => 'phone',
|
764
|
'glyphicon-phone-alt' => 'phone-alt',
|
765
|
'glyphicon-picture' => 'picture',
|
766
|
'glyphicon-plane' => 'plane',
|
767
|
'glyphicon-play' => 'play',
|
768
|
'glyphicon-play-circle' => 'play-circle',
|
769
|
'glyphicon-plus' => 'plus',
|
770
|
'glyphicon-plus-sign' => 'plus-sign',
|
771
|
'glyphicon-print' => 'print',
|
772
|
'glyphicon-pushpin' => 'pushpin',
|
773
|
'glyphicon-qrcode' => 'qrcode',
|
774
|
'glyphicon-question-sign' => 'question-sign',
|
775
|
'glyphicon-random' => 'random',
|
776
|
'glyphicon-record' => 'record',
|
777
|
'glyphicon-refresh' => 'refresh',
|
778
|
'glyphicon-registration-mark' => 'registration-mark',
|
779
|
'glyphicon-remove' => 'remove',
|
780
|
'glyphicon-remove-circle' => 'remove-circle',
|
781
|
'glyphicon-remove-sign' => 'remove-sign',
|
782
|
'glyphicon-repeat' => 'repeat',
|
783
|
'glyphicon-resize-full' => 'resize-full',
|
784
|
'glyphicon-resize-horizontal' => 'resize-horizontal',
|
785
|
'glyphicon-resize-small' => 'resize-small',
|
786
|
'glyphicon-resize-vertical' => 'resize-vertical',
|
787
|
'glyphicon-retweet' => 'retweet',
|
788
|
'glyphicon-road' => 'road',
|
789
|
'glyphicon-save' => 'save',
|
790
|
'glyphicon-saved' => 'saved',
|
791
|
'glyphicon-screenshot' => 'screenshot',
|
792
|
'glyphicon-sd-video' => 'sd-video',
|
793
|
'glyphicon-search' => 'search',
|
794
|
'glyphicon-send' => 'send',
|
795
|
'glyphicon-share' => 'share',
|
796
|
'glyphicon-share-alt' => 'share-alt',
|
797
|
'glyphicon-shopping-cart' => 'shopping-cart',
|
798
|
'glyphicon-signal' => 'signal',
|
799
|
'glyphicon-sort' => 'sort',
|
800
|
'glyphicon-sort-by-alphabet' => 'sort-by-alphabet',
|
801
|
'glyphicon-sort-by-alphabet-alt' => 'sort-by-alphabet-alt',
|
802
|
'glyphicon-sort-by-attributes' => 'sort-by-attributes',
|
803
|
'glyphicon-sort-by-attributes-alt' => 'sort-by-attributes-alt',
|
804
|
'glyphicon-sort-by-order' => 'sort-by-order',
|
805
|
'glyphicon-sort-by-order-alt' => 'sort-by-order-alt',
|
806
|
'glyphicon-sound-5-1' => 'sound-5-1',
|
807
|
'glyphicon-sound-6-1' => 'sound-6-1',
|
808
|
'glyphicon-sound-7-1' => 'sound-7-1',
|
809
|
'glyphicon-sound-dolby' => 'sound-dolby',
|
810
|
'glyphicon-sound-stereo' => 'sound-stereo',
|
811
|
'glyphicon-star' => 'star',
|
812
|
'glyphicon-star-empty' => 'star-empty',
|
813
|
'glyphicon-stats' => 'stats',
|
814
|
'glyphicon-step-backward' => 'step-backward',
|
815
|
'glyphicon-step-forward' => 'step-forward',
|
816
|
'glyphicon-stop' => 'stop',
|
817
|
'glyphicon-subtitles' => 'subtitles',
|
818
|
'glyphicon-tag' => 'tag',
|
819
|
'glyphicon-tags' => 'tags',
|
820
|
'glyphicon-tasks' => 'tasks',
|
821
|
'glyphicon-text-height' => 'text-height',
|
822
|
'glyphicon-text-width' => 'text-width',
|
823
|
'glyphicon-th' => 'th',
|
824
|
'glyphicon-th-large' => 'th-large',
|
825
|
'glyphicon-th-list' => 'th-list',
|
826
|
'glyphicon-thumbs-down' => 'thumbs-down',
|
827
|
'glyphicon-thumbs-up' => 'thumbs-up',
|
828
|
'glyphicon-time' => 'time',
|
829
|
'glyphicon-tint' => 'tint',
|
830
|
'glyphicon-tower' => 'tower',
|
831
|
'glyphicon-transfer' => 'transfer',
|
832
|
'glyphicon-trash' => 'trash',
|
833
|
'glyphicon-tree-conifer' => 'tree-conifer',
|
834
|
'glyphicon-tree-deciduous' => 'tree-deciduous',
|
835
|
'glyphicon-unchecked' => 'unchecked',
|
836
|
'glyphicon-upload' => 'upload',
|
837
|
'glyphicon-usd' => 'usd',
|
838
|
'glyphicon-user' => 'user',
|
839
|
'glyphicon-volume-down' => 'volume-down',
|
840
|
'glyphicon-volume-off' => 'volume-off',
|
841
|
'glyphicon-volume-up' => 'volume-up',
|
842
|
'glyphicon-warning-sign' => 'warning-sign',
|
843
|
'glyphicon-wrench' => 'wrench',
|
844
|
'glyphicon-zoom-in' => 'zoom-in',
|
845
|
'glyphicon-zoom-out' => 'zoom-out',
|
846
|
);
|
847
|
$versions['3.0.1'] = $versions['3.0.0'];
|
848
|
$versions['3.0.2'] = $versions['3.0.1'];
|
849
|
$versions['3.0.3'] = $versions['3.0.2'];
|
850
|
$versions['3.1.0'] = $versions['3.0.3'];
|
851
|
$versions['3.1.1'] = $versions['3.1.0'];
|
852
|
$versions['3.2.0'] = $versions['3.1.1'];
|
853
|
$versions['3.3.0'] = array_merge($versions['3.2.0'], array(
|
854
|
'glyphicon-eur' => 'eur',
|
855
|
));
|
856
|
$versions['3.3.1'] = $versions['3.3.0'];
|
857
|
$versions['3.3.2'] = array_merge($versions['3.3.1'], array(
|
858
|
'glyphicon-alert' => 'alert',
|
859
|
'glyphicon-apple' => 'apple',
|
860
|
'glyphicon-baby-formula' => 'baby-formula',
|
861
|
'glyphicon-bed' => 'bed',
|
862
|
'glyphicon-bishop' => 'bishop',
|
863
|
'glyphicon-bitcoin' => 'bitcoin',
|
864
|
'glyphicon-blackboard' => 'blackboard',
|
865
|
'glyphicon-cd' => 'cd',
|
866
|
'glyphicon-console' => 'console',
|
867
|
'glyphicon-copy' => 'copy',
|
868
|
'glyphicon-duplicate' => 'duplicate',
|
869
|
'glyphicon-education' => 'education',
|
870
|
'glyphicon-equalizer' => 'equalizer',
|
871
|
'glyphicon-erase' => 'erase',
|
872
|
'glyphicon-grain' => 'grain',
|
873
|
'glyphicon-hourglass' => 'hourglass',
|
874
|
'glyphicon-ice-lolly' => 'ice-lolly',
|
875
|
'glyphicon-ice-lolly-tasted' => 'ice-lolly-tasted',
|
876
|
'glyphicon-king' => 'king',
|
877
|
'glyphicon-knight' => 'knight',
|
878
|
'glyphicon-lamp' => 'lamp',
|
879
|
'glyphicon-level-up' => 'level-up',
|
880
|
'glyphicon-menu-down' => 'menu-down',
|
881
|
'glyphicon-menu-hamburger' => 'menu-hamburger',
|
882
|
'glyphicon-menu-left' => 'menu-left',
|
883
|
'glyphicon-menu-right' => 'menu-right',
|
884
|
'glyphicon-menu-up' => 'menu-up',
|
885
|
'glyphicon-modal-window' => 'modal-window',
|
886
|
'glyphicon-object-align-bottom' => 'object-align-bottom',
|
887
|
'glyphicon-object-align-horizontal' => 'object-align-horizontal',
|
888
|
'glyphicon-object-align-left' => 'object-align-left',
|
889
|
'glyphicon-object-align-right' => 'object-align-right',
|
890
|
'glyphicon-object-align-top' => 'object-align-top',
|
891
|
'glyphicon-object-align-vertical' => 'object-align-vertical',
|
892
|
'glyphicon-oil' => 'oil',
|
893
|
'glyphicon-open-file' => 'open-file',
|
894
|
'glyphicon-option-horizontal' => 'option-horizontal',
|
895
|
'glyphicon-option-vertical' => 'option-vertical',
|
896
|
'glyphicon-paste' => 'paste',
|
897
|
'glyphicon-pawn' => 'pawn',
|
898
|
'glyphicon-piggy-bank' => 'piggy-bank',
|
899
|
'glyphicon-queen' => 'queen',
|
900
|
'glyphicon-ruble' => 'ruble',
|
901
|
'glyphicon-save-file' => 'save-file',
|
902
|
'glyphicon-scale' => 'scale',
|
903
|
'glyphicon-scissors' => 'scissors',
|
904
|
'glyphicon-subscript' => 'subscript',
|
905
|
'glyphicon-sunglasses' => 'sunglasses',
|
906
|
'glyphicon-superscript' => 'superscript',
|
907
|
'glyphicon-tent' => 'tent',
|
908
|
'glyphicon-text-background' => 'text-background',
|
909
|
'glyphicon-text-color' => 'text-color',
|
910
|
'glyphicon-text-size' => 'text-size',
|
911
|
'glyphicon-triangle-bottom' => 'triangle-bottom',
|
912
|
'glyphicon-triangle-left' => 'triangle-left',
|
913
|
'glyphicon-triangle-right' => 'triangle-right',
|
914
|
'glyphicon-triangle-top' => 'triangle-top',
|
915
|
'glyphicon-yen' => 'yen',
|
916
|
));
|
917
|
$versions['3.3.4'] = array_merge($versions['3.3.2'], array(
|
918
|
'glyphicon-btc' => 'btc',
|
919
|
'glyphicon-jpy' => 'jpy',
|
920
|
'glyphicon-rub' => 'rub',
|
921
|
'glyphicon-xbt' => 'xbt',
|
922
|
));
|
923
|
$versions['3.3.5'] = $versions['3.3.4'];
|
924
|
$versions['3.3.6'] = $versions['3.3.5'];
|
925
|
$versions['3.3.7'] = $versions['3.3.6'];
|
926
|
}
|
927
|
|
928
|
// Return a specific versions icon set.
|
929
|
if (isset($version) && isset($versions[$version])) {
|
930
|
return $versions[$version];
|
931
|
}
|
932
|
|
933
|
// Return the latest version.
|
934
|
return $versions[BOOTSTRAP_VERSION];
|
935
|
}
|
936
|
|
937
|
/**
|
938
|
* Returns a specific Bootstrap Glyphicon as rendered HTML markup.
|
939
|
*
|
940
|
* @param string $name
|
941
|
* The icon name, minus the "glyphicon-" prefix.
|
942
|
* @param string $default
|
943
|
* (Optional) The default value to return.
|
944
|
* @param array $attributes
|
945
|
* (Optional) Additional attributes to merge onto the icon.
|
946
|
*
|
947
|
* @return string
|
948
|
* The HTML markup containing the icon defined by $name, $default value if
|
949
|
* icon does not exist or returns empty output for whatever reason.
|
950
|
*/
|
951
|
function _bootstrap_icon($name, $default = NULL, array $attributes = array()) {
|
952
|
$icon = _bootstrap_glyphicon($name, $default, $attributes);
|
953
|
return render($icon);
|
954
|
}
|
955
|
|
956
|
/**
|
957
|
* Returns a specific Bootstrap Glyphicon as a render array.
|
958
|
*
|
959
|
* Note: This function was added to keep BC with the former _bootstrap_icon()
|
960
|
* implementation since it didn't return a render array. It is basically a
|
961
|
* backport of 8.x-3.x code so the added $attributes parameter can be more
|
962
|
* easily dealt with.
|
963
|
*
|
964
|
* @see https://www.drupal.org/project/bootstrap/issues/2844885
|
965
|
*
|
966
|
* @param string $name
|
967
|
* The icon name, minus the "glyphicon-" prefix.
|
968
|
* @param array|string $default
|
969
|
* (Optional) The default render array to use if $name is not available.
|
970
|
* @param array $attributes
|
971
|
* (Optional) Additional attributes to merge onto the icon.
|
972
|
*
|
973
|
* @return array
|
974
|
* The render containing the icon defined by $name, $default value if
|
975
|
* icon does not exist or returns NULL if no icon could be rendered.
|
976
|
*/
|
977
|
function _bootstrap_glyphicon($name, $default = array(), array $attributes = array()) {
|
978
|
$icon = array();
|
979
|
|
980
|
// Ensure the icon specified is a valid Bootstrap Glyphicon.
|
981
|
if (_bootstrap_glyphicons_supported() && in_array($name, _bootstrap_glyphicons())) {
|
982
|
// Attempt to use the Icon API module, if enabled and it generates output.
|
983
|
if (module_exists('icon')) {
|
984
|
$icon = array(
|
985
|
'#type' => 'icon',
|
986
|
'#bundle' => 'bootstrap',
|
987
|
'#icon' => 'glyphicon-' . $name,
|
988
|
'#attributes' => $attributes,
|
989
|
);
|
990
|
}
|
991
|
else {
|
992
|
$icon = array(
|
993
|
'#theme' => 'html_tag',
|
994
|
'#tag' => 'span',
|
995
|
'#value' => '',
|
996
|
'#attributes' => $attributes,
|
997
|
);
|
998
|
|
999
|
// Unlike 8.x-3.x, it will be easier to add the classes and aria-hidden
|
1000
|
// attribute afterwards since there is a passed default $attributes
|
1001
|
// parameter and drupal_array_merge_deep() is notorious for not properly
|
1002
|
// merging attributes, especially when dealing with classes.
|
1003
|
_bootstrap_add_class(array('icon', 'glyphicon', 'glyphicon-' . $name), $icon);
|
1004
|
$icon['#attributes']['aria-hidden'] = 'true';
|
1005
|
}
|
1006
|
}
|
1007
|
|
1008
|
// Return the icon.
|
1009
|
if (!empty($icon)) {
|
1010
|
return $icon;
|
1011
|
}
|
1012
|
|
1013
|
// _bootstrap_icon() may pass NULL as $default. If so, return an empty array.
|
1014
|
return isset($default) ? $default : array();
|
1015
|
}
|
1016
|
|
1017
|
/**
|
1018
|
* Determine whether or not Bootstrap Glyphicons can be used.
|
1019
|
*/
|
1020
|
function _bootstrap_glyphicons_supported() {
|
1021
|
// Use the advanced drupal_static() pattern, since this has the potential to
|
1022
|
// be called very often by _bootstrap_icon().
|
1023
|
static $drupal_static_fast;
|
1024
|
if (!isset($drupal_static_fast)) {
|
1025
|
$drupal_static_fast['supported'] = &drupal_static(__FUNCTION__);
|
1026
|
// Get the active theme.
|
1027
|
$drupal_static_fast['theme'] = variable_get('theme_default', $GLOBALS['theme']);
|
1028
|
}
|
1029
|
|
1030
|
// Get static data.
|
1031
|
$supported = &$drupal_static_fast['supported'];
|
1032
|
$theme = &$drupal_static_fast['theme'];
|
1033
|
|
1034
|
// Retrieve supported themes.
|
1035
|
if (!isset($supported)) {
|
1036
|
$supported = array();
|
1037
|
|
1038
|
// Retrieve cached data.
|
1039
|
$cid = 'theme_registry:bootstrap:icon_support';
|
1040
|
if (($cache = cache_get($cid)) && !empty($cache->data)) {
|
1041
|
$supported = $cache->data;
|
1042
|
}
|
1043
|
|
1044
|
if (!isset($supported[$theme])) {
|
1045
|
// Bootstrap based themes are enabled by default to use CDN. Check if
|
1046
|
// that is the case here so no file discovery is necessary. If the active
|
1047
|
// theme does not have this setting, it falls back to the base theme that
|
1048
|
// does.
|
1049
|
$supported[$theme] = !!bootstrap_get_cdn_assets('css', NULL, $theme);
|
1050
|
|
1051
|
// CDN not used, iterate over all of the active (base) themes to determine
|
1052
|
// if they contain glyphicon font files.
|
1053
|
if (!$supported[$theme]) {
|
1054
|
foreach (_bootstrap_get_base_themes($theme, TRUE) as $_theme) {
|
1055
|
// Scan the theme for files.
|
1056
|
$fonts = _bootstrap_file_scan_directory(drupal_get_path('theme', $_theme), '/glyphicons-halflings-regular\.(eot|svg|ttf|woff)$/');
|
1057
|
|
1058
|
// Fonts found, stop the search.
|
1059
|
if (!empty($fonts)) {
|
1060
|
$supported[$theme] = TRUE;
|
1061
|
break;
|
1062
|
}
|
1063
|
}
|
1064
|
}
|
1065
|
|
1066
|
// Cache all supported themes now that this theme is added to the array.
|
1067
|
cache_set($cid, $supported);
|
1068
|
}
|
1069
|
}
|
1070
|
return $supported[$theme];
|
1071
|
}
|
1072
|
|
1073
|
/**
|
1074
|
* Determine whether a specific element is a button.
|
1075
|
*
|
1076
|
* @param array $element
|
1077
|
* A renderable element.
|
1078
|
*
|
1079
|
* @return bool
|
1080
|
* TRUE or FALSE.
|
1081
|
*/
|
1082
|
function _bootstrap_is_button($element) {
|
1083
|
return
|
1084
|
!empty($element['#type']) &&
|
1085
|
!empty($element['#value']) && (
|
1086
|
$element['#type'] === 'button' ||
|
1087
|
$element['#type'] === 'submit' ||
|
1088
|
$element['#type'] === 'image_button'
|
1089
|
);
|
1090
|
}
|
1091
|
|
1092
|
/**
|
1093
|
* Adds a specific Bootstrap class to color a button based on its text value.
|
1094
|
*
|
1095
|
* @param array $element
|
1096
|
* The form element, passed by reference.
|
1097
|
*/
|
1098
|
function _bootstrap_colorize_button(&$element) {
|
1099
|
if (_bootstrap_is_button($element)) {
|
1100
|
// Do not add the class if one is already present in the array.
|
1101
|
$button_classes = array(
|
1102
|
'btn-default',
|
1103
|
'btn-primary',
|
1104
|
'btn-success',
|
1105
|
'btn-info',
|
1106
|
'btn-warning',
|
1107
|
'btn-danger',
|
1108
|
'btn-link',
|
1109
|
);
|
1110
|
$class_intersection = array_intersect($button_classes, $element['#attributes']['class']);
|
1111
|
if (empty($class_intersection)) {
|
1112
|
// Get the matched class.
|
1113
|
$class = bootstrap_setting('button_colorize') ? _bootstrap_colorize_text($element['#value']) : FALSE;
|
1114
|
// If no particular class matched, use the default style.
|
1115
|
if (!$class) {
|
1116
|
$class = 'default';
|
1117
|
}
|
1118
|
$element['#attributes']['class'][] = 'btn-' . $class;
|
1119
|
}
|
1120
|
}
|
1121
|
}
|
1122
|
|
1123
|
/**
|
1124
|
* Matches a Bootstrap class based on a string value.
|
1125
|
*
|
1126
|
* @param string $string
|
1127
|
* The string to match classes against.
|
1128
|
* @param string $default
|
1129
|
* The default class to return if no match is found.
|
1130
|
*
|
1131
|
* @return string
|
1132
|
* The Bootstrap class matched against the value of $haystack or $default if
|
1133
|
* no match could be made.
|
1134
|
*/
|
1135
|
function _bootstrap_colorize_text($string, $default = '') {
|
1136
|
static $texts;
|
1137
|
if (!isset($texts)) {
|
1138
|
$texts = array(
|
1139
|
// Text that match these specific strings are checked first.
|
1140
|
'matches' => array(
|
1141
|
// Primary class.
|
1142
|
t('Download feature') => 'primary',
|
1143
|
|
1144
|
// Success class.
|
1145
|
t('Add effect') => 'success',
|
1146
|
t('Add and configure') => 'success',
|
1147
|
|
1148
|
// Info class.
|
1149
|
t('Save and add') => 'info',
|
1150
|
t('Add another item') => 'info',
|
1151
|
t('Update style') => 'info',
|
1152
|
),
|
1153
|
|
1154
|
// Text that contain these words anywhere in the string are checked last.
|
1155
|
'contains' => array(
|
1156
|
// Primary class.
|
1157
|
t('Confirm') => 'primary',
|
1158
|
t('Filter') => 'primary',
|
1159
|
t('Log in') => 'primary',
|
1160
|
t('Submit') => 'primary',
|
1161
|
t('Search') => 'primary',
|
1162
|
t('Upload') => 'primary',
|
1163
|
|
1164
|
// Success class.
|
1165
|
t('Add') => 'success',
|
1166
|
t('Create') => 'success',
|
1167
|
t('Save') => 'success',
|
1168
|
t('Write') => 'success',
|
1169
|
|
1170
|
// Warning class.
|
1171
|
t('Export') => 'warning',
|
1172
|
t('Import') => 'warning',
|
1173
|
t('Restore') => 'warning',
|
1174
|
t('Rebuild') => 'warning',
|
1175
|
|
1176
|
// Info class.
|
1177
|
t('Apply') => 'info',
|
1178
|
t('Update') => 'info',
|
1179
|
|
1180
|
// Danger class.
|
1181
|
t('Delete') => 'danger',
|
1182
|
t('Remove') => 'danger',
|
1183
|
),
|
1184
|
);
|
1185
|
|
1186
|
// Allow sub-themes to alter this array of patterns.
|
1187
|
drupal_alter('bootstrap_colorize_text', $texts);
|
1188
|
}
|
1189
|
|
1190
|
// Iterate over the array.
|
1191
|
foreach ($texts as $pattern => $strings) {
|
1192
|
foreach ($strings as $value => $class) {
|
1193
|
switch ($pattern) {
|
1194
|
case 'matches':
|
1195
|
if ($string === $value) {
|
1196
|
return $class;
|
1197
|
}
|
1198
|
break;
|
1199
|
|
1200
|
case 'contains':
|
1201
|
if (strpos(drupal_strtolower($string), drupal_strtolower($value)) !== FALSE) {
|
1202
|
return $class;
|
1203
|
}
|
1204
|
break;
|
1205
|
}
|
1206
|
}
|
1207
|
}
|
1208
|
|
1209
|
// Return the default if nothing was matched.
|
1210
|
return $default;
|
1211
|
}
|
1212
|
|
1213
|
/**
|
1214
|
* Adds an icon to button element based on its text value.
|
1215
|
*
|
1216
|
* @param array $element
|
1217
|
* The form element, passed by reference.
|
1218
|
*/
|
1219
|
function _bootstrap_iconize_button(&$element) {
|
1220
|
if (bootstrap_setting('button_iconize') && _bootstrap_is_button($element) && ($icon = _bootstrap_iconize_text($element['#value']))) {
|
1221
|
$element['#icon'] = $icon;
|
1222
|
}
|
1223
|
}
|
1224
|
|
1225
|
/**
|
1226
|
* Matches a Bootstrap Glyphicon based on a string value.
|
1227
|
*
|
1228
|
* @param string $string
|
1229
|
* The string to match classes against.
|
1230
|
* @param string $default
|
1231
|
* The default icon to return if no match is found.
|
1232
|
*
|
1233
|
* @return string
|
1234
|
* The Bootstrap icon matched against the value of $haystack or $default if
|
1235
|
* no match could be made.
|
1236
|
*/
|
1237
|
function _bootstrap_iconize_text($string, $default = '') {
|
1238
|
static $texts;
|
1239
|
if (!isset($texts)) {
|
1240
|
$texts = array(
|
1241
|
// Text that match these specific strings are checked first.
|
1242
|
'matches' => array(),
|
1243
|
|
1244
|
// Text that contain these words anywhere in the string are checked last.
|
1245
|
'contains' => array(
|
1246
|
t('Manage') => 'cog',
|
1247
|
t('Configure') => 'cog',
|
1248
|
t('Download') => 'download',
|
1249
|
t('Export') => 'export',
|
1250
|
t('Filter') => 'filter',
|
1251
|
t('Import') => 'import',
|
1252
|
t('Save') => 'ok',
|
1253
|
t('Update') => 'ok',
|
1254
|
t('Edit') => 'pencil',
|
1255
|
t('Add') => 'plus',
|
1256
|
t('Write') => 'plus',
|
1257
|
t('Cancel') => 'remove',
|
1258
|
t('Delete') => 'trash',
|
1259
|
t('Remove') => 'trash',
|
1260
|
t('Upload') => 'upload',
|
1261
|
t('Log In') => 'log-in',
|
1262
|
),
|
1263
|
);
|
1264
|
|
1265
|
// Allow sub-themes to alter this array of patterns.
|
1266
|
drupal_alter('bootstrap_iconize_text', $texts);
|
1267
|
}
|
1268
|
|
1269
|
// Iterate over the array.
|
1270
|
foreach ($texts as $pattern => $strings) {
|
1271
|
foreach ($strings as $value => $icon) {
|
1272
|
switch ($pattern) {
|
1273
|
case 'matches':
|
1274
|
if ($string === $value) {
|
1275
|
return _bootstrap_icon($icon, $default);
|
1276
|
}
|
1277
|
break;
|
1278
|
|
1279
|
case 'contains':
|
1280
|
if (strpos(drupal_strtolower($string), drupal_strtolower($value)) !== FALSE) {
|
1281
|
return _bootstrap_icon($icon, $default);
|
1282
|
}
|
1283
|
break;
|
1284
|
}
|
1285
|
}
|
1286
|
}
|
1287
|
|
1288
|
// Return a default icon if nothing was matched.
|
1289
|
return _bootstrap_icon($default);
|
1290
|
}
|
1291
|
|
1292
|
/**
|
1293
|
* Invokes a specific suggestion's preprocess functions.
|
1294
|
*
|
1295
|
* @param array $variables
|
1296
|
* The theme implementation variables array.
|
1297
|
*/
|
1298
|
function _bootstrap_preprocess_theme_suggestion(&$variables) {
|
1299
|
$registry = theme_get_registry();
|
1300
|
if (!empty($variables['theme_hook_suggestion']) && !empty($registry[$variables['theme_hook_suggestion']]['preprocess functions'])) {
|
1301
|
// Save the suggestion as the hook to pass to the function.
|
1302
|
$hook = $variables['theme_hook_suggestion'];
|
1303
|
|
1304
|
// Iterate over the preprocess functions.
|
1305
|
foreach ($registry[$hook]['preprocess functions'] as $function) {
|
1306
|
// Ensure that the function is not this one (recursive) and exists.
|
1307
|
if ($function !== __FUNCTION__ && function_exists($function)) {
|
1308
|
// Invoke theme hook suggestion preprocess function.
|
1309
|
$function($variables, $hook);
|
1310
|
|
1311
|
// Unset the theme_hook_suggestion so the suggestion's preprocess
|
1312
|
// functions can provide theme_hook_suggestions if needed.
|
1313
|
if (!empty($variables['theme_hook_suggestions'])) {
|
1314
|
unset($variables['theme_hook_suggestion']);
|
1315
|
}
|
1316
|
}
|
1317
|
}
|
1318
|
}
|
1319
|
}
|
1320
|
|
1321
|
/**
|
1322
|
* Invokes a specific suggestion's process functions.
|
1323
|
*
|
1324
|
* @param array $variables
|
1325
|
* The theme implementation variables array.
|
1326
|
*/
|
1327
|
function _bootstrap_process_theme_suggestion(&$variables) {
|
1328
|
$registry = theme_get_registry();
|
1329
|
if (!empty($variables['theme_hook_suggestion']) && !empty($registry[$variables['theme_hook_suggestion']]['process functions'])) {
|
1330
|
// Save the suggestion as the hook to pass to the function.
|
1331
|
$hook = $variables['theme_hook_suggestion'];
|
1332
|
|
1333
|
// Iterate over the process functions.
|
1334
|
foreach ($registry[$hook]['process functions'] as $function) {
|
1335
|
if (function_exists($function)) {
|
1336
|
// Invoke theme hook suggestion process function.
|
1337
|
$function($variables, $hook);
|
1338
|
|
1339
|
// Unset the theme_hook_suggestion so the suggestion's preprocess
|
1340
|
// functions can provide theme_hook_suggestions if needed.
|
1341
|
if (!empty($variables['theme_hook_suggestions'])) {
|
1342
|
unset($variables['theme_hook_suggestion']);
|
1343
|
}
|
1344
|
}
|
1345
|
}
|
1346
|
}
|
1347
|
}
|
1348
|
|
1349
|
/**
|
1350
|
* Determines if a string of text is considered "simple".
|
1351
|
*
|
1352
|
* @param string $string
|
1353
|
* The string of text to check "simple" criteria on.
|
1354
|
* @param int|FALSE $length
|
1355
|
* The length of characters used to determine whether or not $string is
|
1356
|
* considered "simple". Set explicitly to FALSE to disable this criteria.
|
1357
|
* @param array|FALSE $allowed_tags
|
1358
|
* An array of allowed tag elements. Set explicitly to FALSE to disable this
|
1359
|
* criteria.
|
1360
|
* @param bool $html
|
1361
|
* A variable, passed by reference, that indicates whether or not the
|
1362
|
* string contains HTML.
|
1363
|
*
|
1364
|
* @return bool
|
1365
|
* Returns TRUE if the $string is considered "simple", FALSE otherwise.
|
1366
|
*/
|
1367
|
function _bootstrap_is_simple_string($string, $length = 250, $allowed_tags = NULL, &$html = FALSE) {
|
1368
|
// Use the advanced drupal_static() pattern, since this is called very often.
|
1369
|
static $drupal_static_fast;
|
1370
|
if (!isset($drupal_static_fast)) {
|
1371
|
$drupal_static_fast['strings'] = &drupal_static(__FUNCTION__);
|
1372
|
}
|
1373
|
$strings = &$drupal_static_fast['strings'];
|
1374
|
if (!isset($strings[$string])) {
|
1375
|
$plain_string = strip_tags($string);
|
1376
|
$simple = TRUE;
|
1377
|
if ($allowed_tags !== FALSE) {
|
1378
|
$filtered_string = filter_xss($string, $allowed_tags);
|
1379
|
$html = $filtered_string !== $plain_string;
|
1380
|
$simple = $simple && $string === $filtered_string;
|
1381
|
}
|
1382
|
if ($length !== FALSE) {
|
1383
|
$simple = $simple && strlen($plain_string) <= intval($length);
|
1384
|
}
|
1385
|
$strings[$string] = $simple;
|
1386
|
}
|
1387
|
return $strings[$string];
|
1388
|
}
|
1389
|
|
1390
|
/**
|
1391
|
* Determines if the Path Breadcrumbs module theme function should be used.
|
1392
|
*
|
1393
|
* @param string $theme
|
1394
|
* The machine name of a specific theme to determine status if the Path
|
1395
|
* Breadcrumbs module has been configured to only use its internal function
|
1396
|
* on a specific list of themes.
|
1397
|
*
|
1398
|
* @return bool
|
1399
|
* TRUE or FALSE
|
1400
|
*/
|
1401
|
function _bootstrap_use_path_breadcrumbs($theme = NULL) {
|
1402
|
static $path_breadcrumbs;
|
1403
|
|
1404
|
if (!isset($path_breadcrumbs)) {
|
1405
|
$path_breadcrumbs = FALSE;
|
1406
|
|
1407
|
// Use active theme as the theme key if not explicitly set.
|
1408
|
if (!isset($theme)) {
|
1409
|
$theme = $GLOBALS['theme_key'];
|
1410
|
}
|
1411
|
|
1412
|
// Determine whether or not the internal Path Breadcrumbs theme function
|
1413
|
// should be used or not.
|
1414
|
if (function_exists('path_breadcrumbs_breadcrumb') && module_exists('path_breadcrumbs')) {
|
1415
|
$internal_render = variable_get('path_breadcrumbs_internal_render', 1);
|
1416
|
$themes = variable_get('path_breadcrumbs_internal_render_themes', array());
|
1417
|
$path_breadcrumbs = ($internal_render && (empty($themes) || in_array($theme, $themes)));
|
1418
|
}
|
1419
|
}
|
1420
|
|
1421
|
return $path_breadcrumbs;
|
1422
|
}
|
1423
|
|
1424
|
/**
|
1425
|
* @} End of "defgroup subtheme_helper_functions".
|
1426
|
*/
|