Version 1
[yaffs-website] / web / themes / contrib / bootstrap / src / Bootstrap.php
1 <?php
2 /**
3  * @file
4  * Contains \Drupal\bootstrap\Bootstrap.
5  */
6
7 namespace Drupal\bootstrap;
8
9 use Drupal\bootstrap\Plugin\AlterManager;
10 use Drupal\bootstrap\Plugin\FormManager;
11 use Drupal\bootstrap\Plugin\PreprocessManager;
12 use Drupal\bootstrap\Utility\Element;
13 use Drupal\bootstrap\Utility\Unicode;
14 use Drupal\Component\Utility\Html;
15 use Drupal\Component\Utility\NestedArray;
16 use Drupal\Core\Extension\ThemeHandlerInterface;
17 use Drupal\Core\Form\FormStateInterface;
18
19 /**
20  * The primary class for the Drupal Bootstrap base theme.
21  *
22  * Provides many helper methods.
23  *
24  * @ingroup utility
25  */
26 class Bootstrap {
27
28   /**
29    * Tag used to invalidate caches.
30    *
31    * @var string
32    */
33   const CACHE_TAG = 'theme_registry';
34
35   /**
36    * Append a callback.
37    *
38    * @var int
39    */
40   const CALLBACK_APPEND = 1;
41
42   /**
43    * Prepend a callback.
44    *
45    * @var int
46    */
47   const CALLBACK_PREPEND = 2;
48
49   /**
50    * Replace a callback or append it if not found.
51    *
52    * @var int
53    */
54   const CALLBACK_REPLACE_APPEND = 3;
55
56   /**
57    * Replace a callback or prepend it if not found.
58    *
59    * @var int
60    */
61   const CALLBACK_REPLACE_PREPEND = 4;
62
63   /**
64    * The current supported Bootstrap Framework version.
65    *
66    * @var string
67    */
68   const FRAMEWORK_VERSION = '3.3.7';
69
70   /**
71    * The Bootstrap Framework documentation site.
72    *
73    * @var string
74    */
75   const FRAMEWORK_HOMEPAGE = 'http://getbootstrap.com';
76
77   /**
78    * The Bootstrap Framework repository.
79    *
80    * @var string
81    */
82   const FRAMEWORK_REPOSITORY = 'https://github.com/twbs/bootstrap';
83
84   /**
85    * The project branch.
86    *
87    * @var string
88    */
89   const PROJECT_BRANCH = '8.x-3.x';
90
91   /**
92    * The Drupal Bootstrap documentation site.
93    *
94    * @var string
95    */
96   const PROJECT_DOCUMENTATION = 'http://drupal-bootstrap.org';
97
98   /**
99    * The Drupal Bootstrap project page.
100    *
101    * @var string
102    */
103   const PROJECT_PAGE = 'https://www.drupal.org/project/bootstrap';
104
105   /**
106    * Adds a callback to an array.
107    *
108    * @param array $callbacks
109    *   An array of callbacks to add the callback to, passed by reference.
110    * @param array|string $callback
111    *   The callback to add.
112    * @param array|string $replace
113    *   If specified, the callback will instead replace the specified value
114    *   instead of being appended to the $callbacks array.
115    * @param int $action
116    *   Flag that determines how to add the callback to the array.
117    *
118    * @return bool
119    *   TRUE if the callback was added, FALSE if $replace was specified but its
120    *   callback could be found in the list of callbacks.
121    */
122   public static function addCallback(array &$callbacks, $callback, $replace = NULL, $action = Bootstrap::CALLBACK_APPEND) {
123     // Replace a callback.
124     if ($replace) {
125       // Iterate through the callbacks.
126       foreach ($callbacks as $key => $value) {
127         // Convert each callback and match the string values.
128         if (Unicode::convertCallback($value) === Unicode::convertCallback($replace)) {
129           $callbacks[$key] = $callback;
130           return TRUE;
131         }
132       }
133       // No match found and action shouldn't append or prepend.
134       if ($action !== self::CALLBACK_REPLACE_APPEND || $action !== self::CALLBACK_REPLACE_PREPEND) {
135         return FALSE;
136       }
137     }
138
139     // Append or prepend the callback.
140     switch ($action) {
141       case self::CALLBACK_APPEND:
142       case self::CALLBACK_REPLACE_APPEND:
143         $callbacks[] = $callback;
144         return TRUE;
145
146       case self::CALLBACK_PREPEND:
147       case self::CALLBACK_REPLACE_PREPEND:
148         array_unshift($callbacks, $callback);
149         return TRUE;
150
151       default:
152         return FALSE;
153     }
154   }
155
156   /**
157    * Manages theme alter hooks as classes and allows sub-themes to sub-class.
158    *
159    * @param string $function
160    *   The procedural function name of the alter (e.g. __FUNCTION__).
161    * @param mixed $data
162    *   The variable that was passed to the hook_TYPE_alter() implementation to
163    *   be altered. The type of this variable depends on the value of the $type
164    *   argument. For example, when altering a 'form', $data will be a structured
165    *   array. When altering a 'profile', $data will be an object.
166    * @param mixed $context1
167    *   (optional) An additional variable that is passed by reference.
168    * @param mixed $context2
169    *   (optional) An additional variable that is passed by reference. If more
170    *   context needs to be provided to implementations, then this should be an
171    *   associative array as described above.
172    */
173   public static function alter($function, &$data, &$context1 = NULL, &$context2 = NULL) {
174     static $theme;
175     if (!isset($theme)) {
176       $theme = self::getTheme();
177     }
178
179     // Immediately return if the active theme is not Bootstrap based.
180     if (!$theme->isBootstrap()) {
181       return;
182     }
183
184     // Extract the alter hook name.
185     $hook = Unicode::extractHook($function, 'alter');
186
187     // Handle form alters as a separate plugin.
188     if (strpos($hook, 'form') === 0 && $context1 instanceof FormStateInterface) {
189       $form_state = $context1;
190       $form_id = $context2;
191
192       // Due to a core bug that affects admin themes, we should not double
193       // process the "system_theme_settings" form twice in the global
194       // hook_form_alter() invocation.
195       // @see https://drupal.org/node/943212
196       if ($form_id === 'system_theme_settings') {
197         return;
198       }
199
200       // Keep track of the form identifiers.
201       $ids = [];
202
203       // Get the build data.
204       $build_info = $form_state->getBuildInfo();
205
206       // Extract the base_form_id.
207       $base_form_id = !empty($build_info['base_form_id']) ? $build_info['base_form_id'] : FALSE;
208       if ($base_form_id) {
209         $ids[] = $base_form_id;
210       }
211
212       // If there was no provided form identifier, extract it.
213       if (!$form_id) {
214         $form_id = !empty($build_info['form_id']) ? $build_info['form_id'] : Unicode::extractHook($function, 'alter', 'form');
215       }
216       if ($form_id) {
217         $ids[] = $form_id;
218       }
219
220       // Retrieve a list of form definitions.
221       $form_manager = new FormManager($theme);
222
223       // Iterate over each form identifier and look for a possible plugin.
224       foreach ($ids as $id) {
225         /** @var \Drupal\bootstrap\Plugin\Form\FormInterface $form */
226         if ($form_manager->hasDefinition($id) && ($form = $form_manager->createInstance($id, ['theme' => $theme]))) {
227           $data['#submit'][] = [get_class($form), 'submitForm'];
228           $data['#validate'][] = [get_class($form), 'validateForm'];
229           $form->alterForm($data, $form_state, $form_id);
230         }
231       }
232     }
233     // Process hook alter normally.
234     else {
235       // Retrieve a list of alter definitions.
236       $alter_manager = new AlterManager($theme);
237
238       /** @var \Drupal\bootstrap\Plugin\Alter\AlterInterface $class */
239       if ($alter_manager->hasDefinition($hook) && ($class = $alter_manager->createInstance($hook, ['theme' => $theme]))) {
240         $class->alter($data, $context1, $context2);
241       }
242     }
243   }
244
245   /**
246    * Returns a documentation search URL for a given query.
247    *
248    * @param string $query
249    *   The query to search for.
250    *
251    * @return string
252    *   The complete URL to the documentation site.
253    */
254   public static function apiSearchUrl($query = '') {
255     return self::PROJECT_DOCUMENTATION . '/api/bootstrap/' . self::PROJECT_BRANCH . '/search/' . Html::escape($query);
256   }
257
258   /**
259    * Returns the autoload fix include path.
260    *
261    * This method assists class based callbacks that normally do not work.
262    *
263    * If you notice that your class based callback is never invoked, you may try
264    * using this helper method as an "include" or "file" for your callback, if
265    * the callback metadata supports such an option.
266    *
267    * Depending on when or where a callback is invoked during a request, such as
268    * an ajax or batch request, the theme handler may not yet be fully
269    * initialized.
270    *
271    * Typically there is little that can be done about this "issue" from core.
272    * It must balance the appropriate level that should be bootstrapped along
273    * with common functionality. Cross-request class based callbacks are not
274    * common in themes.
275    *
276    * When this file is included, it will attempt to jump start this process.
277    *
278    * Please keep in mind, that it is merely an attempt and does not guarantee
279    * that it will actually work. If it does not appear to work, do not use it.
280    *
281    * @see \Drupal\Core\Extension\ThemeHandler::listInfo
282    * @see \Drupal\Core\Extension\ThemeHandler::systemThemeList
283    * @see system_list
284    * @see system_register()
285    * @see drupal_classloader_register()
286    *
287    * @return string
288    *   The autoload fix include path, relative to Drupal root.
289    */
290   public static function autoloadFixInclude() {
291     return static::getTheme('bootstrap')->getPath() . '/autoload-fix.php';
292   }
293
294   /**
295    * Matches a Bootstrap class based on a string value.
296    *
297    * @param string|array $value
298    *   The string to match against to determine the class. Passed by reference
299    *   in case it is a render array that needs to be rendered and typecast.
300    * @param string $default
301    *   The default class to return if no match is found.
302    *
303    * @return string
304    *   The Bootstrap class matched against the value of $haystack or $default
305    *   if no match could be made.
306    */
307   public static function cssClassFromString(&$value, $default = '') {
308     static $lang;
309     if (!isset($lang)) {
310       $lang = \Drupal::languageManager()->getCurrentLanguage()->getId();
311     }
312
313     $theme = static::getTheme();
314     $texts = $theme->getCache('cssClassFromString', [$lang]);
315
316     // Ensure it's a string value that was passed.
317     $string = static::toString($value);
318
319     if ($texts->isEmpty()) {
320       $data = [
321         // Text that match these specific strings are checked first.
322         'matches' => [
323           // Primary class.
324           t('Download feature')->render()   => 'primary',
325
326           // Success class.
327           t('Add effect')->render()         => 'success',
328           t('Add and configure')->render()  => 'success',
329           t('Save configuration')->render() => 'success',
330           t('Install and set as default')->render() => 'success',
331
332           // Info class.
333           t('Save and add')->render()       => 'info',
334           t('Add another item')->render()   => 'info',
335           t('Update style')->render()       => 'info',
336         ],
337
338         // Text containing these words anywhere in the string are checked last.
339         'contains' => [
340           // Primary class.
341           t('Confirm')->render()            => 'primary',
342           t('Filter')->render()             => 'primary',
343           t('Log in')->render()             => 'primary',
344           t('Submit')->render()             => 'primary',
345           t('Search')->render()             => 'primary',
346           t('Settings')->render()           => 'primary',
347           t('Upload')->render()             => 'primary',
348
349           // Danger class.
350           t('Delete')->render()             => 'danger',
351           t('Remove')->render()             => 'danger',
352           t('Uninstall')->render()          => 'danger',
353
354           // Success class.
355           t('Add')->render()                => 'success',
356           t('Create')->render()             => 'success',
357           t('Install')->render()            => 'success',
358           t('Save')->render()               => 'success',
359           t('Write')->render()              => 'success',
360
361           // Warning class.
362           t('Export')->render()             => 'warning',
363           t('Import')->render()             => 'warning',
364           t('Restore')->render()            => 'warning',
365           t('Rebuild')->render()            => 'warning',
366
367           // Info class.
368           t('Apply')->render()              => 'info',
369           t('Update')->render()             => 'info',
370         ],
371       ];
372
373       // Allow sub-themes to alter this array of patterns.
374       /** @var \Drupal\Core\Theme\ThemeManager $theme_manager */
375       $theme_manager = \Drupal::service('theme.manager');
376       $theme_manager->alter('bootstrap_colorize_text', $data);
377
378       $texts->setMultiple($data);
379     }
380
381     // Iterate over the array.
382     foreach ($texts as $pattern => $strings) {
383       foreach ($strings as $text => $class) {
384         switch ($pattern) {
385           case 'matches':
386             if ($string === $text) {
387               return $class;
388             }
389             break;
390
391           case 'contains':
392             if (strpos(Unicode::strtolower($string), Unicode::strtolower($text)) !== FALSE) {
393               return $class;
394             }
395             break;
396         }
397       }
398     }
399
400     // Return the default if nothing was matched.
401     return $default;
402   }
403
404   /**
405    * Logs and displays a warning about a deprecated function/method being used.
406    */
407   public static function deprecated() {
408     // Log backtrace.
409     $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
410     \Drupal::logger('bootstrap')->warning('<pre><code>' . print_r($backtrace, TRUE) . '</code></pre>');
411
412     if (!self::getTheme()->getSetting('suppress_deprecated_warnings')) {
413       return;
414     }
415
416     // Extrapolate the caller.
417     $caller = $backtrace[1];
418     $class = '';
419     if (isset($caller['class'])) {
420       $parts = explode('\\', $caller['class']);
421       $class = array_pop($parts) . '::';
422     }
423     drupal_set_message(t('The following function(s) or method(s) have been deprecated, please check the logs for a more detailed backtrace on where these are being invoked. Click on the function or method link to search the documentation site for a possible replacement or solution.'), 'warning');
424     drupal_set_message(t('<a href=":url" target="_blank">@title</a>.', [
425       ':url' => self::apiSearchUrl($class . $caller['function']),
426       '@title' => ($class ? $caller['class'] . $caller['type'] : '') . $caller['function'] . '()',
427     ]), 'warning');
428   }
429
430   /**
431    * Provides additional variables to be used in elements and templates.
432    *
433    * @return array
434    *   An associative array containing key/default value pairs.
435    */
436   public static function extraVariables() {
437     return [
438       // @see https://drupal.org/node/2035055
439       'context' => [],
440
441       // @see https://drupal.org/node/2219965
442       'icon' => NULL,
443       'icon_position' => 'before',
444       'icon_only' => FALSE,
445     ];
446   }
447
448   /**
449    * Retrieves a theme instance of \Drupal\bootstrap.
450    *
451    * @param string $name
452    *   The machine name of a theme. If omitted, the active theme will be used.
453    * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
454    *   The theme handler object.
455    *
456    * @return \Drupal\bootstrap\Theme
457    *   A theme object.
458    */
459   public static function getTheme($name = NULL, ThemeHandlerInterface $theme_handler = NULL) {
460     // Immediately return if theme passed is already instantiated.
461     if ($name instanceof Theme) {
462       return $name;
463     }
464
465     static $themes = [];
466     static $active_theme;
467     if (!isset($active_theme)) {
468       $active_theme = \Drupal::theme()->getActiveTheme()->getName();
469     }
470     if (!isset($name)) {
471       $name = $active_theme;
472     }
473
474     if (!isset($theme_handler)) {
475       $theme_handler = self::getThemeHandler();
476     }
477
478     if (!isset($themes[$name])) {
479       $themes[$name] = new Theme($theme_handler->getTheme($name), $theme_handler);
480     }
481
482     return $themes[$name];
483   }
484
485   /**
486    * Retrieves the theme handler instance.
487    *
488    * @return \Drupal\Core\Extension\ThemeHandlerInterface
489    *   The theme handler instance.
490    */
491   public static function getThemeHandler() {
492     static $theme_handler;
493     if (!isset($theme_handler)) {
494       $theme_handler = \Drupal::service('theme_handler');
495     }
496     return $theme_handler;
497   }
498
499   /**
500    * Returns the theme hook definition information.
501    *
502    * This base-theme's custom theme hook implementations. Never define "path"
503    * as this is automatically detected and added.
504    *
505    * @see \Drupal\bootstrap\Plugin\Alter\ThemeRegistry::alter()
506    * @see bootstrap_theme_registry_alter()
507    * @see bootstrap_theme()
508    * @see hook_theme()
509    */
510   public static function getThemeHooks() {
511     $hooks['bootstrap_carousel'] = [
512       'variables' => [
513         'attributes' => [],
514         'controls' => TRUE,
515         'id' => NULL,
516         'indicators' => TRUE,
517         'interval' => 5000,
518         'pause' => 'hover',
519         'slides' => [],
520         'start_index' => 0,
521         'wrap' => TRUE,
522       ],
523     ];
524
525     $hooks['bootstrap_dropdown'] = [
526       'variables' => [
527         'alignment' => 'down',
528         'attributes' => [],
529         'items' => [],
530         'split' => FALSE,
531         'toggle' => NULL,
532       ],
533     ];
534
535     $hooks['bootstrap_modal'] = [
536       'variables' => [
537         'attributes' => [],
538         'body' => '',
539         'body_attributes' => [],
540         'close_button' => TRUE,
541         'content_attributes' => [],
542         'description' => NULL,
543         'description_display' => 'before',
544         'dialog_attributes' => [],
545         'footer' => '',
546         'footer_attributes' => [],
547         'header_attributes' => [],
548         'id' => NULL,
549         'size' => NULL,
550         'title' => '',
551         'title_attributes' => [],
552       ],
553     ];
554
555     $hooks['bootstrap_panel'] = [
556       'variables' => [
557         'attributes' => [],
558         'body' => [],
559         'body_attributes' => [],
560         'collapsible' => FALSE,
561         'collapsed' => FALSE,
562         'description' => NULL,
563         'description_display' => 'before',
564         'footer' => NULL,
565         'footer_attributes' => [],
566         'heading' => NULL,
567         'heading_attributes' => [],
568         'id' => NULL,
569         'panel_type' => 'default',
570       ],
571     ];
572
573     return $hooks;
574   }
575
576   /**
577    * Returns a specific Bootstrap Glyphicon.
578    *
579    * @param string $name
580    *   The icon name, minus the "glyphicon-" prefix.
581    * @param array $default
582    *   (Optional) The default render array to use if $name is not available.
583    *
584    * @return array
585    *   The render containing the icon defined by $name, $default value if
586    *   icon does not exist or returns NULL if no icon could be rendered.
587    */
588   public static function glyphicon($name, $default = []) {
589     $icon = [];
590
591     // Ensure the icon specified is a valid Bootstrap Glyphicon.
592     // @todo Supply a specific version to _bootstrap_glyphicons() when Icon API
593     // supports versioning.
594     if (self::getTheme()->hasGlyphicons() && in_array($name, self::glyphicons())) {
595       // Attempt to use the Icon API module, if enabled and it generates output.
596       if (\Drupal::moduleHandler()->moduleExists('icon')) {
597         $icon = [
598           '#type' => 'icon',
599           '#bundle' => 'bootstrap',
600           '#icon' => 'glyphicon-' . $name,
601         ];
602       }
603       else {
604         $icon = [
605           '#type' => 'html_tag',
606           '#tag' => 'span',
607           '#value' => '',
608           '#attributes' => [
609             'class' => ['icon', 'glyphicon', 'glyphicon-' . $name],
610             'aria-hidden' => 'true',
611           ],
612         ];
613       }
614     }
615
616     return $icon ?: $default;
617   }
618
619   /**
620    * Matches a Bootstrap Glyphicon based on a string value.
621    *
622    * @param string $value
623    *   The string to match against to determine the icon. Passed by reference
624    *   in case it is a render array that needs to be rendered and typecast.
625    * @param array $default
626    *   The default render array to return if no match is found.
627    *
628    * @return string
629    *   The Bootstrap icon matched against the value of $haystack or $default if
630    *   no match could be made.
631    */
632   public static function glyphiconFromString(&$value, $default = []) {
633     static $lang;
634     if (!isset($lang)) {
635       $lang = \Drupal::languageManager()->getCurrentLanguage()->getId();
636     }
637
638     $theme = static::getTheme();
639     $texts = $theme->getCache('glyphiconFromString', [$lang]);
640
641     // Ensure it's a string value that was passed.
642     $string = static::toString($value);
643
644     if ($texts->isEmpty()) {
645       $data = [
646         // Text that match these specific strings are checked first.
647         'matches' => [],
648
649         // Text containing these words anywhere in the string are checked last.
650         'contains' => [
651           t('Manage')->render()     => 'cog',
652           t('Configure')->render()  => 'cog',
653           t('Settings')->render()   => 'cog',
654           t('Download')->render()   => 'download',
655           t('Export')->render()     => 'export',
656           t('Filter')->render()     => 'filter',
657           t('Import')->render()     => 'import',
658           t('Save')->render()       => 'ok',
659           t('Update')->render()     => 'ok',
660           t('Edit')->render()       => 'pencil',
661           t('Uninstall')->render()  => 'trash',
662           t('Install')->render()    => 'plus',
663           t('Write')->render()      => 'plus',
664           t('Cancel')->render()     => 'remove',
665           t('Delete')->render()     => 'trash',
666           t('Remove')->render()     => 'trash',
667           t('Search')->render()     => 'search',
668           t('Upload')->render()     => 'upload',
669           t('Preview')->render()    => 'eye-open',
670           t('Log in')->render()     => 'log-in',
671         ],
672       ];
673
674       // Allow sub-themes to alter this array of patterns.
675       /** @var \Drupal\Core\Theme\ThemeManager $theme_manager */
676       $theme_manager = \Drupal::service('theme.manager');
677       $theme_manager->alter('bootstrap_iconize_text', $data);
678
679       $texts->setMultiple($data);
680     }
681
682     // Iterate over the array.
683     foreach ($texts as $pattern => $strings) {
684       foreach ($strings as $text => $icon) {
685         switch ($pattern) {
686           case 'matches':
687             if ($string === $text) {
688               return self::glyphicon($icon, $default);
689             }
690             break;
691
692           case 'contains':
693             if (strpos(Unicode::strtolower($string), Unicode::strtolower($text)) !== FALSE) {
694               return self::glyphicon($icon, $default);
695             }
696             break;
697         }
698       }
699     }
700
701     // Return a default icon if nothing was matched.
702     return $default;
703   }
704
705   /**
706    * Returns a list of available Bootstrap Framework Glyphicons.
707    *
708    * @param string $version
709    *   The specific version of glyphicons to return. If not set, the latest
710    *   BOOTSTRAP_VERSION will be used.
711    *
712    * @return array
713    *   An associative array of icons keyed by their classes.
714    */
715   public static function glyphicons($version = NULL) {
716     static $versions;
717     if (!isset($versions)) {
718       $versions = [];
719       $versions['3.0.0'] = [
720         // Class => Name.
721         'glyphicon-adjust' => 'adjust',
722         'glyphicon-align-center' => 'align-center',
723         'glyphicon-align-justify' => 'align-justify',
724         'glyphicon-align-left' => 'align-left',
725         'glyphicon-align-right' => 'align-right',
726         'glyphicon-arrow-down' => 'arrow-down',
727         'glyphicon-arrow-left' => 'arrow-left',
728         'glyphicon-arrow-right' => 'arrow-right',
729         'glyphicon-arrow-up' => 'arrow-up',
730         'glyphicon-asterisk' => 'asterisk',
731         'glyphicon-backward' => 'backward',
732         'glyphicon-ban-circle' => 'ban-circle',
733         'glyphicon-barcode' => 'barcode',
734         'glyphicon-bell' => 'bell',
735         'glyphicon-bold' => 'bold',
736         'glyphicon-book' => 'book',
737         'glyphicon-bookmark' => 'bookmark',
738         'glyphicon-briefcase' => 'briefcase',
739         'glyphicon-bullhorn' => 'bullhorn',
740         'glyphicon-calendar' => 'calendar',
741         'glyphicon-camera' => 'camera',
742         'glyphicon-certificate' => 'certificate',
743         'glyphicon-check' => 'check',
744         'glyphicon-chevron-down' => 'chevron-down',
745         'glyphicon-chevron-left' => 'chevron-left',
746         'glyphicon-chevron-right' => 'chevron-right',
747         'glyphicon-chevron-up' => 'chevron-up',
748         'glyphicon-circle-arrow-down' => 'circle-arrow-down',
749         'glyphicon-circle-arrow-left' => 'circle-arrow-left',
750         'glyphicon-circle-arrow-right' => 'circle-arrow-right',
751         'glyphicon-circle-arrow-up' => 'circle-arrow-up',
752         'glyphicon-cloud' => 'cloud',
753         'glyphicon-cloud-download' => 'cloud-download',
754         'glyphicon-cloud-upload' => 'cloud-upload',
755         'glyphicon-cog' => 'cog',
756         'glyphicon-collapse-down' => 'collapse-down',
757         'glyphicon-collapse-up' => 'collapse-up',
758         'glyphicon-comment' => 'comment',
759         'glyphicon-compressed' => 'compressed',
760         'glyphicon-copyright-mark' => 'copyright-mark',
761         'glyphicon-credit-card' => 'credit-card',
762         'glyphicon-cutlery' => 'cutlery',
763         'glyphicon-dashboard' => 'dashboard',
764         'glyphicon-download' => 'download',
765         'glyphicon-download-alt' => 'download-alt',
766         'glyphicon-earphone' => 'earphone',
767         'glyphicon-edit' => 'edit',
768         'glyphicon-eject' => 'eject',
769         'glyphicon-envelope' => 'envelope',
770         'glyphicon-euro' => 'euro',
771         'glyphicon-exclamation-sign' => 'exclamation-sign',
772         'glyphicon-expand' => 'expand',
773         'glyphicon-export' => 'export',
774         'glyphicon-eye-close' => 'eye-close',
775         'glyphicon-eye-open' => 'eye-open',
776         'glyphicon-facetime-video' => 'facetime-video',
777         'glyphicon-fast-backward' => 'fast-backward',
778         'glyphicon-fast-forward' => 'fast-forward',
779         'glyphicon-file' => 'file',
780         'glyphicon-film' => 'film',
781         'glyphicon-filter' => 'filter',
782         'glyphicon-fire' => 'fire',
783         'glyphicon-flag' => 'flag',
784         'glyphicon-flash' => 'flash',
785         'glyphicon-floppy-disk' => 'floppy-disk',
786         'glyphicon-floppy-open' => 'floppy-open',
787         'glyphicon-floppy-remove' => 'floppy-remove',
788         'glyphicon-floppy-save' => 'floppy-save',
789         'glyphicon-floppy-saved' => 'floppy-saved',
790         'glyphicon-folder-close' => 'folder-close',
791         'glyphicon-folder-open' => 'folder-open',
792         'glyphicon-font' => 'font',
793         'glyphicon-forward' => 'forward',
794         'glyphicon-fullscreen' => 'fullscreen',
795         'glyphicon-gbp' => 'gbp',
796         'glyphicon-gift' => 'gift',
797         'glyphicon-glass' => 'glass',
798         'glyphicon-globe' => 'globe',
799         'glyphicon-hand-down' => 'hand-down',
800         'glyphicon-hand-left' => 'hand-left',
801         'glyphicon-hand-right' => 'hand-right',
802         'glyphicon-hand-up' => 'hand-up',
803         'glyphicon-hd-video' => 'hd-video',
804         'glyphicon-hdd' => 'hdd',
805         'glyphicon-header' => 'header',
806         'glyphicon-headphones' => 'headphones',
807         'glyphicon-heart' => 'heart',
808         'glyphicon-heart-empty' => 'heart-empty',
809         'glyphicon-home' => 'home',
810         'glyphicon-import' => 'import',
811         'glyphicon-inbox' => 'inbox',
812         'glyphicon-indent-left' => 'indent-left',
813         'glyphicon-indent-right' => 'indent-right',
814         'glyphicon-info-sign' => 'info-sign',
815         'glyphicon-italic' => 'italic',
816         'glyphicon-leaf' => 'leaf',
817         'glyphicon-link' => 'link',
818         'glyphicon-list' => 'list',
819         'glyphicon-list-alt' => 'list-alt',
820         'glyphicon-lock' => 'lock',
821         'glyphicon-log-in' => 'log-in',
822         'glyphicon-log-out' => 'log-out',
823         'glyphicon-magnet' => 'magnet',
824         'glyphicon-map-marker' => 'map-marker',
825         'glyphicon-minus' => 'minus',
826         'glyphicon-minus-sign' => 'minus-sign',
827         'glyphicon-move' => 'move',
828         'glyphicon-music' => 'music',
829         'glyphicon-new-window' => 'new-window',
830         'glyphicon-off' => 'off',
831         'glyphicon-ok' => 'ok',
832         'glyphicon-ok-circle' => 'ok-circle',
833         'glyphicon-ok-sign' => 'ok-sign',
834         'glyphicon-open' => 'open',
835         'glyphicon-paperclip' => 'paperclip',
836         'glyphicon-pause' => 'pause',
837         'glyphicon-pencil' => 'pencil',
838         'glyphicon-phone' => 'phone',
839         'glyphicon-phone-alt' => 'phone-alt',
840         'glyphicon-picture' => 'picture',
841         'glyphicon-plane' => 'plane',
842         'glyphicon-play' => 'play',
843         'glyphicon-play-circle' => 'play-circle',
844         'glyphicon-plus' => 'plus',
845         'glyphicon-plus-sign' => 'plus-sign',
846         'glyphicon-print' => 'print',
847         'glyphicon-pushpin' => 'pushpin',
848         'glyphicon-qrcode' => 'qrcode',
849         'glyphicon-question-sign' => 'question-sign',
850         'glyphicon-random' => 'random',
851         'glyphicon-record' => 'record',
852         'glyphicon-refresh' => 'refresh',
853         'glyphicon-registration-mark' => 'registration-mark',
854         'glyphicon-remove' => 'remove',
855         'glyphicon-remove-circle' => 'remove-circle',
856         'glyphicon-remove-sign' => 'remove-sign',
857         'glyphicon-repeat' => 'repeat',
858         'glyphicon-resize-full' => 'resize-full',
859         'glyphicon-resize-horizontal' => 'resize-horizontal',
860         'glyphicon-resize-small' => 'resize-small',
861         'glyphicon-resize-vertical' => 'resize-vertical',
862         'glyphicon-retweet' => 'retweet',
863         'glyphicon-road' => 'road',
864         'glyphicon-save' => 'save',
865         'glyphicon-saved' => 'saved',
866         'glyphicon-screenshot' => 'screenshot',
867         'glyphicon-sd-video' => 'sd-video',
868         'glyphicon-search' => 'search',
869         'glyphicon-send' => 'send',
870         'glyphicon-share' => 'share',
871         'glyphicon-share-alt' => 'share-alt',
872         'glyphicon-shopping-cart' => 'shopping-cart',
873         'glyphicon-signal' => 'signal',
874         'glyphicon-sort' => 'sort',
875         'glyphicon-sort-by-alphabet' => 'sort-by-alphabet',
876         'glyphicon-sort-by-alphabet-alt' => 'sort-by-alphabet-alt',
877         'glyphicon-sort-by-attributes' => 'sort-by-attributes',
878         'glyphicon-sort-by-attributes-alt' => 'sort-by-attributes-alt',
879         'glyphicon-sort-by-order' => 'sort-by-order',
880         'glyphicon-sort-by-order-alt' => 'sort-by-order-alt',
881         'glyphicon-sound-5-1' => 'sound-5-1',
882         'glyphicon-sound-6-1' => 'sound-6-1',
883         'glyphicon-sound-7-1' => 'sound-7-1',
884         'glyphicon-sound-dolby' => 'sound-dolby',
885         'glyphicon-sound-stereo' => 'sound-stereo',
886         'glyphicon-star' => 'star',
887         'glyphicon-star-empty' => 'star-empty',
888         'glyphicon-stats' => 'stats',
889         'glyphicon-step-backward' => 'step-backward',
890         'glyphicon-step-forward' => 'step-forward',
891         'glyphicon-stop' => 'stop',
892         'glyphicon-subtitles' => 'subtitles',
893         'glyphicon-tag' => 'tag',
894         'glyphicon-tags' => 'tags',
895         'glyphicon-tasks' => 'tasks',
896         'glyphicon-text-height' => 'text-height',
897         'glyphicon-text-width' => 'text-width',
898         'glyphicon-th' => 'th',
899         'glyphicon-th-large' => 'th-large',
900         'glyphicon-th-list' => 'th-list',
901         'glyphicon-thumbs-down' => 'thumbs-down',
902         'glyphicon-thumbs-up' => 'thumbs-up',
903         'glyphicon-time' => 'time',
904         'glyphicon-tint' => 'tint',
905         'glyphicon-tower' => 'tower',
906         'glyphicon-transfer' => 'transfer',
907         'glyphicon-trash' => 'trash',
908         'glyphicon-tree-conifer' => 'tree-conifer',
909         'glyphicon-tree-deciduous' => 'tree-deciduous',
910         'glyphicon-unchecked' => 'unchecked',
911         'glyphicon-upload' => 'upload',
912         'glyphicon-usd' => 'usd',
913         'glyphicon-user' => 'user',
914         'glyphicon-volume-down' => 'volume-down',
915         'glyphicon-volume-off' => 'volume-off',
916         'glyphicon-volume-up' => 'volume-up',
917         'glyphicon-warning-sign' => 'warning-sign',
918         'glyphicon-wrench' => 'wrench',
919         'glyphicon-zoom-in' => 'zoom-in',
920         'glyphicon-zoom-out' => 'zoom-out',
921       ];
922       $versions['3.0.1'] = $versions['3.0.0'];
923       $versions['3.0.2'] = $versions['3.0.1'];
924       $versions['3.0.3'] = $versions['3.0.2'];
925       $versions['3.1.0'] = $versions['3.0.3'];
926       $versions['3.1.1'] = $versions['3.1.0'];
927       $versions['3.2.0'] = $versions['3.1.1'];
928       $versions['3.3.0'] = array_merge($versions['3.2.0'], [
929         'glyphicon-eur' => 'eur',
930       ]);
931       $versions['3.3.1'] = $versions['3.3.0'];
932       $versions['3.3.2'] = array_merge($versions['3.3.1'], [
933         'glyphicon-alert' => 'alert',
934         'glyphicon-apple' => 'apple',
935         'glyphicon-baby-formula' => 'baby-formula',
936         'glyphicon-bed' => 'bed',
937         'glyphicon-bishop' => 'bishop',
938         'glyphicon-bitcoin' => 'bitcoin',
939         'glyphicon-blackboard' => 'blackboard',
940         'glyphicon-cd' => 'cd',
941         'glyphicon-console' => 'console',
942         'glyphicon-copy' => 'copy',
943         'glyphicon-duplicate' => 'duplicate',
944         'glyphicon-education' => 'education',
945         'glyphicon-equalizer' => 'equalizer',
946         'glyphicon-erase' => 'erase',
947         'glyphicon-grain' => 'grain',
948         'glyphicon-hourglass' => 'hourglass',
949         'glyphicon-ice-lolly' => 'ice-lolly',
950         'glyphicon-ice-lolly-tasted' => 'ice-lolly-tasted',
951         'glyphicon-king' => 'king',
952         'glyphicon-knight' => 'knight',
953         'glyphicon-lamp' => 'lamp',
954         'glyphicon-level-up' => 'level-up',
955         'glyphicon-menu-down' => 'menu-down',
956         'glyphicon-menu-hamburger' => 'menu-hamburger',
957         'glyphicon-menu-left' => 'menu-left',
958         'glyphicon-menu-right' => 'menu-right',
959         'glyphicon-menu-up' => 'menu-up',
960         'glyphicon-modal-window' => 'modal-window',
961         'glyphicon-object-align-bottom' => 'object-align-bottom',
962         'glyphicon-object-align-horizontal' => 'object-align-horizontal',
963         'glyphicon-object-align-left' => 'object-align-left',
964         'glyphicon-object-align-right' => 'object-align-right',
965         'glyphicon-object-align-top' => 'object-align-top',
966         'glyphicon-object-align-vertical' => 'object-align-vertical',
967         'glyphicon-oil' => 'oil',
968         'glyphicon-open-file' => 'open-file',
969         'glyphicon-option-horizontal' => 'option-horizontal',
970         'glyphicon-option-vertical' => 'option-vertical',
971         'glyphicon-paste' => 'paste',
972         'glyphicon-pawn' => 'pawn',
973         'glyphicon-piggy-bank' => 'piggy-bank',
974         'glyphicon-queen' => 'queen',
975         'glyphicon-ruble' => 'ruble',
976         'glyphicon-save-file' => 'save-file',
977         'glyphicon-scale' => 'scale',
978         'glyphicon-scissors' => 'scissors',
979         'glyphicon-subscript' => 'subscript',
980         'glyphicon-sunglasses' => 'sunglasses',
981         'glyphicon-superscript' => 'superscript',
982         'glyphicon-tent' => 'tent',
983         'glyphicon-text-background' => 'text-background',
984         'glyphicon-text-color' => 'text-color',
985         'glyphicon-text-size' => 'text-size',
986         'glyphicon-triangle-bottom' => 'triangle-bottom',
987         'glyphicon-triangle-left' => 'triangle-left',
988         'glyphicon-triangle-right' => 'triangle-right',
989         'glyphicon-triangle-top' => 'triangle-top',
990         'glyphicon-yen' => 'yen',
991       ]);
992       $versions['3.3.4'] = array_merge($versions['3.3.2'], [
993         'glyphicon-btc' => 'btc',
994         'glyphicon-jpy' => 'jpy',
995         'glyphicon-rub' => 'rub',
996         'glyphicon-xbt' => 'xbt',
997       ]);
998       $versions['3.3.5'] = $versions['3.3.4'];
999       $versions['3.3.6'] = $versions['3.3.5'];
1000       $versions['3.3.7'] = $versions['3.3.6'];
1001     }
1002
1003     // Return a specific versions icon set.
1004     if (isset($version) && isset($versions[$version])) {
1005       return $versions[$version];
1006     }
1007
1008     // Return the latest version.
1009     return $versions[self::FRAMEWORK_VERSION];
1010   }
1011
1012   /**
1013    * Determines if the "cache_context.url.path.is_front" service exists.
1014    *
1015    * @return bool
1016    *   TRUE or FALSE
1017    *
1018    * @see \Drupal\bootstrap\Bootstrap::isFront
1019    * @see \Drupal\bootstrap\Bootstrap::preprocess
1020    * @see https://www.drupal.org/node/2829588
1021    */
1022   public static function hasIsFrontCacheContext() {
1023     static $has_is_front_cache_context;
1024     if (!isset($has_is_front_cache_context)) {
1025       $has_is_front_cache_context = \Drupal::getContainer()->has('cache_context.url.path.is_front');
1026     }
1027     return $has_is_front_cache_context;
1028   }
1029
1030   /**
1031    * Initializes the active theme.
1032    */
1033   final public static function initialize() {
1034     static $initialized = FALSE;
1035     if (!$initialized) {
1036       // Initialize the active theme.
1037       $active_theme = self::getTheme();
1038
1039       // Include deprecated functions.
1040       foreach ($active_theme->getAncestry() as $ancestor) {
1041         if ($ancestor->getSetting('include_deprecated')) {
1042           $files = $ancestor->fileScan('/^deprecated\.php$/');
1043           if ($file = reset($files)) {
1044             $ancestor->includeOnce($file->uri, FALSE);
1045           }
1046         }
1047       }
1048
1049       $initialized = TRUE;
1050     }
1051   }
1052
1053   /**
1054    * Determines if the current path is the "front" page.
1055    *
1056    * *Note:* This method will not return `TRUE` if there is not a proper
1057    * "cache_context.url.path.is_front" service defined.
1058    *
1059    * *Note:* If using this method in preprocess/render array logic, the proper
1060    * #cache context must also be defined:
1061    *
1062    * ```php
1063    * $variables['#cache']['contexts'][] = 'url.path.is_front';
1064    * ```
1065    *
1066    * @return bool
1067    *   TRUE or FALSE
1068    *
1069    * @see \Drupal\bootstrap\Bootstrap::hasIsFrontCacheContext
1070    * @see \Drupal\bootstrap\Bootstrap::preprocess
1071    * @see https://www.drupal.org/node/2829588
1072    */
1073   public static function isFront() {
1074     static $is_front;
1075     if (!isset($is_front)) {
1076       try {
1077         $is_front = static::hasIsFrontCacheContext() ? \Drupal::service('path.matcher')->isFrontPage() : FALSE;
1078       }
1079       catch (\Exception $e) {
1080         $is_front = FALSE;
1081       }
1082     }
1083     return $is_front;
1084   }
1085
1086   /**
1087    * Preprocess theme hook variables.
1088    *
1089    * @param array $variables
1090    *   The variables array, passed by reference.
1091    * @param string $hook
1092    *   The name of the theme hook.
1093    * @param array $info
1094    *   The theme hook info.
1095    */
1096   public static function preprocess(array &$variables, $hook, array $info) {
1097     static $theme;
1098     if (!isset($theme)) {
1099       $theme = self::getTheme();
1100     }
1101     static $preprocess_manager;
1102     if (!isset($preprocess_manager)) {
1103       $preprocess_manager = new PreprocessManager($theme);
1104     }
1105
1106     // Adds a global "is_front" variable back to all templates.
1107     // @see https://www.drupal.org/node/2829585
1108     if (!isset($variables['is_front'])) {
1109       $variables['is_front'] = static::isFront();
1110       if (static::hasIsFrontCacheContext()) {
1111         $variables['#cache']['contexts'][] = 'url.path.is_front';
1112       }
1113     }
1114
1115     // Ensure that any default theme hook variables exist. Due to how theme
1116     // hook suggestion alters work, the variables provided are from the
1117     // original theme hook, not the suggestion.
1118     if (isset($info['variables'])) {
1119       $variables = NestedArray::mergeDeepArray([$info['variables'], $variables], TRUE);
1120     }
1121
1122     // Add active theme context.
1123     // @see https://www.drupal.org/node/2630870
1124     if (!isset($variables['theme'])) {
1125       $variables['theme'] = $theme->getInfo();
1126       $variables['theme']['dev'] = $theme->isDev();
1127       $variables['theme']['livereload'] = $theme->livereloadUrl();
1128       $variables['theme']['name'] = $theme->getName();
1129       $variables['theme']['path'] = $theme->getPath();
1130       $variables['theme']['title'] = $theme->getTitle();
1131       $variables['theme']['settings'] = $theme->settings()->get();
1132       $variables['theme']['has_glyphicons'] = $theme->hasGlyphicons();
1133       $variables['theme']['query_string'] = \Drupal::getContainer()->get('state')->get('system.css_js_query_string') ?: '0';
1134     }
1135
1136     // Invoke necessary preprocess plugin.
1137     if (isset($info['bootstrap preprocess'])) {
1138       if ($preprocess_manager->hasDefinition($info['bootstrap preprocess'])) {
1139         $class = $preprocess_manager->createInstance($info['bootstrap preprocess'], ['theme' => $theme]);
1140         /** @var \Drupal\bootstrap\Plugin\Preprocess\PreprocessInterface $class */
1141         $class->preprocess($variables, $hook, $info);
1142       }
1143     }
1144   }
1145
1146   /**
1147    * Ensures a value is typecast to a string, rendering an array if necessary.
1148    *
1149    * @param string|array $value
1150    *   The value to typecast, passed by reference.
1151    *
1152    * @return string
1153    *   The typecast string value.
1154    */
1155   public static function toString(&$value) {
1156     return (string) (Element::isRenderArray($value) ? Element::create($value)->renderPlain() : $value);
1157   }
1158
1159 }