summaryrefslogtreecommitdiffstatshomepage
path: root/core/lib/Drupal/Core/Render/BareHtmlPageRenderer.php
blob: 0c63ce2ac495046dbbf46c5553b30f2b8ec5170f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php

namespace Drupal\Core\Render;

use Drupal\Component\Utility\UrlHelper;

/**
 * Default bare HTML page renderer.
 */
class BareHtmlPageRenderer implements BareHtmlPageRendererInterface {

  /**
   * The renderer service.
   *
   * @var \Drupal\Core\Render\Renderer
   */
  protected $renderer;

  /**
   * The HTML response attachments processor service.
   *
   * @var \Drupal\Core\Render\AttachmentsResponseProcessorInterface
   */
  protected $htmlResponseAttachmentsProcessor;

  /**
   * Constructs a new BareHtmlPageRenderer.
   *
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer service.
   * @param \Drupal\Core\Render\AttachmentsResponseProcessorInterface $html_response_attachments_processor
   *   The HTML response attachments processor service.
   */
  public function __construct(RendererInterface $renderer, AttachmentsResponseProcessorInterface $html_response_attachments_processor) {
    $this->renderer = $renderer;
    $this->htmlResponseAttachmentsProcessor = $html_response_attachments_processor;
  }

  /**
   * {@inheritdoc}
   */
  public function renderBarePage(array $content, $title, $page_theme_property, array $page_additions = []) {
    $attributes = [
      'class' => [
        str_replace('_', '-', $page_theme_property),
      ],
    ];
    $html = [
      '#type' => 'html',
      '#attributes' => $attributes,
      'page' => [
        '#type' => 'page',
        '#theme' => $page_theme_property,
        '#title' => $title,
        'content' => $content,
      ] + $page_additions,
    ];

    // For backwards compatibility.
    // @todo In Drupal 9, add a $show_messages function parameter.
    if (!isset($page_additions['#show_messages']) || $page_additions['#show_messages'] === TRUE) {
      $html['page']['highlighted'] = ['#type' => 'status_messages'];
    }

    // Add the bare minimum of attachments from the system module and the
    // current maintenance theme.
    $this->systemPageAttachments($html['page']);
    $this->renderer->renderRoot($html);

    $response = new HtmlResponse();
    $response->setContent($html);
    // Process attachments, because this does not go via the regular render
    // pipeline, but will be sent directly.
    $response = $this->htmlResponseAttachmentsProcessor->processAttachments($response);

    return $response;
  }

  /**
   * Helper for system_page_attachments.
   *
   * SystemPageAttachment needs to be on BareHtmlPageRenderer.
   * When BareHtmlPageRenderer is called, the system module is not available.
   * PageAttachmentsHook can inject BareHtmlPageRenderer to use for
   * system_page_attachments.
   *
   * @param array $page
   *   The page to attach to.
   */
  public function systemPageAttachments(array &$page): void {
    // Ensure the same CSS is loaded in template_preprocess_maintenance_page().
    $page['#attached']['library'][] = 'system/base';
    if (\Drupal::service('router.admin_context')->isAdminRoute()) {
      $page['#attached']['library'][] = 'system/admin';
    }

    // Attach libraries used by this theme.
    $active_theme = \Drupal::theme()->getActiveTheme();
    foreach ($active_theme->getLibraries() as $library) {
      $page['#attached']['library'][] = $library;
    }

    // Attach favicon.
    if (theme_get_setting('features.favicon')) {
      $favicon = theme_get_setting('favicon.url');
      $type = theme_get_setting('favicon.mimetype');
      $page['#attached']['html_head_link'][][] = [
        'rel' => 'icon',
        'href' => UrlHelper::stripDangerousProtocols($favicon),
        'type' => $type,
      ];
    }

    // Get the major Drupal version.
    [$version] = explode('.', \Drupal::VERSION);

    // Attach default meta tags.
    $meta_default = [
      // Make sure the Content-Type comes first because the IE browser may be
      // vulnerable to XSS via encoding attacks from any content that comes
      // before this META tag, such as a TITLE tag.
      'system_meta_content_type' => [
        '#tag' => 'meta',
        '#attributes' => [
          'charset' => 'utf-8',
        ],
        // Security: This always has to be output first.
        '#weight' => -1000,
      ],
      // Show Drupal and the major version number in the META GENERATOR tag.
      'system_meta_generator' => [
        '#type' => 'html_tag',
        '#tag' => 'meta',
        '#attributes' => [
          'name' => 'Generator',
          'content' => 'Drupal ' . $version . ' (https://www.drupal.org)',
        ],
      ],
      // Attach default mobile meta tags for responsive design.
      'MobileOptimized' => [
        '#tag' => 'meta',
        '#attributes' => [
          'name' => 'MobileOptimized',
          'content' => 'width',
        ],
      ],
      'HandheldFriendly' => [
        '#tag' => 'meta',
        '#attributes' => [
          'name' => 'HandheldFriendly',
          'content' => 'true',
        ],
      ],
      'viewport' => [
        '#tag' => 'meta',
        '#attributes' => [
          'name' => 'viewport',
          'content' => 'width=device-width, initial-scale=1.0',
        ],
      ],
    ];
    foreach ($meta_default as $key => $value) {
      $page['#attached']['html_head'][] = [$value, $key];
    }

    // Handle setting the "active" class on links by:
    // - loading the active-link library if the current user is authenticated;
    // - applying a response filter if the current user is anonymous.
    // @see \Drupal\Core\Link
    // @see \Drupal\Core\Utility\LinkGenerator::generate()
    // @see \Drupal\Core\Theme\ThemePreprocess::preprocessLinks()
    // @see \Drupal\Core\EventSubscriber\ActiveLinkResponseFilter
    $page['#cache']['contexts'][] = 'user.roles:authenticated';
    if (\Drupal::currentUser()->isAuthenticated()) {
      $page['#attached']['library'][] = 'core/drupal.active-link';
    }
  }

}