Projet

Général

Profil

Paste
Télécharger (46,2 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / themes / bootstrap / includes / common.inc @ 1f623f01

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
  // Retrieve cached data.
563
  $cid = 'theme_registry:bootstrap:files';
564
  $files = array();
565
  if ($cache = cache_get($cid)) {
566
    $files = $cache->data;
567
  }
568
  // Generate a unique hash for all parameters passed as a change in any of
569
  // them would return different results.
570
  $hash = drupal_hash_base64(serialize(func_get_args()));
571
  if (!isset($files[$hash])) {
572
    $files[$hash] = file_scan_directory($dir, $mask, $options);
573
    cache_set($cid, $files);
574
  }
575
  return $files[$hash];
576
}
577

    
578
/**
579
 * Filters HTML to prevent cross-site-scripting (XSS) vulnerabilities.
580
 *
581
 * Very similar to core's filter_xss(). It does, however, include the addition
582
 * of the "span", "div" and "i" elements which are commonly used in Bootstrap.
583
 *
584
 * @param string $string
585
 *   The string with raw HTML in it. It will be stripped of everything that can
586
 *   cause an XSS attack.
587
 * @param array $allowed_tags
588
 *   An array of allowed tags.
589
 *
590
 * @return string
591
 *   An XSS safe version of $string, or an empty string if $string is not
592
 *   valid UTF-8.
593
 *
594
 * @see filter_xss()
595
 */
596
function _bootstrap_filter_xss($string, array $allowed_tags = NULL) {
597
  if (is_null($allowed_tags)) {
598
    $allowed_tags = array(
599
      // Inline elements.
600
      'a',
601
      'cite',
602
      'em',
603
      'i',
604
      'span',
605
      'strong',
606

    
607
      // Block elements.
608
      'blockquote',
609
      'code',
610
      'div',
611
      'ul',
612
      'ol',
613
      'li',
614
      'dl',
615
      'dt',
616
      'dd',
617
    );
618
  }
619
  return filter_xss($string, $allowed_tags);
620
}
621

    
622
/**
623
 * Returns a list of available Bootstrap Glyphicons.
624
 *
625
 * @param string $version
626
 *   The specific version of glyphicons to return. If not set, the latest
627
 *   BOOTSTRAP_VERSION will be used.
628
 *
629
 * @return array
630
 *   An associative array of icons keyed by their classes.
631
 */
