HtmlResponseAttachmentsProcessor.php 17.6 KB
Newer Older
1 2 3 4 5 6 7
<?php

namespace Drupal\Core\Render;

use Drupal\Core\Asset\AssetCollectionRendererInterface;
use Drupal\Core\Asset\AssetResolverInterface;
use Drupal\Core\Asset\AttachedAssets;
8
use Drupal\Core\Asset\AttachedAssetsInterface;
9
use Drupal\Core\Config\ConfigFactoryInterface;
10
use Drupal\Core\Form\EnforcedResponseException;
11 12
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Component\Utility\Html;
13 14 15 16 17
use Symfony\Component\HttpFoundation\RequestStack;

/**
 * Processes attachments of HTML responses.
 *
18 19 20 21 22 23 24 25
 * This class is used by the rendering service to process the #attached part of
 * the render array, for HTML responses.
 *
 * To render attachments to HTML for testing without a controller, use the
 * 'bare_html_page_renderer' service to generate a
 * Drupal\Core\Render\HtmlResponse object. Then use its getContent(),
 * getStatusCode(), and/or the headers property to access the result.
 *
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
 * @see template_preprocess_html()
 * @see \Drupal\Core\Render\AttachmentsResponseProcessorInterface
 * @see \Drupal\Core\Render\BareHtmlPageRenderer
 * @see \Drupal\Core\Render\HtmlResponse
 * @see \Drupal\Core\Render\MainContent\HtmlRenderer
 */
class HtmlResponseAttachmentsProcessor implements AttachmentsResponseProcessorInterface {

  /**
   * The asset resolver service.
   *
   * @var \Drupal\Core\Asset\AssetResolverInterface
   */
  protected $assetResolver;

  /**
   * A config object for the system performance configuration.
   *
   * @var \Drupal\Core\Config\Config
   */
  protected $config;

  /**
   * The CSS asset collection renderer service.
   *
   * @var \Drupal\Core\Asset\AssetCollectionRendererInterface
   */
  protected $cssCollectionRenderer;

  /**
   * The JS asset collection renderer service.
   *
   * @var \Drupal\Core\Asset\AssetCollectionRendererInterface
   */
  protected $jsCollectionRenderer;

  /**
   * The request stack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

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

76 77 78 79 80 81 82
  /**
   * The module handler service.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
  /**
   * Constructs a HtmlResponseAttachmentsProcessor object.
   *
   * @param \Drupal\Core\Asset\AssetResolverInterface $asset_resolver
   *   An asset resolver.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   A config factory for retrieving required config objects.
   * @param \Drupal\Core\Asset\AssetCollectionRendererInterface $css_collection_renderer
   *   The CSS asset collection renderer.
   * @param \Drupal\Core\Asset\AssetCollectionRendererInterface $js_collection_renderer
   *   The JS asset collection renderer.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack.
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer.
98 99
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler service.
100
   */
101
  public function __construct(AssetResolverInterface $asset_resolver, ConfigFactoryInterface $config_factory, AssetCollectionRendererInterface $css_collection_renderer, AssetCollectionRendererInterface $js_collection_renderer, RequestStack $request_stack, RendererInterface $renderer, ModuleHandlerInterface $module_handler) {
102 103 104 105 106 107
    $this->assetResolver = $asset_resolver;
    $this->config = $config_factory->get('system.performance');
    $this->cssCollectionRenderer = $css_collection_renderer;
    $this->jsCollectionRenderer = $js_collection_renderer;
    $this->requestStack = $request_stack;
    $this->renderer = $renderer;
108
    $this->moduleHandler = $module_handler;
109 110 111 112 113 114 115 116 117 118 119
  }

  /**
   * {@inheritdoc}
   */
  public function processAttachments(AttachmentsInterface $response) {
    // @todo Convert to assertion once https://www.drupal.org/node/2408013 lands
    if (!$response instanceof HtmlResponse) {
      throw new \InvalidArgumentException('\Drupal\Core\Render\HtmlResponse instance expected.');
    }

120 121 122 123
    // First, render the actual placeholders; this may cause additional
    // attachments to be added to the response, which the attachment
    // placeholders rendered by renderHtmlResponseAttachmentPlaceholders() will
    // need to include.
124 125 126 127 128 129 130 131 132 133 134 135
    //
    // @todo Exceptions should not be used for code flow control. However, the
    //   Form API does not integrate with the HTTP Kernel based architecture of
    //   Drupal 8. In order to resolve this issue properly it is necessary to
    //   completely separate form submission from rendering.
    //   @see https://www.drupal.org/node/2367555
    try {
      $response = $this->renderPlaceholders($response);
    }
    catch (EnforcedResponseException $e) {
      return $e->getResponse();
    }
136

137
    // Get a reference to the attachments.
138 139
    $attached = $response->getAttachments();

140 141 142 143 144 145 146 147 148
    // Send a message back if the render array has unsupported #attached types.
    $unsupported_types = array_diff(
      array_keys($attached),
      ['html_head', 'feed', 'html_head_link', 'http_header', 'library', 'html_response_attachment_placeholders', 'placeholders', 'drupalSettings']
    );
    if (!empty($unsupported_types)) {
      throw new \LogicException(sprintf('You are not allowed to use %s in #attached.', implode(', ', $unsupported_types)));
    }

149 150 151 152 153 154
    // If we don't have any placeholders, there is no need to proceed.
    if (!empty($attached['html_response_attachment_placeholders'])) {
      // Get the placeholders from attached and then remove them.
      $attachment_placeholders = $attached['html_response_attachment_placeholders'];
      unset($attached['html_response_attachment_placeholders']);

155 156 157 158 159 160 161 162 163 164 165 166 167
      $assets = AttachedAssets::createFromRenderArray(['#attached' => $attached]);
      // Take Ajax page state into account, to allow for something like
      // Turbolinks to be implemented without altering core.
      // @see https://github.com/rails/turbolinks/
      $ajax_page_state = $this->requestStack->getCurrentRequest()->get('ajax_page_state');
      $assets->setAlreadyLoadedLibraries(isset($ajax_page_state) ? explode(',', $ajax_page_state['libraries']) : []);
      $variables = $this->processAssetLibraries($assets, $attachment_placeholders);
      // $variables now contains the markup to load the asset libraries. Update
      // $attached with the final list of libraries and JavaScript settings, so
      // that $response can be updated with those. Then the response object will
      // list the final, processed attachments.
      $attached['library'] = $assets->getLibraries();
      $attached['drupalSettings'] = $assets->getSettings();
168 169 170 171 172 173 174 175 176 177 178 179

      // Since we can only replace content in the HTML head section if there's a
      // placeholder for it, we can safely avoid processing the render array if
      // it's not present.
      if (!empty($attachment_placeholders['head'])) {
        // 'feed' is a special case of 'html_head_link'. We process them into
        // 'html_head_link' entries and merge them.
        if (!empty($attached['feed'])) {
          $attached = BubbleableMetadata::mergeAttachments(
            $attached,
            $this->processFeed($attached['feed'])
          );
180
          unset($attached['feed']);
181 182 183 184 185 186 187 188 189 190 191 192 193 194
        }
        // 'html_head_link' is a special case of 'html_head' which can be present
        // as a head element, but also as a Link: HTTP header depending on
        // settings in the render array. Processing it can add to both the
        // 'html_head' and 'http_header' keys of '#attached', so we must address
        // it before 'html_head'.
        if (!empty($attached['html_head_link'])) {
          // Merge the processed 'html_head_link' into $attached so that its
          // 'html_head' and 'http_header' values are present for further
          // processing.
          $attached = BubbleableMetadata::mergeAttachments(
            $attached,
            $this->processHtmlHeadLink($attached['html_head_link'])
          );
195
          unset($attached['html_head_link']);
196 197 198 199 200 201 202
        }

        // Now we can process 'html_head', which contains both 'feed' and
        // 'html_head_link'.
        if (!empty($attached['html_head'])) {
          $variables['head'] = $this->processHtmlHead($attached['html_head']);
        }
203
      }
204

205 206
      // Now replace the attachment placeholders.
      $this->renderHtmlResponseAttachmentPlaceholders($response, $attachment_placeholders, $variables);
207 208
    }

209
    // Set the HTTP headers and status code on the response if any bubbled.
210 211 212
    if (!empty($attached['http_header'])) {
      $this->setHeaders($response, $attached['http_header']);
    }
213

214 215 216 217
    // AttachmentsResponseProcessorInterface mandates that the response it
    // processes contains the final attachment values.
    $response->setAttachments($attached);

218 219 220
    return $response;
  }

221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
  /**
   * Renders placeholders (#attached['placeholders']).
   *
   * First, the HTML response object is converted to an equivalent render array,
   * with #markup being set to the response's content and #attached being set to
   * the response's attachments. Among these attachments, there may be
   * placeholders that need to be rendered (replaced).
   *
   * Next, RendererInterface::renderRoot() is called, which renders the
   * placeholders into their final markup.
   *
   * The markup that results from RendererInterface::renderRoot() is now the
   * original HTML response's content, but with the placeholders rendered. We
   * overwrite the existing content in the original HTML response object with
   * this markup. The markup that was rendered for the placeholders may also
   * have attachments (e.g. for CSS/JS assets) itself, and cacheability metadata
   * that indicates what that markup depends on. That metadata is also added to
   * the HTML response object.
   *
   * @param \Drupal\Core\Render\HtmlResponse $response
   *   The HTML response whose placeholders are being replaced.
   *
   * @return \Drupal\Core\Render\HtmlResponse
   *   The updated HTML response, with replaced placeholders.
   *
   * @see \Drupal\Core\Render\Renderer::replacePlaceholders()
   * @see \Drupal\Core\Render\Renderer::renderPlaceholder()
   */
  protected function renderPlaceholders(HtmlResponse $response) {
    $build = [
251
      '#markup' => Markup::create($response->getContent()),
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
      '#attached' => $response->getAttachments(),
    ];
    // RendererInterface::renderRoot() renders the $build render array and
    // updates it in place. We don't care about the return value (which is just
    // $build['#markup']), but about the resulting render array.
    // @todo Simplify this when https://www.drupal.org/node/2495001 lands.
    $this->renderer->renderRoot($build);

    // Update the Response object now that the placeholders have been rendered.
    $placeholders_bubbleable_metadata = BubbleableMetadata::createFromRenderArray($build);
    $response
      ->setContent($build['#markup'])
      ->addCacheableDependency($placeholders_bubbleable_metadata)
      ->setAttachments($placeholders_bubbleable_metadata->getAttachments());

    return $response;
  }

270 271 272
  /**
   * Processes asset libraries into render arrays.
   *
273 274
   * @param \Drupal\Core\Asset\AttachedAssetsInterface $assets
   *   The attached assets collection for the current response.
275 276 277 278 279 280 281 282 283
   * @param array $placeholders
   *   The placeholders that exist in the response.
   *
   * @return array
   *   An array keyed by asset type, with keys:
   *     - styles
   *     - scripts
   *     - scripts_bottom
   */
284
  protected function processAssetLibraries(AttachedAssetsInterface $assets, array $placeholders) {
285 286 287 288 289 290 291 292 293 294 295 296
    $variables = [];

    // Print styles - if present.
    if (isset($placeholders['styles'])) {
      // Optimize CSS if necessary, but only during normal site operation.
      $optimize_css = !defined('MAINTENANCE_MODE') && $this->config->get('css.preprocess');
      $variables['styles'] = $this->cssCollectionRenderer->render($this->assetResolver->getCssAssets($assets, $optimize_css));
    }

    // Print scripts - if any are present.
    if (isset($placeholders['scripts']) || isset($placeholders['scripts_bottom'])) {
      // Optimize JS if necessary, but only during normal site operation.
297
      $optimize_js = !defined('MAINTENANCE_MODE') && !\Drupal::state()->get('system.maintenance_mode') && $this->config->get('js.preprocess');
298 299 300 301 302 303 304 305 306
      list($js_assets_header, $js_assets_footer) = $this->assetResolver->getJsAssets($assets, $optimize_js);
      $variables['scripts'] = $this->jsCollectionRenderer->render($js_assets_header);
      $variables['scripts_bottom'] = $this->jsCollectionRenderer->render($js_assets_footer);
    }

    return $variables;
  }

