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. 10 core/lib/Drupal/Core/Render/Element/HtmlTag.php \Drupal\Core\Render\Element\HtmlTag
  3. 11.x 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'),
];

Plugin annotation

@RenderElement("html_tag");

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
21 #type uses of HtmlTag
BubbleableMetadataTest::providerTestMergeAttachmentsHtmlHeadMerging in core/tests/Drupal/Tests/Core/Render/BubbleableMetadataTest.php
Data provider for testMergeAttachmentsHtmlHeadMerging
claro_preprocess_field_multiple_value_form in core/themes/claro/claro.theme
Implements hook_preprocess_HOOK() for field_multiple_value_form.
ConfigSync::buildForm in core/modules/config/src/Form/ConfigSync.php
Form constructor.
contextual_toolbar in core/modules/contextual/contextual.module
Implements hook_toolbar().
CssCollectionRenderer::render in core/lib/Drupal/Core/Asset/CssCollectionRenderer.php
Renders an asset collection.

... See full list

File

core/lib/Drupal/Core/Render/Element/HtmlTag.php, line 34

Namespace

Drupal\Core\Render\Element
View source
class HtmlTag extends RenderElement {
    
    /**
     * 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 = get_class($this);
        return [
            '#pre_render' => [
                [
                    $class,
                    'preRenderConditionalComments',
                ],
                [
                    $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 .= '>';
            $markup = $element['#value'] instanceof MarkupInterface ? $element['#value'] : Xss::filterAdmin($element['#value']);
            $element['#markup'] = Markup::create($markup);
        }
        $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;
    }
    
    /**
     * Pre-render callback: Renders #browsers into #prefix and #suffix.
     *
     * @param array $element
     *   A render array with a '#browsers' property. The '#browsers' property can
     *   contain any or all of the following keys:
     *   - 'IE': If FALSE, the element is not rendered by Internet Explorer. If
     *     TRUE, the element is rendered by Internet Explorer. Can also be a string
     *     containing an expression for Internet Explorer to evaluate as part of a
     *     conditional comment. For example, this can be set to 'lt IE 7' for the
     *     element to be rendered in Internet Explorer 6, but not in Internet
     *     Explorer 7 or higher. Defaults to TRUE.
     *   - '!IE': If FALSE, the element is not rendered by browsers other than
     *     Internet Explorer. If TRUE, the element is rendered by those browsers.
     *     Defaults to TRUE.
     *   Examples:
     *   - To render an element in all browsers, '#browsers' can be left out or set
     *     to array('IE' => TRUE, '!IE' => TRUE).
     *   - To render an element in Internet Explorer only, '#browsers' can be set
     *     to array('!IE' => FALSE).
     *   - To render an element in Internet Explorer 6 only, '#browsers' can be set
     *     to array('IE' => 'lt IE 7', '!IE' => FALSE).
     *   - To render an element in Internet Explorer 8 and higher and in all other
     *     browsers, '#browsers' can be set to array('IE' => 'gte IE 8').
     *
     * @return array
     *   The passed-in element with markup for conditional comments potentially
     *   added to '#prefix' and '#suffix'.
     */
    public static function preRenderConditionalComments($element) {
        $browsers = isset($element['#browsers']) ? $element['#browsers'] : [];
        $browsers += [
            'IE' => TRUE,
            '!IE' => TRUE,
        ];
        // If rendering in all browsers, no need for conditional comments.
        if ($browsers['IE'] === TRUE && $browsers['!IE']) {
            return $element;
        }
        // Determine the conditional comment expression for Internet Explorer to
        // evaluate.
        if ($browsers['IE'] === TRUE) {
            $expression = 'IE';
        }
        elseif ($browsers['IE'] === FALSE) {
            $expression = '!IE';
        }
        else {
            // The IE expression might contain some user input data.
            $expression = Xss::filterAdmin($browsers['IE']);
        }
        // If the #prefix and #suffix properties are used, wrap them with
        // conditional comment markup. The conditional comment expression is
        // evaluated by Internet Explorer only. To control the rendering by other
        // browsers, use either the "downlevel-hidden" or "downlevel-revealed"
        // technique. See http://wikipedia.org/wiki/Conditional_comment
        // for details.
        // Ensure what we are dealing with is safe.
        // This would be done later anyway in drupal_render().
        $prefix = isset($element['#prefix']) ? $element['#prefix'] : '';
        if ($prefix && !$prefix instanceof MarkupInterface) {
            $prefix = Xss::filterAdmin($prefix);
        }
        $suffix = isset($element['#suffix']) ? $element['#suffix'] : '';
        if ($suffix && !$suffix instanceof MarkupInterface) {
            $suffix = Xss::filterAdmin($suffix);
        }
        // We ensured above that $expression is either a string we created or is
        // admin XSS filtered, and that $prefix and $suffix are also admin XSS
        // filtered if they are unsafe. Thus, all these strings are safe.
        if (!$browsers['!IE']) {
            // "downlevel-hidden".
            $element['#prefix'] = Markup::create("\n<!--[if {$expression}]>\n" . $prefix);
            $element['#suffix'] = Markup::create($suffix . "<![endif]-->\n");
        }
        else {
            // "downlevel-revealed".
            $element['#prefix'] = Markup::create("\n<!--[if {$expression}]><!-->\n" . $prefix);
            $element['#suffix'] = Markup::create($suffix . "<!--<![endif]-->\n");
        }
        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::preRenderConditionalComments public static function Pre-render callback: Renders #browsers into #prefix and #suffix.
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
RenderElement::preRenderAjaxForm public static function Adds Ajax information about an element to communicate with JavaScript.
RenderElement::preRenderGroup public static function Adds members of this group as actual elements for rendering.
RenderElement::processAjaxForm public static function Form element processing handler for the #ajax form property. 1
RenderElement::processGroup public static function Arranges elements into groups.
RenderElement::setAttributes public static function Sets a form element&#039;s class attribute. Overrides ElementInterface::setAttributes

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