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