  /**
307
   * Renders HTML response attachment placeholders.
308
   *
309 310 311
   * This is the last step where all of the attachments are placed into the
   * response object's contents.
   *
312 313 314 315 316 317 318 319 320
   * @param \Drupal\Core\Render\HtmlResponse $response
   *   The HTML response to update.
   * @param array $placeholders
   *   An array of placeholders, keyed by type with the placeholders
   *   present in the content of the response as values.
   * @param array $variables
   *   The variables to render and replace, keyed by type with renderable
   *   arrays as values.
   */
321
  protected function renderHtmlResponseAttachmentPlaceholders(HtmlResponse $response, array $placeholders, array $variables) {
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
    $content = $response->getContent();
    foreach ($placeholders as $type => $placeholder) {
      if (isset($variables[$type])) {
        $content = str_replace($placeholder, $this->renderer->renderPlain($variables[$type]), $content);
      }
    }
    $response->setContent($content);
  }

  /**
   * Sets headers on a response object.
   *
   * @param \Drupal\Core\Render\HtmlResponse $response
   *   The HTML response to update.
   * @param array $headers
337 338 339 340 341 342 343
   *   The headers to set, as an array. The items in this array should be as
   *   follows:
   *   - The header name.
   *   - The header value.
   *   - (optional) Whether to replace a current value with the new one, or add
   *     it to the others. If the value is not replaced, it will be appended,
   *     resulting in a header like this: 'Header: value1,value2'
344 345
   */
  protected function setHeaders(HtmlResponse $response, array $headers) {
346 347 348 349 350
    foreach ($headers as $values) {
      $name = $values[0];
      $value = $values[1];
      $replace = !empty($values[2]);

351 352
      // Drupal treats the HTTP response status code like a header, even though
      // it really is not.
353
      if (strtolower($name) === 'status') {
354 355
        $response->setStatusCode($value);
      }
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
      else {
        $response->headers->set($name, $value, $replace);
      }
    }
  }