632
function _bootstrap_glyphicons($version = NULL) {
633
  static $versions;
634
  if (!isset($versions)) {
635
    $versions = array();
636
    $versions['3.0.0'] = array(
637
      // Class => Name.
638
      'glyphicon-adjust' => 'adjust',
639
      'glyphicon-align-center' => 'align-center',
640
      'glyphicon-align-justify' => 'align-justify',
641
      'glyphicon-align-left' => 'align-left',
642
      'glyphicon-align-right' => 'align-right',
643
      'glyphicon-arrow-down' => 'arrow-down',
644
      'glyphicon-arrow-left' => 'arrow-left',
645
      'glyphicon-arrow-right' => 'arrow-right',
646
      'glyphicon-arrow-up' => 'arrow-up',
647
      'glyphicon-asterisk' => 'asterisk',
648
      'glyphicon-backward' => 'backward',
649
      'glyphicon-ban-circle' => 'ban-circle',
650
      'glyphicon-barcode' => 'barcode',
651
      'glyphicon-bell' => 'bell',
652
      'glyphicon-bold' => 'bold',
653
      'glyphicon-book' => 'book',
654
      'glyphicon-bookmark' => 'bookmark',
655
      'glyphicon-briefcase' => 'briefcase',
656
      'glyphicon-bullhorn' => 'bullhorn',
657
      'glyphicon-calendar' => 'calendar',
658
      'glyphicon-camera' => 'camera',
659
      'glyphicon-certificate' => 'certificate',
660
      'glyphicon-check' => 'check',
661
      'glyphicon-chevron-down' => 'chevron-down',
662
      'glyphicon-chevron-left' => 'chevron-left',
663
      'glyphicon-chevron-right' => 'chevron-right',
664
      'glyphicon-chevron-up' => 'chevron-up',
665
      'glyphicon-circle-arrow-down' => 'circle-arrow-down',
666
      'glyphicon-circle-arrow-left' => 'circle-arrow-left',
667
      'glyphicon-circle-arrow-right' => 'circle-arrow-right',
668
      'glyphicon-circle-arrow-up' => 'circle-arrow-up',
669
      'glyphicon-cloud' => 'cloud',
670
      'glyphicon-cloud-download' => 'cloud-download',
671
      'glyphicon-cloud-upload' => 'cloud-upload',
672
      'glyphicon-cog' => 'cog',
673
      'glyphicon-collapse-down' => 'collapse-down',
674
      'glyphicon-collapse-up' => 'collapse-up',
675
      'glyphicon-comment' => 'comment',
676
      'glyphicon-compressed' => 'compressed',
677
      'glyphicon-copyright-mark' => 'copyright-mark',
678
      'glyphicon-credit-card' => 'credit-card',
679
      'glyphicon-cutlery' => 'cutlery',
680
      'glyphicon-dashboard' => 'dashboard',
681
      'glyphicon-download' => 'download',
682
      'glyphicon-download-alt' => 'download-alt',
683
      'glyphicon-earphone' => 'earphone',
684
      'glyphicon-edit' => 'edit',
685
      'glyphicon-eject' => 'eject',
686
      'glyphicon-envelope' => 'envelope',
687
      'glyphicon-euro' => 'euro',
688
      'glyphicon-exclamation-sign' => 'exclamation-sign',
689
      'glyphicon-expand' => 'expand',
690
      'glyphicon-export' => 'export',
691
      'glyphicon-eye-close' => 'eye-close',
692
      'glyphicon-eye-open' => 'eye-open',
693
      'glyphicon-facetime-video' => 'facetime-video',
694
      'glyphicon-fast-backward' => 'fast-backward',
695
      'glyphicon-fast-forward' => 'fast-forward',
696
      'glyphicon-file' => 'file',
697
      'glyphicon-film' => 'film',
698
      'glyphicon-filter' => 'filter',
699
      'glyphicon-fire' => 'fire',
700
      'glyphicon-flag' => 'flag',
701
      'glyphicon-flash' => 'flash',
702
      'glyphicon-floppy-disk' => 'floppy-disk',
703
      'glyphicon-floppy-open' => 'floppy-open',
704
      'glyphicon-floppy-remove' => 'floppy-remove',
705
      'glyphicon-floppy-save' => 'floppy-save',
706
      'glyphicon-floppy-saved' => 'floppy-saved',
707
      'glyphicon-folder-close' => 'folder-close',
708
      'glyphicon-folder-open' => 'folder-open',
709
      'glyphicon-font' => 'font',
710
      'glyphicon-forward' => 'forward',
711
      'glyphicon-fullscreen' => 'fullscreen',
712
      'glyphicon-gbp' => 'gbp',
713
      'glyphicon-gift' => 'gift',
714
      'glyphicon-glass' => 'glass',
715
      'glyphicon-globe' => 'globe',
716
      'glyphicon-hand-down' => 'hand-down',
717
      'glyphicon-hand-left' => 'hand-left',
718
      'glyphicon-hand-right' => 'hand-right',
719
      'glyphicon-hand-up' => 'hand-up',
720
      'glyphicon-hd-video' => 'hd-video',
721
      'glyphicon-hdd' => 'hdd',
722
      'glyphicon-header' => 'header',
723
      'glyphicon-headphones' => 'headphones',
724
      'glyphicon-heart' => 'heart',
725
      'glyphicon-heart-empty' => 'heart-empty',
726
      'glyphicon-home' => 'home',
727
      'glyphicon-import' => 'import',
728
      'glyphicon-inbox' => 'inbox',
729
      'glyphicon-indent-left' => 'indent-left',
730
      'glyphicon-indent-right' => 'indent-right',
731
      'glyphicon-info-sign' => 'info-sign',
732
      'glyphicon-italic' => 'italic',
733
      'glyphicon-leaf' => 'leaf',
734
      'glyphicon-link' => 'link',
735
      'glyphicon-list' => 'list',
736
      'glyphicon-list-alt' => 'list-alt',
737
      'glyphicon-lock' => 'lock',
738
      'glyphicon-log-in' => 'log-in',
739
      'glyphicon-log-out' => 'log-out',
740
      'glyphicon-magnet' => 'magnet',
741
      'glyphicon-map-marker' => 'map-marker',
742
      'glyphicon-minus' => 'minus',
743
      'glyphicon-minus-sign' => 'minus-sign',
744
      'glyphicon-move' => 'move',
745
      'glyphicon-music' => 'music',
746
      'glyphicon-new-window' => 'new-window',
747
      'glyphicon-off' => 'off',
748
      'glyphicon-ok' => 'ok',
749
      'glyphicon-ok-circle' => 'ok-circle',
750
      'glyphicon-ok-sign' => 'ok-sign',
751
      'glyphicon-open' => 'open',
752
      'glyphicon-paperclip' => 'paperclip',
753
      'glyphicon-pause' => 'pause',
754
      'glyphicon-pencil' => 'pencil',
755
      'glyphicon-phone' => 'phone',
756
      'glyphicon-phone-alt' => 'phone-alt',
757
      'glyphicon-picture' => 'picture',
758
      'glyphicon-plane' => 'plane',
759
      'glyphicon-play' => 'play',
760
      'glyphicon-play-circle' => 'play-circle',
761
      'glyphicon-plus' => 'plus',
762
      'glyphicon-plus-sign' => 'plus-sign',
763
      'glyphicon-print' => 'print',
764
      'glyphicon-pushpin' => 'pushpin',
765
      'glyphicon-qrcode' => 'qrcode',
766
      'glyphicon-question-sign' => 'question-sign',
767
      'glyphicon-random' => 'random',
768
      'glyphicon-record' => 'record',
769
      'glyphicon-refresh' => 'refresh',
770
      'glyphicon-registration-mark' => 'registration-mark',
771
      'glyphicon-remove' => 'remove',
772
      'glyphicon-remove-circle' => 'remove-circle',
773
      'glyphicon-remove-sign' => 'remove-sign',
774
      'glyphicon-repeat' => 'repeat',
775
      'glyphicon-resize-full' => 'resize-full',
776
      'glyphicon-resize-horizontal' => 'resize-horizontal',
777
      'glyphicon-resize-small' => 'resize-small',
778
      'glyphicon-resize-vertical' => 'resize-vertical',
779
      'glyphicon-retweet' => 'retweet',
780
      'glyphicon-road' => 'road',
781
      'glyphicon-save' => 'save',
782
      'glyphicon-saved' => 'saved',
783
      'glyphicon-screenshot' => 'screenshot',
784
      'glyphicon-sd-video' => 'sd-video',
785
      'glyphicon-search' => 'search',
786
      'glyphicon-send' => 'send',
787
      'glyphicon-share' => 'share',
788
      'glyphicon-share-alt' => 'share-alt',
789
      'glyphicon-shopping-cart' => 'shopping-cart',
790
      'glyphicon-signal' => 'signal',
791
      'glyphicon-sort' => 'sort',
792
      'glyphicon-sort-by-alphabet' => 'sort-by-alphabet',
793
      'glyphicon-sort-by-alphabet-alt' => 'sort-by-alphabet-alt',
794
      'glyphicon-sort-by-attributes' => 'sort-by-attributes',
795
      'glyphicon-sort-by-attributes-alt' => 'sort-by-attributes-alt',
796
      'glyphicon-sort-by-order' => 'sort-by-order',
797
      'glyphicon-sort-by-order-alt' => 'sort-by-order-alt',
798
      'glyphicon-sound-5-1' => 'sound-5-1',
799
      'glyphicon-sound-6-1' => 'sound-6-1',
800
      'glyphicon-sound-7-1' => 'sound-7-1',
801
      'glyphicon-sound-dolby' => 'sound-dolby',
802
      'glyphicon-sound-stereo' => 'sound-stereo',
803
      'glyphicon-star' => 'star',
804
      'glyphicon-star-empty' => 'star-empty',
805
      'glyphicon-stats' => 'stats',
806
      'glyphicon-step-backward' => 'step-backward',
807
      'glyphicon-step-forward' => 'step-forward',
808
      'glyphicon-stop' => 'stop',
809
      'glyphicon-subtitles' => 'subtitles',
810
      'glyphicon-tag' => 'tag',
811
      'glyphicon-tags' => 'tags',
812
      'glyphicon-tasks' => 'tasks',
813
      'glyphicon-text-height' => 'text-height',
814
      'glyphicon-text-width' => 'text-width',
815
      'glyphicon-th' => 'th',
816
      'glyphicon-th-large' => 'th-large',
817
      'glyphicon-th-list' => 'th-list',
818
      'glyphicon-thumbs-down' => 'thumbs-down',
819
      'glyphicon-thumbs-up' => 'thumbs-up',
820
      'glyphicon-time' => 'time',
821
      'glyphicon-tint' => 'tint',
822
      'glyphicon-tower' => 'tower',
823
      'glyphicon-transfer' => 'transfer',
824
      'glyphicon-trash' => 'trash',
825
      'glyphicon-tree-conifer' => 'tree-conifer',
826
      'glyphicon-tree-deciduous' => 'tree-deciduous',
827
      'glyphicon-unchecked' => 'unchecked',
828
      'glyphicon-upload' => 'upload',
829
      'glyphicon-usd' => 'usd',
830
      'glyphicon-user' => 'user',
831
      'glyphicon-volume-down' => 'volume-down',
832
      'glyphicon-volume-off' => 'volume-off',
833
      'glyphicon-volume-up' => 'volume-up',
834
      'glyphicon-warning-sign' => 'warning-sign',
835
      'glyphicon-wrench' => 'wrench',
836
      'glyphicon-zoom-in' => 'zoom-in',
837
      'glyphicon-zoom-out' => 'zoom-out',
838
    );
839
    $versions['3.0.1'] = $versions['3.0.0'];
840
    $versions['3.0.2'] = $versions['3.0.1'];
841
    $versions['3.0.3'] = $versions['3.0.2'];
842
    $versions['3.1.0'] = $versions['3.0.3'];
843
    $versions['3.1.1'] = $versions['3.1.0'];
844
    $versions['3.2.0'] = $versions['3.1.1'];
845
    $versions['3.3.0'] = array_merge($versions['3.2.0'], array(
846
      'glyphicon-eur' => 'eur',
847
    ));
848
    $versions['3.3.1'] = $versions['3.3.0'];
849
    $versions['3.3.2'] = array_merge($versions['3.3.1'], array(
850
      'glyphicon-alert' => 'alert',
851
      'glyphicon-apple' => 'apple',
852
      'glyphicon-baby-formula' => 'baby-formula',
853
      'glyphicon-bed' => 'bed',
854
      'glyphicon-bishop' => 'bishop',
855
      'glyphicon-bitcoin' => 'bitcoin',
856
      'glyphicon-blackboard' => 'blackboard',
857
      'glyphicon-cd' => 'cd',
858
      'glyphicon-console' => 'console',
859
      'glyphicon-copy' => 'copy',
860
      'glyphicon-duplicate' => 'duplicate',
861
      'glyphicon-education' => 'education',
862
      'glyphicon-equalizer' => 'equalizer',
863
      'glyphicon-erase' => 'erase',
864
      'glyphicon-grain' => 'grain',
865
      'glyphicon-hourglass' => 'hourglass',
866
      'glyphicon-ice-lolly' => 'ice-lolly',
867
      'glyphicon-ice-lolly-tasted' => 'ice-lolly-tasted',
868
      'glyphicon-king' => 'king',
869
      'glyphicon-knight' => 'knight',
870
      'glyphicon-lamp' => 'lamp',
871
      'glyphicon-level-up' => 'level-up',
872
      'glyphicon-menu-down' => 'menu-down',
873
      'glyphicon-menu-hamburger' => 'menu-hamburger',
874
      'glyphicon-menu-left' => 'menu-left',
875
      'glyphicon-menu-right' => 'menu-right',
876
      'glyphicon-menu-up' => 'menu-up',
877
      'glyphicon-modal-window' => 'modal-window',
878
      'glyphicon-object-align-bottom' => 'object-align-bottom',
879
      'glyphicon-object-align-horizontal' => 'object-align-horizontal',
880
      'glyphicon-object-align-left' => 'object-align-left',
881
      'glyphicon-object-align-right' => 'object-align-right',
882
      'glyphicon-object-align-top' => 'object-align-top',
883
      'glyphicon-object-align-vertical' => 'object-align-vertical',
884
      'glyphicon-oil' => 'oil',
885
      'glyphicon-open-file' => 'open-file',
886
      'glyphicon-option-horizontal' => 'option-horizontal',
887
      'glyphicon-option-vertical' => 'option-vertical',
888
      'glyphicon-paste' => 'paste',
889
      'glyphicon-pawn' => 'pawn',
890
      'glyphicon-piggy-bank' => 'piggy-bank',
891
      'glyphicon-queen' => 'queen',
892
      'glyphicon-ruble' => 'ruble',
893
      'glyphicon-save-file' => 'save-file',
894
      'glyphicon-scale' => 'scale',
895
      'glyphicon-scissors' => 'scissors',
896
      'glyphicon-subscript' => 'subscript',
897
      'glyphicon-sunglasses' => 'sunglasses',
898
      'glyphicon-superscript' => 'superscript',
899
      'glyphicon-tent' => 'tent',
900
      'glyphicon-text-background' => 'text-background',
901
      'glyphicon-text-color' => 'text-color',
902
      'glyphicon-text-size' => 'text-size',
903
      'glyphicon-triangle-bottom' => 'triangle-bottom',
904
      'glyphicon-triangle-left' => 'triangle-left',
905
      'glyphicon-triangle-right' => 'triangle-right',
906
      'glyphicon-triangle-top' => 'triangle-top',
907
      'glyphicon-yen' => 'yen',
908
    ));
909
    $versions['3.3.4'] = array_merge($versions['3.3.2'], array(
910
      'glyphicon-btc' => 'btc',
911
      'glyphicon-jpy' => 'jpy',
912
      'glyphicon-rub' => 'rub',
913
      'glyphicon-xbt' => 'xbt',
914
    ));
915
    $versions['3.3.5'] = $versions['3.3.4'];
916
  }
917

    
918
  // Return a specific versions icon set.
919
  if (isset($version) && isset($versions[$version])) {
920
    return $versions[$version];
921
  }
922

    
923
  // Return the latest version.
924
  return $versions[BOOTSTRAP_VERSION];
925
}
926

    
927
/**
928
 * Returns a specific Bootstrap Glyphicon.
929
 *
930
 * @param string $name
931
 *   The icon name, minus the "glyphicon-" prefix.
932
 * @param string $default
933
 *   (Optional) The default value to return.
934
 *
935
 * @return string
936
 *   The HTML markup containing the icon defined by $name, $default value if
937
 *   icon does not exist or returns empty output for whatever reason.
938
 */
