Browse Source

Issue #3135932 by mxwright: Twig variable in PHP filter

merge-requests/2/head
Chi 5 years ago
parent
commit
47e2509afc
  1. 11
      src/TwigTweakExtension.php

11
src/TwigTweakExtension.php

@ -119,7 +119,7 @@ class TwigTweakExtension extends AbstractExtension {
]; ];
if (Settings::get('twig_tweak_enable_php_filter')) { if (Settings::get('twig_tweak_enable_php_filter')) {
$filters[] = new TwigFilter('php', [self::class, 'phpFilter']); $filters[] = new TwigFilter('php', [self::class, 'phpFilter'], ['needs_context' => TRUE]);
} }
$this->moduleHandler->alter('twig_tweak_filters', $filters); $this->moduleHandler->alter('twig_tweak_filters', $filters);
@ -622,17 +622,20 @@ class TwigTweakExtension extends AbstractExtension {
/** /**
* 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 static function phpFilter(string $code) { public static function phpFilter(array $context, string $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