TwigExtension
class TwigExtension extends AbstractExtension (View source)
A class providing Drupal Twig extensions.
This provides a Twig extension that registers various Drupal-specific extensions to Twig, specifically Twig functions, filter, and node visitors.
Properties
| protected UrlGeneratorInterface | $urlGenerator | The URL generator. |
|
| protected RendererInterface | $renderer | The renderer. |
|
| protected ThemeManagerInterface | $themeManager | The theme manager. |
|
| protected DateFormatterInterface | $dateFormatter | The date formatter. |
Methods
Constructs \Drupal\Core\Template\TwigExtension.
{@inheritdoc}
{@inheritdoc}
{@inheritdoc}
{@inheritdoc}
{@inheritdoc}
Generates a URL path given a route name and parameters.
Generates an absolute URL given a route name and parameters.
Gets the name of the active theme.
Gets the path of the active theme.
Determines at compile time whether the generated URL will be safe.
Attaches an asset library to the template, and hence to the response.
Provides a placeholder wrapper around ::escapeFilter.
Overrides twig_escape_filter().
Bubbles Twig template argument's cacheability & attachment metadata.
Wrapper around render() for twig printed output.
Joins several strings together safely.
Creates an Attribute object.
Removes child elements from a copy of the original array.
Details
__construct(RendererInterface $renderer, UrlGeneratorInterface $url_generator, ThemeManagerInterface $theme_manager, DateFormatterInterface $date_formatter)
Constructs \Drupal\Core\Template\TwigExtension.
getFunctions()
{@inheritdoc}
getFilters()
{@inheritdoc}
getNodeVisitors()
{@inheritdoc}
getTokenParsers()
{@inheritdoc}
getName()
{@inheritdoc}
string
getPath($name, array $parameters = [], array $options = [])
Generates a URL path given a route name and parameters.
array
getUrl($name, array $parameters = [], array $options = [])
Generates an absolute URL given a route name and parameters.
array
getLink(string $text, Url|string $url, array|Attribute $attributes = [])
Gets a rendered link from a url object.
string
getActiveTheme()
Gets the name of the active theme.
string
getActiveThemePath()
Gets the path of the active theme.
array
isUrlGenerationSafe(Node $args_node)
Determines at compile time whether the generated URL will be safe.
Saves the unneeded automatic escaping for performance reasons.
The URL generation process percent encodes non-alphanumeric characters. Thus, the only character within a URL that must be escaped in HTML is the ampersand ("&") which separates query params. Thus we cannot mark the generated URL as always safe, but only when we are sure there won't be multiple query params. This is the case when there are none or only one constant parameter given. For instance, we know beforehand this will not need to be escaped:
- path('route')
- path('route', {'param': 'value'}) But the following may need to be escaped:
- path('route', var)
- path('route', {'param': ['val1', 'val2'] }) // a sub-array
- path('route', {'param1': 'value1', 'param2': 'value2'}) If param1 and param2 reference placeholders in the route, it would not need to be escaped, but we don't know that in advance.
attachLibrary(string $library)
Attaches an asset library to the template, and hence to the response.
Allows Twig templates to attach asset libraries using
string|null
escapePlaceholder(Environment $env, mixed $string)
Provides a placeholder wrapper around ::escapeFilter.
string|null
escapeFilter(Environment $env, mixed $arg, string $strategy = 'html', string $charset = NULL, bool $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().
protected
bubbleArgMetadata(mixed $arg)
Bubbles Twig template argument's cacheability & attachment metadata.
For example: a generated link or generated URL object is passed as a Twig template argument, and its bubbleable metadata must be bubbled.
mixed
renderVar(mixed $arg)
Wrapper around render() for twig printed output.
If an object is passed which does not implement __toString(), RenderableInterface or toString() then an exception is thrown; Other objects are casted to string. However in the case that the object is an instance of a \Twig\Markup object it is returned directly to support auto escaping.
If an array is passed it is rendered via render() and scalar values are returned directly.
string
safeJoin(Environment $env, array|Traversable|null $value, string $glue = '')
Joins several strings together safely.
Attribute
createAttribute(array $attributes = [])
Creates an Attribute object.
array
withoutFilter(array|object $element)
Removes child elements from a copy of the original array.
Creates a copy of the renderable array and removes child elements by key specified through filter's arguments. The copy can be printed without these elements. The original renderable array is still available and can be used to print child elements in their entirety in the twig template.