Commit d2e0ae49 authored by lauriii's avatar lauriii
Browse files

Issue #2558079 by Aki Tendo, Jo Fitzgerald, Wim Leers, borisson_: Assertions...

Issue #2558079 by Aki Tendo, Jo Fitzgerald, Wim Leers, borisson_: Assertions in DrupalTemplateTwigExtension
parent 29a2728e
...@@ -232,6 +232,8 @@ public function getName() { ...@@ -232,6 +232,8 @@ public function getName() {
* @see \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute() * @see \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute()
*/ */
public function getPath($name, $parameters = [], $options = []) { public function getPath($name, $parameters = [], $options = []) {
assert($this->urlGenerator instanceof UrlGeneratorInterface, "The URL generator hasn't been set up. Any configuration YAML file with a service directive dealing with the Twig configuration can cause this, most likely found in a recently installed or changed module.");
$options['absolute'] = FALSE; $options['absolute'] = FALSE;
return $this->urlGenerator->generateFromRoute($name, $parameters, $options); return $this->urlGenerator->generateFromRoute($name, $parameters, $options);
} }
...@@ -253,6 +255,8 @@ public function getPath($name, $parameters = [], $options = []) { ...@@ -253,6 +255,8 @@ public function getPath($name, $parameters = [], $options = []) {
* @todo Add an option for scheme-relative URLs. * @todo Add an option for scheme-relative URLs.
*/ */
public function getUrl($name, $parameters = [], $options = []) { public function getUrl($name, $parameters = [], $options = []) {
assert($this->urlGenerator instanceof UrlGeneratorInterface, "The URL generator hasn't been set up. Any configuration YAML file with a service directive dealing with the Twig configuration can cause this, most likely found in a recently installed or changed module.");
// Generate URL. // Generate URL.
$options['absolute'] = TRUE; $options['absolute'] = TRUE;
$generated_url = $this->urlGenerator->generateFromRoute($name, $parameters, $options, TRUE); $generated_url = $this->urlGenerator->generateFromRoute($name, $parameters, $options, TRUE);
...@@ -277,6 +281,9 @@ public function getUrl($name, $parameters = [], $options = []) { ...@@ -277,6 +281,9 @@ public function getUrl($name, $parameters = [], $options = []) {
* A render array representing a link to the given URL. * A render array representing a link to the given URL.
*/ */
public function getLink($text, $url, $attributes = []) { public function getLink($text, $url, $attributes = []) {
assert(is_string($url) || $url instanceof Url, '$url must be a string or object of type \Drupal\Core\Url');
assert(is_array($attributes) || $attributes instanceof Attribute, '$attributes, if set, must be an array or object of type \Drupal\Core\Template\Attribute');
if (!$url instanceof Url) { if (!$url instanceof Url) {
$url = Url::fromUri($url); $url = Url::fromUri($url);
} }
...@@ -374,6 +381,8 @@ public function isUrlGenerationSafe(\Twig_Node $args_node) { ...@@ -374,6 +381,8 @@ public function isUrlGenerationSafe(\Twig_Node $args_node) {
* An asset library. * An asset library.
*/ */
public function attachLibrary($library) { public function attachLibrary($library) {
assert(is_string($library), 'Argument must be a string.');
// Use Renderer::render() on a temporary render array to get additional // Use Renderer::render() on a temporary render array to get additional
// bubbleable metadata on the render stack. // bubbleable metadata on the render stack.
$template_attached = ['#attached' => ['library' => [$library]]]; $template_attached = ['#attached' => ['library' => [$library]]];
...@@ -391,8 +400,10 @@ public function attachLibrary($library) { ...@@ -391,8 +400,10 @@ public function attachLibrary($library) {
* @return string|null * @return string|null
* The escaped, rendered output, or NULL if there is no valid output. * The escaped, rendered output, or NULL if there is no valid output.
*/ */
public function escapePlaceholder($env, $string) { public function escapePlaceholder(\Twig_Environment $env, $string) {
return '<em class="placeholder">' . $this->escapeFilter($env, $string) . '</em>'; $return = $this->escapeFilter($env, $string);
return $return ? '<em class="placeholder">' . $return . '</em>' : NULL;
} }
/** /**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment