function HtmlTag::preRenderConditionalComments
Same name in other branches
- 9 core/lib/Drupal/Core/Render/Element/HtmlTag.php \Drupal\Core\Render\Element\HtmlTag::preRenderConditionalComments()
Pre-render callback: Renders #browsers into #prefix and #suffix.
Parameters
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 value
array The passed-in element with markup for conditional comments potentially added to '#prefix' and '#suffix'.
2 calls to HtmlTag::preRenderConditionalComments()
- HtmlTagTest::providerPreRenderHtmlTag in core/
tests/ Drupal/ Tests/ Core/ Render/ Element/ HtmlTagTest.php - Data provider for preRenderHtmlTag test.
- HtmlTagTest::testPreRenderConditionalComments in core/
tests/ Drupal/ Tests/ Core/ Render/ Element/ HtmlTagTest.php - @covers ::preRenderConditionalComments @dataProvider providerPreRenderConditionalComments
File
-
core/
lib/ Drupal/ Core/ Render/ Element/ HtmlTag.php, line 141
Class
- HtmlTag
- Provides a render element for any HTML tag, with properties and value.
Namespace
Drupal\Core\Render\ElementCode
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;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.