function theme_render_and_autoescape
Same name in other branches
- 9 core/includes/theme.inc \theme_render_and_autoescape()
- 8.9.x core/includes/theme.inc \theme_render_and_autoescape()
Escapes and renders variables for theme functions.
This method is used in theme functions to ensure that the result is safe for output inside HTML fragments. This mimics the behavior of the auto-escape functionality in Twig.
Note: This function should be kept in sync with \Drupal\Core\Template\TwigExtension::escapeFilter().
Parameters
mixed $arg: The string, object, or render array to escape if needed.
Return value
string The rendered string, safe for use in HTML. The string is not safe when used as any part of an HTML attribute name or value.
Throws
\Exception Thrown when an object is passed in which cannot be printed.
Deprecated
in drupal:10.1.0 and is removed from drupal:11.0.0. There is no replacement. Theme engines must handle escaping by themselves.
See also
https://www.drupal.org/node/3336253
4 calls to theme_render_and_autoescape()
- ThemeRenderAndAutoescapeTest::testBubblingMetadata in core/
tests/ Drupal/ KernelTests/ Core/ Theme/ ThemeRenderAndAutoescapeTest.php - Ensure cache metadata is bubbled when using theme_render_and_autoescape().
- ThemeRenderAndAutoescapeTest::testBubblingMetadataWithRenderable in core/
tests/ Drupal/ KernelTests/ Core/ Theme/ ThemeRenderAndAutoescapeTest.php - Ensure cache metadata is bubbled when using theme_render_and_autoescape().
- ThemeRenderAndAutoescapeTest::testThemeEscapeAndRenderNotPrintable in core/
tests/ Drupal/ KernelTests/ Core/ Theme/ ThemeRenderAndAutoescapeTest.php - Ensures invalid content is handled correctly.
- ThemeRenderAndAutoescapeTest::testThemeRenderAndAutoescape in core/
tests/ Drupal/ KernelTests/ Core/ Theme/ ThemeRenderAndAutoescapeTest.php - @dataProvider providerTestThemeRenderAndAutoescape
File
-
core/
includes/ theme.inc, line 365
Code
function theme_render_and_autoescape($arg) {
@trigger_error('theme_render_and_autoescape() is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. There is no replacement. Theme engines must handle escaping by themselves. See https://www.drupal.org/node/3336253', E_USER_DEPRECATED);
// If it's a renderable, then it'll be up to the generated render array it
// returns to contain the necessary cacheability & attachment metadata. If
// it doesn't implement CacheableDependencyInterface or AttachmentsInterface
// then there is nothing to do here.
if (!$arg instanceof RenderableInterface && ($arg instanceof CacheableDependencyInterface || $arg instanceof AttachmentsInterface)) {
$arg_bubbleable = [];
BubbleableMetadata::createFromObject($arg)->applyTo($arg_bubbleable);
\Drupal::service('renderer')->render($arg_bubbleable);
}
if ($arg instanceof MarkupInterface) {
return (string) $arg;
}
$return = NULL;
if (is_scalar($arg)) {
$return = (string) $arg;
}
elseif (is_object($arg)) {
if ($arg instanceof RenderableInterface) {
$arg = $arg->toRenderable();
}
elseif (method_exists($arg, '__toString')) {
$return = (string) $arg;
}
elseif (method_exists($arg, 'toString')) {
$return = $arg->toString();
}
else {
throw new \Exception('Object of type ' . get_class($arg) . ' cannot be printed.');
}
}
// We have a string or an object converted to a string: Escape it!
if (isset($return)) {
return $return instanceof MarkupInterface ? $return : Html::escape($return);
}
// This is a normal render array, which is safe by definition, with special
// simple cases already handled.
// Early return if this element was pre-rendered (no need to re-render).
if (isset($arg['#printed']) && $arg['#printed'] == TRUE && isset($arg['#markup']) && strlen($arg['#markup']) > 0) {
return (string) $arg['#markup'];
}
$arg['#printed'] = FALSE;
return (string) \Drupal::service('renderer')->render($arg);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.