939
function _bootstrap_icon($name, $default = NULL) {
940
  $output = NULL;
941
  // Ensure the icon specified is a valid Bootstrap Glyphicon.
942
  // @todo Supply a specific version to _bootstrap_glyphicons() when Icon API
943
  // supports versioning.
944
  if (_bootstrap_glyphicons_supported() && in_array($name, _bootstrap_glyphicons())) {
945
    // Attempt to use the Icon API module, if enabled and it generates output.
946
    if (module_exists('icon')) {
947
      $output = theme('icon', array('bundle' => 'bootstrap', 'icon' => 'glyphicon-' . $name));
948
    }
949
    if (empty($output)) {
950
      // Mimic the Icon API markup.
951
      $attributes = array(
952
        'class' => array('icon', 'glyphicon', 'glyphicon-' . $name),
953
        'aria-hidden' => 'true',
954
      );
955
      $output = '<span' . drupal_attributes($attributes) . '></span>';
956
    }
957
  }
958
  return empty($output) && isset($default) ? $default : $output;
959
}
960

    
961
/**
962
 * Determine whether or not Bootstrap Glyphicons can be used.
963
 */
964
function _bootstrap_glyphicons_supported() {
965
  // Use the advanced drupal_static() pattern, since this has the potential to
966
  // be called very often by _bootstrap_icon().
967
  static $drupal_static_fast;
968
  if (!isset($drupal_static_fast)) {
969
    $drupal_static_fast['supported'] = &drupal_static(__FUNCTION__);
970
    // Get the active theme.
971
    $drupal_static_fast['theme'] = variable_get('theme_default', $GLOBALS['theme']);
972
  }
973

    
974
  // Get static data.
975
  $supported = &$drupal_static_fast['supported'];
976
  $theme = &$drupal_static_fast['theme'];
977

    
978
  // Retrieve supported themes.
979
  if (!isset($supported)) {
980
    $supported = array();
981

    
982
    // Retrieve cached data.
983
    $cid = 'theme_registry:bootstrap:icon_support';
984
    if (($cache = cache_get($cid)) && !empty($cache->data)) {
985
      $supported = $cache->data;
986
    }
987

    
988
    if (!isset($supported[$theme])) {
989
      // Bootstrap based themes are enabled by default to use CDN. Check if
990
      // that is the case here so no file discovery is necessary. If the active
991
      // theme does not have this setting, it falls back to the base theme that
992
      // does.
993
      $supported[$theme] = !!bootstrap_get_cdn_assets('css', NULL, $theme);
994

    
995
      // CDN not used, iterate over all of the active (base) themes to determine
996
      // if they contain glyphicon font files.
997
      if (!$supported[$theme]) {
998
        foreach (_bootstrap_get_base_themes($theme, TRUE) as $_theme) {
999
          // Scan the theme for files.
1000
          $fonts = _bootstrap_file_scan_directory(drupal_get_path('theme', $_theme), '/glyphicons-halflings-regular\.(eot|svg|ttf|woff)$/');
1001

    
1002
          // Fonts found, stop the search.
1003
          if (!empty($fonts)) {
1004
            $supported[$theme] = TRUE;
1005
            break;
1006
          }
1007
        }
1008
      }
1009

    
1010
      // Cache all supported themes now that this theme is added to the array.
1011
      cache_set($cid, $supported);
1012
    }
1013
  }
1014
  return $supported[$theme];
1015
}
1016

    
1017
/**
1018
 * Determine whether a specific element is a button.
1019
 *
1020
 * @param array $element
1021
 *   A renderable element.
1022
 *
1023
 * @return bool
1024
 *   TRUE or FALSE.
1025
 */
