HtmlFragment.php 3.95 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
<?php

/**
 * @file
 * Contains \Drupal\Core\Page\HtmlFragment.
 */

namespace Drupal\Core\Page;

use Drupal\Component\Utility\String;
use Drupal\Component\Utility\Xss;
12
use Drupal\Core\Cache\CacheableInterface;
13
14
15
16
17
18
19
20
21
use Drupal\Core\Utility\Title;

/**
 * Response object that contains variables for injection into the html template.
 *
 * @todo Should we have this conform to an interface?
 *   https://drupal.org/node/1871596#comment-7134686
 * @todo Add method replacements for *all* data sourced by html.tpl.php.
 */
22
class HtmlFragment implements CacheableInterface {
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

  /**
   * HTML content string.
   *
   * @var string
   */
  protected $content;

  /**
   * The title of this HtmlFragment.
   *
   * @var string
   */
  protected $title = '';

38
39
40
41
42
43
44
  /**
   * The cache metadata of this HtmlFragment.
   *
   * @var array
   */
  protected $cache = array();

45
46
47
48
49
  /**
   * Constructs a new HtmlFragment.
   *
   * @param string $content
   *   The content for this fragment.
50
51
   * @param array $cache_info
   *   The cache information.
52
   */
53
  public function __construct($content = '', array $cache_info = array()) {
54
    $this->content = $content;
55
56
57
58
59
60
61
    $this->cache = $cache_info + array(
      'keys' => array(),
      'tags' => array(),
      'bin' => NULL,
      'max_age' => 0,
      'is_cacheable' => TRUE,
    );
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
  }

  /**
   * Sets the response content.
   *
   * This should be the bulk of the page content, and will ultimately be placed
   * within the <body> tag in final HTML output.
   *
   * Valid types are strings, numbers, and objects that implement a __toString()
   * method.
   *
   * @param mixed $content
   *   The content for this fragment.
   *
   * @return self
   *   The fragment.
   */
  public function setContent($content) {
    $this->content = $content;
    return $this;
  }

  /**
   * Gets the main content of this HtmlFragment.
   *
   * @return string
   *   The content for this fragment.
   */
  public function getContent() {
    return $this->content;
  }

  /**
   * Sets the title of this HtmlFragment.
   *
   * Handling of this title varies depending on what is consuming this
   * HtmlFragment object. If it's a block, it may only be used as the block's
   * title; if it's at the page level, it will be used in a number of places,
   * including the html <head> title.
   *
   * @param string $title
   *   Value to assign to the page title.
   * @param int $output
   *   (optional) normally should be left as Title::CHECK_PLAIN. Only set to
   *   PASS_THROUGH if you have already removed any possibly dangerous code
   *   from $title using a function like
   *   \Drupal\Component\Utility\String::checkPlain() or
   *   \Drupal\Component\Utility\Xss::filterAdmin(). With this flag the string
   *   will be passed through unchanged.
   */
  public function setTitle($title, $output = Title::CHECK_PLAIN) {
    if ($output == Title::CHECK_PLAIN) {
      $this->title = String::checkPlain($title);
    }
    else if ($output == Title::FILTER_XSS_ADMIN) {
      $this->title = Xss::filterAdmin($title);
    }
    else {
      $this->title = $title;
    }
  }

  /**
   * Indicates whether or not this HtmlFragment has a title.
   *
   * @return bool
   */
  public function hasTitle() {
    return !empty($this->title);
  }

  /**
   * Gets the title for this HtmlFragment, if any.
   *
   * @return string
   *   The title.
   */
  public function getTitle() {
    return $this->title;
  }

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
  /**
   * {@inheritdoc}
   *
   * @TODO Use a trait once we require php 5.4 for all the cache methods.
   */
  public function getCacheKeys() {
    return $this->cache['keys'];
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheTags() {
    return $this->cache['tags'];
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheBin() {
    return $this->cache['bin'];
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheMaxAge() {
    return $this->cache['max_age'];
  }

  /**
   * {@inheritdoc}
   */
  public function isCacheable() {
    return $this->cache['is_cacheable'];
  }

180
}