theme.maintenance.inc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * @file
  4. * Theming for maintenance pages.
  5. */
  6. use Drupal\Component\Utility\Unicode;
  7. use Drupal\Core\Site\Settings;
  8. /**
  9. * Sets up the theming system for maintenance page.
  10. *
  11. * Used for site installs, updates and when the site is in maintenance mode.
  12. * It also applies when the database is unavailable or bootstrap was not
  13. * complete. Seven is always used for the initial install and update
  14. * operations. In other cases, Bartik is used, but this can be overridden by
  15. * setting a "maintenance_theme" key in the $settings variable in settings.php.
  16. */
  17. function _drupal_maintenance_theme() {
  18. // If the theme is already set, assume the others are set too, and do nothing.
  19. if (\Drupal::theme()->hasActiveTheme()) {
  20. return;
  21. }
  22. require_once __DIR__ . '/theme.inc';
  23. require_once __DIR__ . '/common.inc';
  24. require_once __DIR__ . '/unicode.inc';
  25. require_once __DIR__ . '/file.inc';
  26. require_once __DIR__ . '/module.inc';
  27. require_once __DIR__ . '/database.inc';
  28. Unicode::check();
  29. // Install and update pages are treated differently to prevent theming overrides.
  30. if (defined('MAINTENANCE_MODE') && (MAINTENANCE_MODE == 'install' || MAINTENANCE_MODE == 'update')) {
  31. if (drupal_installation_attempted()) {
  32. $custom_theme = $GLOBALS['install_state']['theme'];
  33. }
  34. else {
  35. $custom_theme = Settings::get('maintenance_theme', 'seven');
  36. }
  37. }
  38. else {
  39. // Use the maintenance theme if specified, otherwise attempt to use the
  40. // default site theme.
  41. try {
  42. $custom_theme = Settings::get('maintenance_theme', '');
  43. if (!$custom_theme) {
  44. $config = \Drupal::config('system.theme');
  45. $custom_theme = $config->get('default');
  46. }
  47. }
  48. catch (\Exception $e) {
  49. // Whatever went wrong (often a database connection problem), we are
  50. // about to fall back to a sensible theme so there is no need for special
  51. // handling.
  52. }
  53. if (!$custom_theme) {
  54. // We have been unable to identify the configured theme, so fall back to
  55. // a safe default. Bartik is reasonably user friendly and fairly generic.
  56. $custom_theme = 'bartik';
  57. }
  58. }
  59. $themes = \Drupal::service('theme_handler')->listInfo();
  60. // If no themes are installed yet, or if the requested custom theme is not
  61. // installed, retrieve all available themes.
  62. /** @var \Drupal\Core\Theme\ThemeInitialization $theme_init */
  63. $theme_init = \Drupal::service('theme.initialization');
  64. $theme_handler = \Drupal::service('theme_handler');
  65. if (empty($themes) || !isset($themes[$custom_theme])) {
  66. $themes = $theme_handler->rebuildThemeData();
  67. $theme_handler->addTheme($themes[$custom_theme]);
  68. }
  69. // \Drupal\Core\Extension\ThemeHandlerInterface::listInfo() triggers a
  70. // \Drupal\Core\Extension\ModuleHandler::alter() in maintenance mode, but we
  71. // can't let themes alter the .info.yml data until we know a theme's base
  72. // themes. So don't set active theme until after
  73. // \Drupal\Core\Extension\ThemeHandlerInterface::listInfo() builds its cache.
  74. $theme = $custom_theme;
  75. // Find all our ancestor themes and put them in an array.
  76. // @todo This is just a workaround. Find a better way how to handle themes
  77. // on maintenance pages, see https://www.drupal.org/node/2322619.
  78. // This code is basically a duplicate of
  79. // \Drupal\Core\Theme\ThemeInitialization::getActiveThemeByName.
  80. $base_themes = [];
  81. $ancestor = $theme;
  82. while ($ancestor && isset($themes[$ancestor]->base_theme)) {
  83. $base_themes[] = $themes[$themes[$ancestor]->base_theme];
  84. $ancestor = $themes[$ancestor]->base_theme;
  85. if ($ancestor) {
  86. // Ensure that the base theme is added and installed.
  87. $theme_handler->addTheme($themes[$ancestor]);
  88. }
  89. }
  90. \Drupal::theme()->setActiveTheme($theme_init->getActiveTheme($themes[$custom_theme], $base_themes));
  91. // Prime the theme registry.
  92. Drupal::service('theme.registry');
  93. }
  94. /**
  95. * Prepares variables for authorize.php operation report templates.
  96. *
  97. * This report displays the results of an operation run via authorize.php.
  98. *
  99. * Default template: authorize-report.html.twig.
  100. *
  101. * @param array $variables
  102. * An associative array containing:
  103. * - messages: An array of result messages.
  104. */
  105. function template_preprocess_authorize_report(&$variables) {
  106. $messages = [];
  107. if (!empty($variables['messages'])) {
  108. foreach ($variables['messages'] as $heading => $logs) {
  109. $items = [];
  110. foreach ($logs as $number => $log_message) {
  111. if ($number === '#abort') {
  112. continue;
  113. }
  114. $class = 'authorize-results__' . ($log_message['success'] ? 'success' : 'failure');
  115. $items[] = [
  116. '#wrapper_attributes' => ['class' => [$class]],
  117. '#markup' => $log_message['message'],
  118. ];
  119. }
  120. $messages[] = [
  121. '#theme' => 'item_list',
  122. '#items' => $items,
  123. '#title' => $heading,
  124. ];
  125. }
  126. }
  127. $variables['messages'] = $messages;
  128. }