1026
function _bootstrap_is_button($element) {
1027
  return
1028
    !empty($element['#type']) &&
1029
    !empty($element['#value']) && (
1030
      $element['#type'] === 'button' ||
1031
      $element['#type'] === 'submit' ||
1032
      $element['#type'] === 'image_button'
1033
    );
1034
}
1035

    
1036
/**
1037
 * Adds a specific Bootstrap class to color a button based on its text value.
1038
 *
1039
 * @param array $element
1040
 *   The form element, passed by reference.
1041
 */
1042
function _bootstrap_colorize_button(&$element) {
1043
  if (_bootstrap_is_button($element)) {
1044
    // Do not add the class if one is already present in the array.
1045
    $button_classes = array(
1046
      'btn-default',
1047
      'btn-primary',
1048
      'btn-success',
1049
      'btn-info',
1050
      'btn-warning',
1051
      'btn-danger',
1052
      'btn-link',
1053
    );
1054
    $class_intersection = array_intersect($button_classes, $element['#attributes']['class']);
1055
    if (empty($class_intersection)) {
1056
      // Get the matched class.
1057
      $class = bootstrap_setting('button_colorize') ? _bootstrap_colorize_text($element['#value']) : FALSE;
1058
      // If no particular class matched, use the default style.
1059
      if (!$class) {
1060
        $class = 'default';
1061
      }
1062
      $element['#attributes']['class'][] = 'btn-' . $class;
1063
    }
1064
  }
1065
}
1066

    
1067
/**
1068
 * Matches a Bootstrap class based on a string value.
1069
 *
1070
 * @param string $string
1071
 *   The string to match classes against.
1072
 * @param string $default
1073
 *   The default class to return if no match is found.
1074
 *
1075
 * @return string
1076
 *   The Bootstrap class matched against the value of $haystack or $default if
1077
 *   no match could be made.
1078
 */
