Newer
Older
<?php
/**
* @file
* Contains \Drupal\Core\Page\HeadElement.
*/
namespace Drupal\Core\Page;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Template\Attribute;
/**
* This class represents an HTML element that appears in the HEAD tag.
*
* @see template_preprocess_html()
17
18
19
20
21
22
23
24
25
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
*/
class HeadElement {
/**
* An array of attributes for this element.
*
* @var array
*/
protected $attributes = array();
/**
* The element name.
*
* Sub-classes should override this value with the name of their element.
*
* @var string
*/
protected $element = '';
/**
* If this element should be wrapped in <noscript>.
*
* @var bool
*/
protected $noScript = FALSE;
/**
* Renders this object to an HTML element string.
*
* @return string
*/
public function __toString() {
// Render the attributes via the attribute template class.
// @todo Should HeadElement just extend the Attribute classes?
$attributes = new Attribute($this->attributes);
$rendered = (string) $attributes;
$string = "<{$this->element}{$rendered} />";
if ($this->noScript) {
$string = "<noscript>$string</noscript>";
}
return SafeMarkup::set($string);
}
/**
* Sets an attribute on this element.
*
* @param mixed $key
* The attribute to set.
* @param mixed $value
* The value to which to set it.
*
Angie Byron
committed
* @return $this
*/
public function setAttribute($key, $value) {
$this->attributes[$key] = $value;
return $this;
}
catch
committed
/**
* Gets all the attributes.
*
* @return array
* An array of all the attributes keyed by name of attribute.
*/
public function &getAttributes() {
return $this->attributes;
}
/**
* Sets if this element should be wrapped in <noscript>.
*
* @param bool $value
* (optional) Whether or not this element should be wrapped in <noscript>.
* Defaults to TRUE.
*
Angie Byron
committed
* @return $this