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