1079
function _bootstrap_colorize_text($string, $default = '') {
1080
  static $texts;
1081
  if (!isset($texts)) {
1082
    $texts = array(
1083
      // Text that match these specific strings are checked first.
1084
      'matches' => array(
1085
        // Primary class.
1086
        t('Download feature')   => 'primary',
1087

    
1088
        // Success class.
1089
        t('Add effect')         => 'success',
1090
        t('Add and configure')  => 'success',
1091

    
1092
        // Info class.
1093
        t('Save and add')       => 'info',
1094
        t('Add another item')   => 'info',
1095
        t('Update style')       => 'info',
1096
      ),
1097

    
1098
      // Text that contain these words anywhere in the string are checked last.
1099
      'contains' => array(
1100
        // Primary class.
1101
        t('Confirm')            => 'primary',
1102
        t('Filter')             => 'primary',
1103
        t('Submit')             => 'primary',
1104
        t('Search')             => 'primary',
1105
        t('Log in')             => 'primary',
1106

    
1107
        // Success class.
1108
        t('Add')                => 'success',
1109
        t('Create')             => 'success',
1110
        t('Save')               => 'success',
1111
        t('Write')              => 'success',
1112

    
1113
        // Warning class.
1114
        t('Export')             => 'warning',
1115
        t('Import')             => 'warning',
1116
        t('Restore')            => 'warning',
1117
        t('Rebuild')            => 'warning',
1118

    
1119
        // Info class.
1120
        t('Apply')              => 'info',
1121
        t('Update')             => 'info',
1122

    
1123
        // Danger class.
1124
        t('Delete')             => 'danger',
1125
        t('Remove')             => 'danger',
1126
      ),
1127
    );
1128

    
1129
    // Allow sub-themes to alter this array of patterns.
1130
    drupal_alter('bootstrap_colorize_text', $texts);
1131
  }
1132

    
1133
  // Iterate over the array.
1134
  foreach ($texts as $pattern => $strings) {
1135
    foreach ($strings as $value => $class) {
1136
      switch ($pattern) {
1137
        case 'matches':
1138
          if ($string === $value) {
1139
            return $class;
1140
          }
1141
          break;
1142

    
1143
        case 'contains':
1144
          if (strpos(drupal_strtolower($string), drupal_strtolower($value)) !== FALSE) {
1145
            return $class;
1146
          }
1147
          break;
1148
      }
1149
    }
1150
  }
1151

    
1152
  // Return the default if nothing was matched.
1153
  return $default;
1154
}
1155

    
1156
/**
1157
 * Adds an icon to button element based on its text value.
1158
 *
1159
 * @param array $element
1160
 *   The form element, passed by reference.
1161
 */
