1. 8.2.x core/lib/Drupal/Core/Render/Element/HtmlTag.php HtmlTag
  2. 8.0.x core/lib/Drupal/Core/Render/Element/HtmlTag.php HtmlTag
  3. 8.1.x core/lib/Drupal/Core/Render/Element/HtmlTag.php HtmlTag
  4. 8.3.x core/lib/Drupal/Core/Render/Element/HtmlTag.php 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
14 #type uses of HtmlTag
BubbleableMetadataTest::providerTestMergeAttachmentsHtmlHeadMerging in core/tests/Drupal/Tests/Core/Render/BubbleableMetadataTest.php
Data provider for testMergeAttachmentsHtmlHeadMerging
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.
CssCollectionRendererUnitTest::providerTestRender in core/tests/Drupal/Tests/Core/Asset/CssCollectionRendererUnitTest.php
Provides data for the CSS asset rendering test.

... 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
   */
  static protected $voidElements = array(
    'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input',
    'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr',
  );

  /**
   * {@inheritdoc}
   */
  public function getInfo() {
    $class = get_class($this);
    return array(
      '#pre_render' => array(
        array($class, 'preRenderConditionalComments'),
        array($class, 'preRenderHtmlTag'),
      ),
      '#attributes' => array(),
      '#value' => NULL,
    );
  }

  /**
   * Pre-render callback: Renders a generic HTML tag with attributes into #markup.
   *
   * @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']);
    $markup = '<' . $escaped_tag . $attributes;
    // Construct a void element.
    if (in_array($element['#tag'], self::$voidElements)) {
      $markup .= " />\n";
    }
    // Construct all other elements.
    else {
      $markup .= '>';
      $markup .= $element['#value'] instanceof MarkupInterface ? $element['#value'] : Xss::filterAdmin($element['#value']);
      $markup .= '</' . $escaped_tag . ">\n";
    }
    if (!empty($element['#noscript'])) {
      $markup = "<noscript>$markup</noscript>";
    }
    $element['#markup'] = Markup::create($markup);
    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'] : array();
    $browsers += array(
      '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

Contains filters are case sensitive
Namesort descending Modifiers Type Description
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function Prevents serializing any properties.
DependencySerializationTrait::__wakeup public function
HtmlTag::$voidElements static protected property Void elements do not contain values or closing tags.
HtmlTag::getInfo public function 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 into #markup.
PluginBase::$configuration protected property Configuration information passed into the plugin.
PluginBase::$pluginDefinition protected property The plugin implementation definition.
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::__construct public function Constructs a Drupal\Component\Plugin\PluginBase object.
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.
RenderElement::processGroup public static function Arranges elements into groups.
RenderElement::setAttributes public static function Sets a form element's class attribute. Overrides ElementInterface::setAttributes
StringTranslationTrait::$stringTranslation protected property The string translation service.
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use.
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.