BareHtmlPageRenderer.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace Drupal\Core\Render;
  3. /**
  4. * Default bare HTML page renderer.
  5. */
  6. class BareHtmlPageRenderer implements BareHtmlPageRendererInterface {
  7. /**
  8. * The renderer service.
  9. *
  10. * @var \Drupal\Core\Render\Renderer
  11. */
  12. protected $renderer;
  13. /**
  14. * The HTML response attachments processor service.
  15. *
  16. * @var \Drupal\Core\Render\AttachmentsResponseProcessorInterface
  17. */
  18. protected $htmlResponseAttachmentsProcessor;
  19. /**
  20. * Constructs a new BareHtmlPageRenderer.
  21. *
  22. * @param \Drupal\Core\Render\RendererInterface $renderer
  23. * The renderer service.
  24. * @param \Drupal\Core\Render\AttachmentsResponseProcessorInterface $html_response_attachments_processor
  25. * The HTML response attachments processor service.
  26. */
  27. public function __construct(RendererInterface $renderer, AttachmentsResponseProcessorInterface $html_response_attachments_processor) {
  28. $this->renderer = $renderer;
  29. $this->htmlResponseAttachmentsProcessor = $html_response_attachments_processor;
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function renderBarePage(array $content, $title, $page_theme_property, array $page_additions = []) {
  35. $attributes = [
  36. 'class' => [
  37. str_replace('_', '-', $page_theme_property),
  38. ],
  39. ];
  40. $html = [
  41. '#type' => 'html',
  42. '#attributes' => $attributes,
  43. 'page' => [
  44. '#type' => 'page',
  45. '#theme' => $page_theme_property,
  46. '#title' => $title,
  47. 'content' => $content,
  48. ] + $page_additions,
  49. ];
  50. // For backwards compatibility.
  51. // @todo In Drupal 9, add a $show_messages function parameter.
  52. if (!isset($page_additions['#show_messages']) || $page_additions['#show_messages'] === TRUE) {
  53. $html['page']['highlighted'] = ['#type' => 'status_messages'];
  54. }
  55. // Add the bare minimum of attachments from the system module and the
  56. // current maintenance theme.
  57. system_page_attachments($html['page']);
  58. $this->renderer->renderRoot($html);
  59. $response = new HtmlResponse();
  60. $response->setContent($html);
  61. // Process attachments, because this does not go via the regular render
  62. // pipeline, but will be sent directly.
  63. $response = $this->htmlResponseAttachmentsProcessor->processAttachments($response);
  64. return $response;
  65. }
  66. }