1162
function _bootstrap_iconize_button(&$element) {
1163
  if (bootstrap_setting('button_iconize') && _bootstrap_is_button($element) && ($icon = _bootstrap_iconize_text($element['#value']))) {
1164
    $element['#icon'] = $icon;
1165
  }
1166
}
1167

    
1168
/**
1169
 * Matches a Bootstrap Glyphicon based on a string value.
1170
 *
1171
 * @param string $string
1172
 *   The string to match classes against.
1173
 * @param string $default
1174
 *   The default icon to return if no match is found.
1175
 *
1176
 * @return string
1177
 *   The Bootstrap icon matched against the value of $haystack or $default if
1178
 *   no match could be made.
1179
 */
1180
function _bootstrap_iconize_text($string, $default = '') {
1181
  static $texts;
1182
  if (!isset($texts)) {
1183
    $texts = array(
1184
      // Text that match these specific strings are checked first.
1185
      'matches' => array(),
1186

    
1187
      // Text that contain these words anywhere in the string are checked last.
1188
      'contains' => array(
1189
        t('Manage')     => 'cog',
1190
        t('Configure')  => 'cog',
1191
        t('Download')   => 'download',
1192
        t('Export')     => 'export',
1193
        t('Filter')     => 'filter',
1194
        t('Import')     => 'import',
1195
        t('Save')       => 'ok',
1196
        t('Update')     => 'ok',
1197
        t('Edit')       => 'pencil',
1198
        t('Add')        => 'plus',
1199
        t('Write')      => 'plus',
1200
        t('Cancel')     => 'remove',
1201
        t('Delete')     => 'trash',
1202
        t('Remove')     => 'trash',
1203
        t('Upload')     => 'upload',
1204
        t('Log In')     => 'log-in',
1205
      ),
1206
    );
1207

    
1208
    // Allow sub-themes to alter this array of patterns.
1209
    drupal_alter('bootstrap_iconize_text', $texts);
1210
  }
1211

    
1212
  // Iterate over the array.
1213
  foreach ($texts as $pattern => $strings) {
1214
    foreach ($strings as $value => $icon) {
1215
      switch ($pattern) {
1216
        case 'matches':
1217
          if ($string === $value) {
1218
            return _bootstrap_icon($icon, $default);
1219
          }
1220
          break;
1221

    
1222
        case 'contains':
1223
          if (strpos(drupal_strtolower($string), drupal_strtolower($value)) !== FALSE) {
1224
            return _bootstrap_icon($icon, $default);
1225
          }
1226
          break;
1227
      }
1228
    }
1229
  }
1230

    
1231
  // Return a default icon if nothing was matched.
1232
  return _bootstrap_icon($default);
1233
}
1234

    
1235
/**
1236
 * Invokes a specific suggestion's preprocess functions.
1237
 *
1238
 * @param array $variables
1239
 *   The theme implementation variables array.
1240
 */
