file LibraryInfo.php

Namespace

Drupal\bootstrap\Plugin\Alter
  1. <?php
  2. namespace Drupal\bootstrap\Plugin\Alter;
  3. use Drupal\bootstrap\Plugin\PluginBase;
  4. /**
  5. * Implements hook_library_info_alter().
  6. *
  7. * @ingroup plugins_alter
  8. *
  9. * @BootstrapAlter("library_info")
  10. */
  11. class LibraryInfo extends PluginBase implements AlterInterface {
  12. /**
  13. * {@inheritdoc}
  14. */
  15. public function alter(&$libraries, &$extension = NULL, &$context2 = NULL) {
  16. $livereload = $this->theme->livereloadUrl();
  17. // Disable preprocess on all CSS/JS if "livereload" is enabled.
  18. if ($livereload) {
  19. $this->processLibrary($libraries, function (&$info, &$key, $type) {
  20. if ($type === 'css' || $type === 'js') {
  21. $info['preprocess'] = FALSE;
  22. }
  23. });
  24. }
  25. if ($extension === 'bootstrap') {
  26. // Alter the "livereload.js" placeholder with the correct URL.
  27. if ($livereload) {
  28. $libraries['livereload']['js'][$livereload] = $libraries['livereload']['js']['livereload.js'];
  29. unset($libraries['livereload']['js']['livereload.js']);
  30. }
  31. // Alter the framework library based on currently set CDN Provider.
  32. $this->theme->getCdnProvider()->alterFrameworkLibrary($libraries['framework']);
  33. // Add back deprecated library dependencies that are only available in D8.
  34. if (((int) substr(\Drupal::VERSION, 0, 1)) < 9) {
  35. $libraries['drupal.vertical-tabs']['dependencies'][] = 'core/matchmedia';
  36. }
  37. }
  38. // Core replacements.
  39. elseif ($extension === 'core') {
  40. // Replace core dialog/jQuery UI implementations with Bootstrap Modals.
  41. if ($this->theme->getSetting('modal_enabled')) {
  42. // Replace dependencies if using bridge so jQuery UI is not loaded
  43. // and remove dialog.jquery-ui.js since the dialog widget isn't loaded.
  44. if ($this->theme->getSetting('modal_jquery_ui_bridge')) {
  45. // Remove core's jquery.ui.dialog dependency.
  46. $key = array_search('core/jquery.ui.dialog', $libraries['drupal.dialog']['dependencies']);
  47. if ($key !== FALSE) {
  48. unset($libraries['drupal.dialog']['dependencies'][$key]);
  49. }
  50. // Remove core's dialog.jquery-ui.js.
  51. unset($libraries['drupal.dialog']['js']['misc/dialog/dialog.jquery-ui.js']);
  52. // Add the Modal jQuery UI Bridge.
  53. $libraries['drupal.dialog']['dependencies'][] = 'bootstrap/dialog';
  54. $libraries['drupal.dialog']['dependencies'][] = 'bootstrap/modal.jquery.ui.bridge';
  55. }
  56. // Otherwise, just append the modal.
  57. else {
  58. $libraries['drupal.dialog']['dependencies'][] = 'bootstrap/modal';
  59. $libraries['drupal.dialog']['dependencies'][] = 'bootstrap/dialog';
  60. }
  61. }
  62. }
  63. }
  64. /**
  65. * Processes library definitions.
  66. *
  67. * @param array $libraries
  68. * The libraries array, passed by reference.
  69. * @param callable $callback
  70. * The callback to perform processing on the library.
  71. */
  72. public function processLibrary(array &$libraries, callable $callback) {
  73. foreach ($libraries as &$library) {
  74. foreach ($library as $type => $definition) {
  75. if (is_array($definition)) {
  76. $modified = [];
  77. // CSS needs special handling since it contains grouping.
  78. if ($type === 'css') {
  79. foreach ($definition as $group => $files) {
  80. foreach ($files as $key => $info) {
  81. call_user_func_array($callback, [&$info, &$key, $type]);
  82. $modified[$group][$key] = $info;
  83. }
  84. }
  85. }
  86. else {
  87. foreach ($definition as $key => $info) {
  88. call_user_func_array($callback, [&$info, &$key, $type]);
  89. $modified[$key] = $info;
  90. }
  91. }
  92. $library[$type] = $modified;
  93. }
  94. }
  95. }
  96. }
  97. }

Classes

Name Description
LibraryInfo Implements hook_library_info_alter().