8.5.x TwigExtension.php TwigExtension::escapeFilter(\Twig_Environment $env, $arg, $strategy = 'html', $charset = NULL, $autoescape = FALSE)
8.0.x TwigExtension.php TwigExtension::escapeFilter(\Twig_Environment $env, $arg, $strategy = 'html', $charset = NULL, $autoescape = FALSE)
8.1.x TwigExtension.php TwigExtension::escapeFilter(\Twig_Environment $env, $arg, $strategy = 'html', $charset = NULL, $autoescape = FALSE)
8.2.x TwigExtension.php TwigExtension::escapeFilter(\Twig_Environment $env, $arg, $strategy = 'html', $charset = NULL, $autoescape = FALSE)
8.3.x TwigExtension.php TwigExtension::escapeFilter(\Twig_Environment $env, $arg, $strategy = 'html', $charset = NULL, $autoescape = FALSE)
8.4.x TwigExtension.php TwigExtension::escapeFilter(\Twig_Environment $env, $arg, $strategy = 'html', $charset = NULL, $autoescape = FALSE)
8.6.x TwigExtension.php TwigExtension::escapeFilter(\Twig_Environment $env, $arg, $strategy = 'html', $charset = NULL, $autoescape = FALSE)

Overrides twig_escape_filter().

Replacement function for Twig's escape filter.

Note: This function should be kept in sync with theme_render_and_autoescape().

@todo Refactor this to keep it in sync with theme_render_and_autoescape() in https://www.drupal.org/node/2575065

Parameters

\Twig_Environment $env: A Twig_Environment instance.

mixed $arg: The value to be escaped.

string $strategy: The escaping strategy. Defaults to 'html'.

string $charset: The charset.

bool $autoescape: Whether the function is called by the auto-escaping feature (TRUE) or by the developer (FALSE).

Return value

string|null The escaped, rendered output, or NULL if there is no valid output.

Throws

\Exception When $arg is passed as an object which does not implement __toString(), RenderableInterface or toString().

2 calls to TwigExtension::escapeFilter()
TwigExtension::escapePlaceholder in core/lib/Drupal/Core/Template/TwigExtension.php
Provides a placeholder wrapper around ::escapeFilter.
TwigExtension::safeJoin in core/lib/Drupal/Core/Template/TwigExtension.php
Joins several strings together safely.

File

core/lib/Drupal/Core/Template/TwigExtension.php, line 412

Class

TwigExtension
A class providing Drupal Twig extensions.

Namespace

Drupal\Core\Template

Code

public function escapeFilter(\Twig_Environment $env, $arg, $strategy = 'html', $charset = NULL, $autoescape = FALSE) {

  // Check for a numeric zero int or float.
  if ($arg === 0 || $arg === 0.0) {
    return 0;
  }

  // Return early for NULL and empty arrays.
  if ($arg == NULL) {
    return NULL;
  }
  $this
    ->bubbleArgMetadata($arg);

  // Keep Twig_Markup objects intact to support autoescaping.
  if ($autoescape && ($arg instanceof \Twig_Markup || $arg instanceof MarkupInterface)) {
    return $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: Autoescape it!
  if (isset($return)) {
    if ($autoescape && $return instanceof MarkupInterface) {
      return $return;
    }

    // Drupal only supports the HTML escaping strategy, so provide a
    // fallback for other strategies.
    if ($strategy == 'html') {
      return Html::escape($return);
    }
    return twig_escape_filter($env, $return, $strategy, $charset, $autoescape);
  }

  // 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 $arg['#markup'];
  }
  $arg['#printed'] = FALSE;
  return $this->renderer
    ->render($arg);
}