Browse Source

Issue #3135932 by mxwright: Twig variable in PHP filter

merge-requests/4/head
Chi 4 years ago
parent
commit
21b6079272
  1. 11
      src/TwigExtension.php

11
src/TwigExtension.php

@ -405,7 +405,7 @@ class TwigExtension extends \Twig_Extension {
// @code // @code
// {{ 'now'|date('Y') }} // {{ 'now'|date('Y') }}
// @endcode // @endcode
$filters[] = new \Twig_SimpleFilter('php', [$this, 'phpFilter']); $filters[] = new \Twig_SimpleFilter('php', [$this, 'phpFilter'], ['needs_context' => TRUE]);
} }
return $filters; return $filters;
} }
@ -1286,17 +1286,20 @@ class TwigExtension extends \Twig_Extension {
/** /**
* Evaluates a string of PHP code. * Evaluates a string of PHP code.
* *
* @param array $context
* Twig context.
* @param string $code * @param string $code
* Valid PHP code to be evaluated. * Valid PHP code to be evaluated.
* *
* @return mixed * @return mixed
* The eval() result. * The eval() result.
*/ */
public function phpFilter($code) { public function phpFilter(array $context, $code) {
// Make Twig variables available in PHP code.
extract($context);
ob_start(); ob_start();
// @codingStandardsIgnoreStart // phpcs:ignore Drupal.Functions.DiscouragedFunctions.Discouraged
print eval($code); print eval($code);
// @codingStandardsIgnoreEnd
$output = ob_get_contents(); $output = ob_get_contents();
ob_end_clean(); ob_end_clean();
return $output; return $output;

Loading…
Cancel
Save