public 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().
File
- core/lib/Drupal/Core/Template/TwigExtension.php, line 412
Class
- TwigExtension
- A class providing Drupal Twig extensions.
Namespace
Drupal\Core\Template
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | 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 ; } // You can't throw exceptions in the magic PHP __toString() methods, see // http://php.net/manual/language.oop5.magic.php#object.tostring so // we also support a toString method. 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 ); } |
Please login to continue.