HtmlFragment.php 4.73 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
use Drupal\Core\Utility\Title;

/**
16
 * Basic mutable implementation of an HTML fragment.
17
 */
18
class HtmlFragment implements CacheableInterface, HtmlFragmentInterface {
19

20 21 22 23 24 25 26 27 28 29 30 31 32 33
  /**
   * An array of Link elements.
   *
   * @var array
   */
  protected $links = array();

  /**
   * An array of Meta elements.
   *
   * @var array
   */
  protected $metatags = array();

34 35 36 37 38 39 40 41 42 43 44 45 46 47
  /**
   * HTML content string.
   *
   * @var string
   */
  protected $content;

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

48 49 50 51 52 53 54
  /**
   * The cache metadata of this HtmlFragment.
   *
   * @var array
   */
  protected $cache = array();

55 56 57 58 59
  /**
   * Constructs a new HtmlFragment.
   *
   * @param string $content
   *   The content for this fragment.
60 61
   * @param array $cache_info
   *   The cache information.
62
   */
63
  public function __construct($content = '', array $cache_info = array()) {
64
    $this->content = $content;
65 66 67 68 69 70 71
    $this->cache = $cache_info + array(
      'keys' => array(),
      'tags' => array(),
      'bin' => NULL,
      'max_age' => 0,
      'is_cacheable' => TRUE,
    );
72 73
  }

74 75 76 77 78 79 80 81 82 83 84 85 86 87
  /**
   * Adds a link element to the page.
   *
   * @param \Drupal\Core\Page\LinkElement $link
   *   A link element to enqueue.
   *
   * @return $this
   */
  public function addLinkElement(LinkElement $link) {
    $this->links[] = $link;
    return $this;
  }

  /**
88
   * {@inheritdoc}
89 90 91 92 93 94
   */
  public function &getLinkElements() {
    return $this->links;
  }

  /**
95
   * {@inheritdoc}
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
   */
  public function getFeedLinkElements() {
    $feed_links = array();
    foreach ($this->links as $link) {
      if ($link instanceof FeedLinkElement) {
        $feed_links[] = $link;
      }
    }
    return $feed_links;
  }

  /**
   * Adds a meta element to the page.
   *
   * @param \Drupal\Core\Page\MetaElement $meta
   *   A meta element to add.
   *
   * @return $this
   */
  public function addMetaElement(MetaElement $meta) {
    $this->metatags[] = $meta;
    return $this;
  }

  /**
121
   * {@inheritdoc}
122 123 124 125 126
   */
  public function &getMetaElements() {
    return $this->metatags;
  }

127 128 129 130 131 132 133 134 135 136 137 138
  /**
   * 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.
   *
139
   * @return $this
140 141 142 143 144 145 146
   */
  public function setContent($content) {
    $this->content = $content;
    return $this;
  }

  /**
147
   * {@inheritdoc}
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
   */
  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
165 166
   *   Title::PASS_THROUGH if you have already removed any possibly dangerous
   *   code from $title using a function like
167 168 169
   *   \Drupal\Component\Utility\String::checkPlain() or
   *   \Drupal\Component\Utility\Xss::filterAdmin(). With this flag the string
   *   will be passed through unchanged.
170 171
   *
   * @return $this
172 173 174 175 176 177 178 179 180 181 182
   */
  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;
    }
183
    return $this;
184 185 186
  }

  /**
187
   * {@inheritdoc}
188 189 190 191 192 193
   */
  public function hasTitle() {
    return !empty($this->title);
  }

  /**
194
   * {@inheritdoc}
195 196 197 198 199
   */
  public function getTitle() {
    return $this->title;
  }

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
  /**
   * {@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'];
  }

237
}