theme.maintenance.inc 4.9 KB

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