theme.maintenance.inc 4.85 KB
Newer Older
1 2 3 4 5 6 7
<?php

/**
 * @file
 * Theming for maintenance pages.
 */

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 15
 * Used for site installs, updates and when the site is in maintenance mode.
 * It also applies when the database is unavailable or bootstrap was not
16 17
 * complete. Seven is always used for the initial install and update
 * 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 22
  // If the theme is already set, assume the others are set too, and do nothing.
  if (\Drupal::theme()->hasActiveTheme()) {
23 24 25
    return;
  }

26 27 28 29 30
  require_once __DIR__ . '/theme.inc';
  require_once __DIR__ . '/common.inc';
  require_once __DIR__ . '/unicode.inc';
  require_once __DIR__ . '/file.inc';
  require_once __DIR__ . '/module.inc';
31
  require_once __DIR__ . '/database.inc';
32
  Unicode::check();
33 34 35

  // Install and update pages are treated differently to prevent theming overrides.
  if (defined('MAINTENANCE_MODE') && (MAINTENANCE_MODE == 'install' || MAINTENANCE_MODE == 'update')) {
36 37 38 39
    if (drupal_installation_attempted()) {
      $custom_theme = $GLOBALS['install_state']['theme'];
    }
    else {
40
      $custom_theme = Settings::get('maintenance_theme', 'seven');
41
    }
42 43
  }
  else {
44 45 46
    // Use the maintenance theme if specified, otherwise attempt to use the
    // default site theme.
    try {
47
      $custom_theme = Settings::get('maintenance_theme', '');
48 49
      if (!$custom_theme) {
        $config = \Drupal::config('system.theme');
50 51 52
        $custom_theme = $config->get('default');
      }
    }
53 54 55 56 57 58 59 60
    catch (\Exception $e) {
      // Whatever went wrong (often a database connection problem), we are
      // about to fall back to a sensible theme so there is no need for special
      // handling.
    }
    if (!$custom_theme) {
      // We have been unable to identify the configured theme, so fall back to
      // a safe default. Bartik is reasonably user friendly and fairly generic.
61 62
      $custom_theme = 'bartik';
    }
63 64
  }

65
  $themes = \Drupal::service('theme_handler')->listInfo();
66

67 68
  // If no themes are installed yet, or if the requested custom theme is not
  // installed, retrieve all available themes.
69 70
  /** @var \Drupal\Core\Theme\ThemeInitialization $theme_init */
  $theme_init = \Drupal::service('theme.initialization');
71
  $theme_handler = \Drupal::service('theme_handler');
72 73 74 75 76
  if (empty($themes) || !isset($themes[$custom_theme])) {
    $themes = $theme_handler->rebuildThemeData();
    $theme_handler->addTheme($themes[$custom_theme]);
  }

77 78 79 80 81
  // \Drupal\Core\Extension\ThemeHandlerInterface::listInfo() triggers a
  // \Drupal\Core\Extension\ModuleHandler::alter() in maintenance mode, but we
  // can't let themes alter the .info.yml data until we know a theme's base
  // themes. So don't set active theme until after
  // \Drupal\Core\Extension\ThemeHandlerInterface::listInfo() builds its cache.
82 83
  $theme = $custom_theme;

84
  // Find all our ancestor themes and put them in an array.
85 86 87 88 89
  // @todo This is just a workaround. Find a better way how to handle themes
  //   on maintenance pages, see https://www.drupal.org/node/2322619.
  // This code is basically a duplicate of
  // \Drupal\Core\Theme\ThemeInitialization::getActiveThemeByName.
  $base_themes = [];
90 91
  $ancestor = $theme;
  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 97
      $theme_handler->addTheme($themes[$ancestor]);
    }
98
  }
99
  \Drupal::theme()->setActiveTheme($theme_init->getActiveTheme($themes[$custom_theme], $base_themes));
100 101
  // Prime the theme registry.
  Drupal::service('theme.registry');
102 103
}

104
/**
105
 * Prepares variables for authorize.php operation report templates.
106
 *
107 108 109 110 111
 * This report displays the results of an operation run via authorize.php.
 *
 * Default template: authorize-report.html.twig.
 *
 * @param array $variables
112
 *   An associative array containing:
113 114
 *   - messages: An array of result messages.
 */
115 116 117 118 119
function template_preprocess_authorize_report(&$variables) {
  $messages = [];
  if (!empty($variables['messages'])) {
    foreach ($variables['messages'] as $heading => $logs) {
      $items = [];
120 121 122 123
      foreach ($logs as $number => $log_message) {
        if ($number === '#abort') {
          continue;
        }
124 125 126 127 128
        $class = 'authorize-results__' . ($log_message['success'] ? 'success' : 'failure');
        $items[] = [
          '#wrapper_attributes' => ['class' => [$class]],
          '#markup' => $log_message['message'],
        ];
129
      }
130
      $messages[] = [
131 132 133
        '#theme' => 'item_list',
        '#items' => $items,
        '#title' => $heading,
134
      ];
135 136
    }
  }
137
  $variables['messages'] = $messages;
138
}