comparison 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
comparison
equal deleted inserted replaced
17:129ea1e6d783 18:af1871eacc83
175 // @todo Make that the default for |join? Upstream issue: 175 // @todo Make that the default for |join? Upstream issue:
176 // https://github.com/fabpot/Twig/issues/1420 176 // https://github.com/fabpot/Twig/issues/1420
177 new \Twig_SimpleFilter('safe_join', [$this, 'safeJoin'], ['needs_environment' => TRUE, 'is_safe' => ['html']]), 177 new \Twig_SimpleFilter('safe_join', [$this, 'safeJoin'], ['needs_environment' => TRUE, 'is_safe' => ['html']]),
178 178
179 // Array filters. 179 // Array filters.
180 new \Twig_SimpleFilter('without', 'twig_without'), 180 new \Twig_SimpleFilter('without', [$this, 'withoutFilter']),
181 181
182 // CSS class and ID filters. 182 // CSS class and ID filters.
183 new \Twig_SimpleFilter('clean_class', '\Drupal\Component\Utility\Html::getClass'), 183 new \Twig_SimpleFilter('clean_class', '\Drupal\Component\Utility\Html::getClass'),
184 new \Twig_SimpleFilter('clean_id', '\Drupal\Component\Utility\Html::getId'), 184 new \Twig_SimpleFilter('clean_id', '\Drupal\Component\Utility\Html::getId'),
185 // This filter will render a renderable array to use the string results. 185 // This filter will render a renderable array to use the string results.
230 * The generated URL path (relative URL) for the given route. 230 * The generated URL path (relative URL) for the given route.
231 * 231 *
232 * @see \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute() 232 * @see \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute()
233 */ 233 */
234 public function getPath($name, $parameters = [], $options = []) { 234 public function getPath($name, $parameters = [], $options = []) {
235 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.");
236
235 $options['absolute'] = FALSE; 237 $options['absolute'] = FALSE;
236 return $this->urlGenerator->generateFromRoute($name, $parameters, $options); 238 return $this->urlGenerator->generateFromRoute($name, $parameters, $options);
237 } 239 }
238 240
239 /** 241 /**
251 * The generated absolute URL for the given route. 253 * The generated absolute URL for the given route.
252 * 254 *
253 * @todo Add an option for scheme-relative URLs. 255 * @todo Add an option for scheme-relative URLs.
254 */ 256 */
255 public function getUrl($name, $parameters = [], $options = []) { 257 public function getUrl($name, $parameters = [], $options = []) {
258 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.");
259
256 // Generate URL. 260 // Generate URL.
257 $options['absolute'] = TRUE; 261 $options['absolute'] = TRUE;
258 $generated_url = $this->urlGenerator->generateFromRoute($name, $parameters, $options, TRUE); 262 $generated_url = $this->urlGenerator->generateFromRoute($name, $parameters, $options, TRUE);
259 263
260 // Return as render array, so we can bubble the bubbleable metadata. 264 // Return as render array, so we can bubble the bubbleable metadata.
275 * 279 *
276 * @return array 280 * @return array
277 * A render array representing a link to the given URL. 281 * A render array representing a link to the given URL.
278 */ 282 */
279 public function getLink($text, $url, $attributes = []) { 283 public function getLink($text, $url, $attributes = []) {
284 assert(is_string($url) || $url instanceof Url, '$url must be a string or object of type \Drupal\Core\Url');
285 assert(is_array($attributes) || $attributes instanceof Attribute, '$attributes, if set, must be an array or object of type \Drupal\Core\Template\Attribute');
286
280 if (!$url instanceof Url) { 287 if (!$url instanceof Url) {
281 $url = Url::fromUri($url); 288 $url = Url::fromUri($url);
282 } 289 }
283 // The twig extension should not modify the original URL object, this 290 // The twig extension should not modify the original URL object, this
284 // ensures consistent rendering. 291 // ensures consistent rendering.
372 * 379 *
373 * @param string $library 380 * @param string $library
374 * An asset library. 381 * An asset library.
375 */ 382 */
376 public function attachLibrary($library) { 383 public function attachLibrary($library) {
384 assert(is_string($library), 'Argument must be a string.');
385
377 // Use Renderer::render() on a temporary render array to get additional 386 // Use Renderer::render() on a temporary render array to get additional
378 // bubbleable metadata on the render stack. 387 // bubbleable metadata on the render stack.
379 $template_attached = ['#attached' => ['library' => [$library]]]; 388 $template_attached = ['#attached' => ['library' => [$library]]];
380 $this->renderer->render($template_attached); 389 $this->renderer->render($template_attached);
381 } 390 }
389 * The value to be escaped. 398 * The value to be escaped.
390 * 399 *
391 * @return string|null 400 * @return string|null
392 * The escaped, rendered output, or NULL if there is no valid output. 401 * The escaped, rendered output, or NULL if there is no valid output.
393 */ 402 */
394 public function escapePlaceholder($env, $string) { 403 public function escapePlaceholder(\Twig_Environment $env, $string) {
395 return '<em class="placeholder">' . $this->escapeFilter($env, $string) . '</em>'; 404 $return = $this->escapeFilter($env, $string);
405
406 return $return ? '<em class="placeholder">' . $return . '</em>' : NULL;
396 } 407 }
397 408
398 /** 409 /**
399 * Overrides twig_escape_filter(). 410 * Overrides twig_escape_filter().
400 * 411 *
628 */ 639 */
629 public function createAttribute(array $attributes = []) { 640 public function createAttribute(array $attributes = []) {
630 return new Attribute($attributes); 641 return new Attribute($attributes);
631 } 642 }
632 643
644 /**
645 * Removes child elements from a copy of the original array.
646 *
647 * Creates a copy of the renderable array and removes child elements by key
648 * specified through filter's arguments. The copy can be printed without these
649 * elements. The original renderable array is still available and can be used
650 * to print child elements in their entirety in the twig template.
651 *
652 * @param array|object $element
653 * The parent renderable array to exclude the child items.
654 * @param string[] ...
655 * The string keys of $element to prevent printing.
656 *
657 * @return array
658 * The filtered renderable array.
659 */
660 public function withoutFilter($element) {
661 if ($element instanceof \ArrayAccess) {
662 $filtered_element = clone $element;
663 }
664 else {
665 $filtered_element = $element;
666 }
667 $args = func_get_args();
668 unset($args[0]);
669 foreach ($args as $arg) {
670 if (isset($filtered_element[$arg])) {
671 unset($filtered_element[$arg]);
672 }
673 }
674 return $filtered_element;
675 }
676
633 } 677 }