Same name and namespace in other branches
- 8.9.x core/lib/Drupal/Core/Render/Element/HtmlTag.php \Drupal\Core\Render\Element\HtmlTag
- 9 core/lib/Drupal/Core/Render/Element/HtmlTag.php \Drupal\Core\Render\Element\HtmlTag
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\Core\Render\Element\RenderElement implements ElementInterface
- class \Drupal\Core\Render\Element\HtmlTag
- class \Drupal\Core\Render\Element\RenderElement implements ElementInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of HtmlTag
1 file declares its use of HtmlTag
- HtmlTagTest.php in core/
tests/ Drupal/ Tests/ Core/ Render/ Element/ HtmlTagTest.php
File
- core/
lib/ Drupal/ Core/ Render/ Element/ HtmlTag.php, line 35
Namespace
Drupal\Core\Render\ElementView source
class HtmlTag extends RenderElementBase {
/**
* Void elements do not contain values or closing tags.
* @see http://www.w3.org/TR/html5/syntax.html#syntax-start-tag
* @see http://www.w3.org/TR/html5/syntax.html#void-elements
*/
protected static $voidElements = [
'area',
'base',
'br',
'col',
'embed',
'hr',
'img',
'input',
'keygen',
'link',
'meta',
'param',
'source',
'track',
'wbr',
'rect',
'circle',
'polygon',
'ellipse',
'stop',
'use',
'path',
];
/**
* {@inheritdoc}
*/
public function getInfo() {
$class = static::class;
return [
'#pre_render' => [
[
$class,
'preRenderHtmlTag',
],
],
'#attributes' => [],
'#value' => NULL,
];
}
/**
* Pre-render callback: Renders a generic HTML tag with attributes.
*
* @param array $element
* An associative array containing:
* - #tag: The tag name to output. Typical tags added to the HTML HEAD:
* - meta: To provide meta information, such as a page refresh.
* - link: To refer to stylesheets and other contextual information.
* - script: To load JavaScript.
* The value of #tag is escaped.
* - #attributes: (optional) An array of HTML attributes to apply to the
* tag. The attributes are escaped, see \Drupal\Core\Template\Attribute.
* - #value: (optional) A string containing tag content, such as inline
* CSS. The value of #value will be XSS admin filtered if it is not safe.
* - #noscript: (optional) If TRUE, the markup (including any prefix or
* suffix) will be wrapped in a <noscript> element. (Note that passing
* any non-empty value here will add the <noscript> tag.)
*
* @return array
*/
public static function preRenderHtmlTag($element) {
$attributes = isset($element['#attributes']) ? new Attribute($element['#attributes']) : '';
// An HTML tag should not contain any special characters. Escape them to
// ensure this cannot be abused.
$escaped_tag = HtmlUtility::escape($element['#tag']);
$open_tag = '<' . $escaped_tag . $attributes;
$close_tag = '</' . $escaped_tag . ">\n";
// Construct a void element.
if (in_array($element['#tag'], self::$voidElements)) {
$open_tag .= ' />';
$close_tag = "\n";
}
else {
$open_tag .= '>';
if ($element['#value'] === NULL) {
$element['#markup'] = '';
}
elseif ($element['#value'] instanceof MarkupInterface) {
$element['#markup'] = $element['#value'];
}
else {
$element['#markup'] = Markup::create(Xss::filterAdmin($element['#value']));
}
}
$prefix = isset($element['#prefix']) ? $element['#prefix'] . $open_tag : $open_tag;
$suffix = isset($element['#suffix']) ? $close_tag . $element['#suffix'] : $close_tag;
if (!empty($element['#noscript'])) {
$prefix = '<noscript>' . $prefix;
$suffix .= '</noscript>';
}
$element['#prefix'] = Markup::create($prefix);
$element['#suffix'] = Markup::create($suffix);
return $element;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
HtmlTag:: |
protected static | property | Void elements do not contain values or closing tags. | |
HtmlTag:: |
public | function |
Returns the element properties for this element. Overrides ElementInterface:: |
|
HtmlTag:: |
public static | function | Pre-render callback: Renders a generic HTML tag with attributes. | |
MessengerTrait:: |
protected | property | The messenger. | 10 |
MessengerTrait:: |
public | function | Gets the messenger. | 10 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
2 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 38 |
RenderElement:: |
public static | function | Adds Ajax information about an element to communicate with JavaScript. | |
RenderElement:: |
public static | function | Adds members of this group as actual elements for rendering. | |
RenderElement:: |
public static | function | Form element processing handler for the #ajax form property. | 1 |
RenderElement:: |
public static | function | Arranges elements into groups. | |
RenderElement:: |
public static | function |
Sets a form element's class attribute. Overrides ElementInterface:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 3 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 1 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |