Drupal 8  8.0.2
TwigExtension Class Reference
Inheritance diagram for TwigExtension:

Public Member Functions

 __construct (RendererInterface $renderer)
 
 setGenerators (UrlGeneratorInterface $url_generator)
 
 setUrlGenerator (UrlGeneratorInterface $url_generator)
 
 setThemeManager (ThemeManagerInterface $theme_manager)
 
 setDateFormatter (DateFormatter $date_formatter)
 
 getFunctions ()
 
 getFilters ()
 
 getNodeVisitors ()
 
 getTokenParsers ()
 
 getName ()
 
 getPath ($name, $parameters=array(), $options=array())
 
 getUrl ($name, $parameters=array(), $options=array())
 
 getLink ($text, $url, $attributes=[])
 
 getActiveTheme ()
 
 getActiveThemePath ()
 
 isUrlGenerationSafe (\Twig_Node $args_node)
 
 attachLibrary ($library)
 
 escapePlaceholder ($env, $string)
 
 escapeFilter (\Twig_Environment $env, $arg, $strategy= 'html', $charset=NULL, $autoescape=FALSE)
 
 renderVar ($arg)
 
 safeJoin (\Twig_Environment $env, $value, $glue= '')
 

Protected Attributes

 $urlGenerator
 
 $renderer
 
 $themeManager
 
 $dateFormatter
 

Detailed Description

A class providing Drupal Twig extensions.

Specifically Twig functions, filter and node visitors.

See Also

Constructor & Destructor Documentation

__construct ( RendererInterface  $renderer)

Constructs .

Parameters
\Drupal\Core\Render\RendererInterface$rendererThe renderer.

Member Function Documentation

attachLibrary (   $library)

Attaches an asset library to the template, and hence to the response.

Allows Twig templates to attach asset libraries using

{{ attach_library('extension/library_name') }}
Parameters
string$libraryAn asset library.
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().

Parameters
\Twig_Environment$envA Twig_Environment instance.
mixed$argThe value to be escaped.
string$strategyThe escaping strategy. Defaults to 'html'.
string$charsetThe charset.
bool$autoescapeWhether the function is called by the auto-escaping feature (TRUE) or by the developer (FALSE).
Returns
string|null The escaped, rendered output, or NULL if there is no valid output.
Todo:
Refactor this to keep it in sync with theme_render_and_autoescape() in https://www.drupal.org/node/2575065

References Html\escape(), and SafeMarkup\isSafe().

Referenced by TwigExtension\escapePlaceholder(), and TwigExtension\safeJoin().

Here is the call graph for this function:

Here is the caller graph for this function:

escapePlaceholder (   $env,
  $string 
)

Provides a placeholder wrapper around ::escapeFilter.

Parameters
\Twig_Environment$envA Twig_Environment instance.
mixed$stringThe value to be escaped.
Returns
string|null The escaped, rendered output, or NULL if there is no valid output.

References TwigExtension\escapeFilter().

Here is the call graph for this function:

getActiveTheme ( )

Gets the name of the active theme.

Returns
string The name of the active theme.
getActiveThemePath ( )

Gets the path of the active theme.

Returns
string The path to the active theme.
getFilters ( )

{}

getFunctions ( )

{}

getLink (   $text,
  $url,
  $attributes = [] 
)

Gets a rendered link from an url object.

Parameters
string$textThe link text for the anchor tag as a translated string.
\Drupal\Core\Url | string$urlThe URL object or string used for the link.
array | \Drupal\Core\Template\Attribute$attributesAn optional array or Attribute object of link attributes.
Returns
array A render array representing a link to the given URL.

References Url\fromUri().

Here is the call graph for this function:

getName ( )

{}

getNodeVisitors ( )

{}

getPath (   $name,
  $parameters = array(),
  $options = array() 
)

Generates a URL path given a route name and parameters.

Parameters
$nameThe name of the route.
array$parametersAn associative array of route parameters names and values.
array$options(optional) An associative array of additional options. The 'absolute' option is forced to be FALSE.
See Also
::generateFromRoute().
Returns
string The generated URL path (relative URL) for the given route.

References Drupal\urlGenerator().

Here is the call graph for this function:

getTokenParsers ( )

{}

getUrl (   $name,
  $parameters = array(),
  $options = array() 
)

Generates an absolute URL given a route name and parameters.

Parameters
$nameThe name of the route.
array$parametersAn associative array of route parameter names and values.
array$options(optional) An associative array of additional options. The 'absolute' option is forced to be TRUE.
Returns
string The generated absolute URL for the given route.
Todo:
Add an option for scheme-relative URLs.

References Drupal\urlGenerator().

Here is the call graph for this function:

isUrlGenerationSafe ( \Twig_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 an 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.
Parameters
\Twig_Node$args_nodeThe arguments of the path/url functions.
Returns
array An array with the contexts the URL is safe
renderVar (   $arg)

Wrapper around render() for twig printed output.

If an object is passed that has no __toString method 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.

Parameters
mixed$argString, Object or Render Array.
Returns
mixed The rendered output or an Twig_Markup object.
See Also
render
TwigNodeVisitor
safeJoin ( \Twig_Environment  $env,
  $value,
  $glue = '' 
)

Joins several strings together safely.

Parameters
\Twig_Environment$envA Twig_Environment instance.
mixed[]|\Traversable|NULL$value The pieces to join.
string$glueThe delimiter with which to join the string. Defaults to an empty string. This value is expected to be safe for output and user provided data should never be used as a glue.
Returns
string The strings joined together.

References TwigExtension\escapeFilter().

Here is the call graph for this function:

setDateFormatter ( DateFormatter  $date_formatter)

Sets the date formatter.

Parameters
\Drupal\Core\Datetime\DateFormatter$date_formatterThe date formatter.
Returns
$this
setGenerators ( UrlGeneratorInterface  $url_generator)

Sets the URL generator.

Parameters
\Drupal\Core\Routing\UrlGeneratorInterface$url_generatorThe URL generator.
Returns
$this
Deprecated:
in Drupal 8.0.x-dev, will be removed before Drupal 9.0.0. Use ::setUrlGenerator().

References TwigExtension\setUrlGenerator().

Here is the call graph for this function:

setThemeManager ( ThemeManagerInterface  $theme_manager)

Sets the theme manager.

Parameters
\Drupal\Core\Theme\ThemeManagerInterface$theme_managerThe theme manager.
Returns
$this
setUrlGenerator ( UrlGeneratorInterface  $url_generator)

Sets the URL generator.

Parameters
\Drupal\Core\Routing\UrlGeneratorInterface$url_generatorThe URL generator.
Returns
$this

References Drupal\urlGenerator().

Referenced by TwigExtension\setGenerators().

Here is the call graph for this function:

Here is the caller graph for this function:


The documentation for this class was generated from the following file: