DefaultHtmlPageRenderer.php 3.06 KB
Newer Older
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
<?php

/**
 * @file
 * Contains \Drupal\Core\Page\DefaultHtmlPageRenderer
 */

namespace Drupal\Core\Page;

use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageManager;

/**
 * Default page rendering engine.
 */
class DefaultHtmlPageRenderer implements HtmlPageRendererInterface {

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManager
   */
  protected $languageManager;

  /**
   * Constructs a new DefaultHtmlPageRenderer.
   *
   * @param \Drupal\Core\Language\LanguageManager $language_manager
   *   The language manager service.
   */
  public function __construct(LanguageManager $language_manager) {
    $this->languageManager = $language_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function render(HtmlFragment $fragment, $status_code = 200) {
39
40
    // Converts the given HTML fragment which represents the main content region
    // of the page into a render array.
41
42
    $page_content['main'] = array(
      '#markup' => $fragment->getContent(),
43
      '#cache' => array('tags' => $fragment->getCacheTags()),
44
    );
45
    $page_content['#title'] = $fragment->getTitle();
46

47
48
    // Build the full page array by calling drupal_prepare_page(), which invokes
    // hook_page_build(). This adds the other regions to the page.
49
50
    $page_array = drupal_prepare_page($page_content);

51
52
    // Collect cache tags for all the content in all the regions on the page.
    $tags = drupal_render_collect_cache_tags($page_array);
53

54
55
56
    // Build the HtmlPage object.
    $page = new HtmlPage('', array('tags' => $tags), $fragment->getTitle());
    $page = $this->preparePage($page, $page_array);
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
    $page->setBodyTop(drupal_render($page_array['page_top']));
    $page->setBodyBottom(drupal_render($page_array['page_bottom']));
    $page->setContent(drupal_render($page_array));
    $page->setStatusCode($status_code);

    return $page;
  }

  /**
   * {@inheritdoc}
   */
  public function renderPage(HtmlPage $page) {
    $render = array(
      '#theme' => 'html',
      '#page_object' => $page,
    );
    return drupal_render($render);
  }

  /**
   * Enhances a page object based on a render array.
   *
   * @param \Drupal\Core\Page\HtmlPage $page
   *   The page object to enhance.
   * @param array $page_array
   *   The page array to extract onto the page object.
   *
   * @return \Drupal\Core\Page\HtmlPage
   *   The modified page object.
   */
  public function preparePage(HtmlPage $page, &$page_array) {
    // @todo Remove this one drupal_get_title() has been eliminated.
    if (!$page->hasTitle()) {
      $title = drupal_get_title();
      // drupal_set_title() already ensured security, so not letting the
      // title pass through would cause double escaping.
      $page->setTitle($title, PASS_THROUGH);
    }

    $page_array['#page'] = $page;

    // HTML element attributes.
99
    $language_interface = $this->languageManager->getCurrentLanguage();
100
101
102
103
104
105
106
107
    $html_attributes = $page->getHtmlAttributes();
    $html_attributes['lang'] = $language_interface->id;
    $html_attributes['dir'] = $language_interface->direction ? 'rtl' : 'ltr';

    return $page;
  }

}