class HtmlTag

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Render/Element/HtmlTag.php \Drupal\Core\Render\Element\HtmlTag
  2. 8.9.x core/lib/Drupal/Core/Render/Element/HtmlTag.php \Drupal\Core\Render\Element\HtmlTag
  3. 10 core/lib/Drupal/Core/Render/Element/HtmlTag.php \Drupal\Core\Render\Element\HtmlTag

Provides a render element for any HTML tag, with properties and value.

Properties:

  • #tag: The tag name to output.
  • #attributes: (array, optional) HTML attributes to apply to the tag. The attributes are escaped, see \Drupal\Core\Template\Attribute.
  • #value: (string, optional) A string containing the textual contents of the tag.
  • #noscript: (bool, optional) When set to TRUE, the markup (including any prefix or suffix) will be wrapped in a <noscript> element.

Usage example:

$build['hello'] = [
    '#type' => 'html_tag',
    '#tag' => 'p',
    '#value' => $this->t('Hello World'),
];

Hierarchy

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 33

Namespace

Drupal\Core\Render\Element
View 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

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
HtmlTag::$voidElements protected static property Void elements do not contain values or closing tags.
HtmlTag::getInfo public function Returns the element properties for this element. Overrides ElementInterface::getInfo
HtmlTag::preRenderHtmlTag public static function Pre-render callback: Renders a generic HTML tag with attributes.
PluginInspectionInterface::getPluginDefinition public function Gets the definition of the plugin implementation. 6
PluginInspectionInterface::getPluginId public function Gets the plugin_id of the plugin instance. 2
RenderElementBase::preRenderAjaxForm public static function Adds Ajax information about an element to communicate with JavaScript. 2
RenderElementBase::preRenderGroup public static function Adds members of this group as actual elements for rendering. 2
RenderElementBase::processAjaxForm public static function Form element processing handler for the #ajax form property. 3
RenderElementBase::processGroup public static function Arranges elements into groups. 2
RenderElementBase::setAttributes public static function Sets a form element&#039;s class attribute. Overrides ElementInterface::setAttributes 2

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.