theme.maintenance.inc 4.8 KB

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