Mercurial > hg > isophonics-drupal-site
diff core/lib/Drupal/Core/Template/TwigExtension.php @ 18:af1871eacc83
Update to Drupal core 8.7.1
author | Chris Cannam |
---|---|
date | Thu, 09 May 2019 15:33:08 +0100 |
parents | 4c8ae668cc8c |
children |
line wrap: on
line diff
--- a/core/lib/Drupal/Core/Template/TwigExtension.php Thu Feb 28 13:21:36 2019 +0000 +++ b/core/lib/Drupal/Core/Template/TwigExtension.php Thu May 09 15:33:08 2019 +0100 @@ -177,7 +177,7 @@ new \Twig_SimpleFilter('safe_join', [$this, 'safeJoin'], ['needs_environment' => TRUE, 'is_safe' => ['html']]), // Array filters. - new \Twig_SimpleFilter('without', 'twig_without'), + new \Twig_SimpleFilter('without', [$this, 'withoutFilter']), // CSS class and ID filters. new \Twig_SimpleFilter('clean_class', '\Drupal\Component\Utility\Html::getClass'), @@ -232,6 +232,8 @@ * @see \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute() */ 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; return $this->urlGenerator->generateFromRoute($name, $parameters, $options); } @@ -253,6 +255,8 @@ * @todo Add an option for scheme-relative URLs. */ 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. $options['absolute'] = TRUE; $generated_url = $this->urlGenerator->generateFromRoute($name, $parameters, $options, TRUE); @@ -277,6 +281,9 @@ * A render array representing a link to the given URL. */ 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) { $url = Url::fromUri($url); } @@ -374,6 +381,8 @@ * An asset 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 // bubbleable metadata on the render stack. $template_attached = ['#attached' => ['library' => [$library]]]; @@ -391,8 +400,10 @@ * @return string|null * The escaped, rendered output, or NULL if there is no valid output. */ - public function escapePlaceholder($env, $string) { - return '<em class="placeholder">' . $this->escapeFilter($env, $string) . '</em>'; + public function escapePlaceholder(\Twig_Environment $env, $string) { + $return = $this->escapeFilter($env, $string); + + return $return ? '<em class="placeholder">' . $return . '</em>' : NULL; } /** @@ -630,4 +641,37 @@ return new Attribute($attributes); } + /** + * 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. + * + * @param array|object $element + * The parent renderable array to exclude the child items. + * @param string[] ... + * The string keys of $element to prevent printing. + * + * @return array + * The filtered renderable array. + */ + public function withoutFilter($element) { + if ($element instanceof \ArrayAccess) { + $filtered_element = clone $element; + } + else { + $filtered_element = $element; + } + $args = func_get_args(); + unset($args[0]); + foreach ($args as $arg) { + if (isset($filtered_element[$arg])) { + unset($filtered_element[$arg]); + } + } + return $filtered_element; + } + }