1241
function _bootstrap_preprocess_theme_suggestion(&$variables) {
1242
  $registry = theme_get_registry();
1243
  if (!empty($variables['theme_hook_suggestion']) && !empty($registry[$variables['theme_hook_suggestion']]['preprocess functions'])) {
1244
    // Save the suggestion as the hook to pass to the function.
1245
    $hook = $variables['theme_hook_suggestion'];
1246

    
1247
    // Iterate over the preprocess functions.
1248
    foreach ($registry[$hook]['preprocess functions'] as $function) {
1249
      // Ensure that the function is not this one (recursive) and exists.
1250
      if ($function !== __FUNCTION__ && function_exists($function)) {
1251
        // Invoke theme hook suggestion preprocess function.
1252
        $function($variables, $hook);
1253

    
1254
        // Unset the theme_hook_suggestion so the suggestion's preprocess
1255
        // functions can provide theme_hook_suggestions if needed.
1256
        if (!empty($variables['theme_hook_suggestions'])) {
1257
          unset($variables['theme_hook_suggestion']);
1258
        }
1259
      }
1260
    }
1261
  }
1262
}
1263

    
1264
/**
1265
 * Invokes a specific suggestion's process functions.
1266
 *
1267
 * @param array $variables
1268
 *   The theme implementation variables array.
1269
 */
1270
function _bootstrap_process_theme_suggestion(&$variables) {
1271
  $registry = theme_get_registry();
1272
  if (!empty($variables['theme_hook_suggestion']) && !empty($registry[$variables['theme_hook_suggestion']]['process functions'])) {
1273
    // Save the suggestion as the hook to pass to the function.
1274
    $hook = $variables['theme_hook_suggestion'];
1275

    
1276
    // Iterate over the process functions.
1277
    foreach ($registry[$hook]['process functions'] as $function) {
1278
      if (function_exists($function)) {
1279
        // Invoke theme hook suggestion process function.
1280
        $function($variables, $hook);
1281

    
1282
        // Unset the theme_hook_suggestion so the suggestion's preprocess
1283
        // functions can provide theme_hook_suggestions if needed.
1284
        if (!empty($variables['theme_hook_suggestions'])) {
1285
          unset($variables['theme_hook_suggestion']);
1286
        }
1287
      }
1288
    }
1289
  }
1290
}
1291

    
1292
/**
1293
 * Determines if a string of text is considered "simple".
1294
 *
1295
 * @param string $string
1296
 *   The string of text to check "simple" criteria on.
1297
 * @param int|FALSE $length
1298
 *   The length of characters used to determine whether or not $string is
1299
 *   considered "simple". Set explicitly to FALSE to disable this criteria.
1300
 * @param array|FALSE $allowed_tags
1301
 *   An array of allowed tag elements. Set explicitly to FALSE to disable this
1302
 *   criteria.
1303
 * @param bool $html
1304
 *   A variable, passed by reference, that indicates whether or not the
1305
 *   string contains HTML.
1306
 *
1307
 * @return bool
1308
 *   Returns TRUE if the $string is considered "simple", FALSE otherwise.
1309
 */
1310
function _bootstrap_is_simple_string($string, $length = 250, $allowed_tags = NULL, &$html = FALSE) {
1311
  // Use the advanced drupal_static() pattern, since this is called very often.
1312
  static $drupal_static_fast;
1313
  if (!isset($drupal_static_fast)) {
1314
    $drupal_static_fast['strings'] = &drupal_static(__FUNCTION__);
1315
  }
1316
  $strings = &$drupal_static_fast['strings'];
1317
  if (!isset($strings[$string])) {
1318
    $plain_string = strip_tags($string);
1319
    $simple = TRUE;
1320
    if ($allowed_tags !== FALSE) {
1321
      $filtered_string = filter_xss($string, $allowed_tags);
1322
      $html = $filtered_string !== $plain_string;
1323
      $simple = $simple && $string === $filtered_string;
1324
    }
1325
    if ($length !== FALSE) {
1326
      $simple = $simple && strlen($plain_string) <= intval($length);
1327
    }
1328
    $strings[$string] = $simple;
1329
  }
1330
  return $strings[$string];
1331
}
1332

    
1333
/**
1334
 * Determines if the Path Breadcrumbs module theme function should be used.
1335
 *
1336
 * @param string $theme
1337
 *   The machine name of a specific theme to determine status if the Path
1338
 *   Breadcrumbs module has been configured to only use its internal function
1339
 *   on a specific list of themes.
1340
 *
1341
 * @return bool
1342
 *   TRUE or FALSE
1343
 */
1344
function _bootstrap_use_path_breadcrumbs($theme = NULL) {
1345
  static $path_breadcrumbs;
1346

    
1347
  if (!isset($path_breadcrumbs)) {
1348
    $path_breadcrumbs = FALSE;
1349

    
1350
    // Use active theme as the theme key if not explicitly set.
1351
    if (!isset($theme)) {
1352
      $theme = $GLOBALS['theme_key'];
1353
    }
1354

    
1355
    // Determine whether or not the internal Path Breadcrumbs theme function
1356
    // should be used or not.
1357
    if (function_exists('path_breadcrumbs_breadcrumb') && module_exists('path_breadcrumbs')) {
1358
      $internal_render = variable_get('path_breadcrumbs_internal_render', 1);
1359
      $themes = variable_get('path_breadcrumbs_internal_render_themes', array());
1360
      $path_breadcrumbs = ($internal_render && (empty($themes) || in_array($theme, $themes)));
1361
    }
1362
  }
1363

    
1364
  return $path_breadcrumbs;
1365
}
1366

    
1367
/**
1368
 * @} End of "defgroup subtheme_helper_functions".
1369
 */