  /**
   * Ensure proper key/data order and defaults for renderable head items.
   *
   * @param array $html_head
   *   The ['#attached']['html_head'] portion of a render array.
   *
   * @return array
   *   The ['#attached']['html_head'] portion of a render array with #type of
   *   html_tag added for items without a #type.
   */
  protected function processHtmlHead(array $html_head) {
    $head = [];
    foreach ($html_head as $item) {
      list($data, $key) = $item;
      if (!isset($data['#type'])) {
        $data['#type'] = 'html_tag';
      }
      $head[$key] = $data;
    }
    return $head;
  }

  /**
   * Transform a html_head_link array into html_head and http_header arrays.
   *
387 388
   * Variable html_head_link is a special case of html_head which can be present
   * as a link item in the HTML head section, and also as a Link: HTTP header,
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
   * depending on options in the render array. Processing it can add to both the
   * html_head and http_header sections.
   *
   * @param array $html_head_link
   *   The 'html_head_link' value of a render array. Each head link is specified
   *   by a two-element array:
   *   - An array specifying the attributes of the link.
   *   - A boolean specifying whether the link should also be a Link: HTTP
   *     header.
   *
   * @return array
   *   An ['#attached'] section of a render array. This allows us to easily
   *   merge the results with other render arrays. The array could contain the
   *   following keys:
   *   - http_header
   *   - html_head
   */
  protected function processHtmlHeadLink(array $html_head_link) {
    $attached = [];

    foreach ($html_head_link as $item) {
      $attributes = $item[0];
      $should_add_header = isset($item[1]) ? $item[1] : FALSE;

413
      $element = [
414 415
        '#tag' => 'link',
        '#attributes' => $attributes,
416
      ];
417 418 419 420 421
      $href = $attributes['href'];
      $attached['html_head'][] = [$element, 'html_head_link:' . $attributes['rel'] . ':' . $href];

      if ($should_add_header) {
        // Also add a HTTP header "Link:".
422
        $href = '<' . Html::escape($attributes['href']) . '>';
423
        unset($attributes['href']);
424 425 426 427 428
        if ($param = drupal_http_header_attributes($attributes)) {
          $href .= ';' . $param;
        }

        $attached['http_header'][] = ['Link', $href, FALSE];
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
      }
    }
    return $attached;
  }

  /**
   * Transform a 'feed' attachment into an 'html_head_link' attachment.
   *
   * The RSS feed is a special case of 'html_head_link', so we just turn it into
   * one.
   *
   * @param array $attached_feed
   *   The ['#attached']['feed'] portion of a render array.
   *
   * @return array
   *   An ['#attached']['html_head_link'] array, suitable for merging with
   *   another 'html_head_link' array.
   */
  protected function processFeed($attached_feed) {
    $html_head_link = [];
449
    foreach ($attached_feed as $item) {
450 451 452 453 454 455 456
      $feed_link = [
        'href' => $item[0],
        'rel' => 'alternate',
        'title' => empty($item[1]) ? '' : $item[1],
        'type' => 'application/rss+xml',
      ];
      $html_head_link[] = [$feed_link, FALSE];
457
    }
458
    return ['html_head_link' => $html_head_link];
459 460 461
  }

}