diff --git a/Node/SearchAndRenderBlockNode.php b/Node/SearchAndRenderBlockNode.php index 9967639d..fa8653c2 100644 --- a/Node/SearchAndRenderBlockNode.php +++ b/Node/SearchAndRenderBlockNode.php @@ -12,6 +12,7 @@ namespace Symfony\Bridge\Twig\Node; use Twig\Compiler; +use Twig\Extension\CoreExtension; use Twig\Node\Expression\ArrayExpression; use Twig\Node\Expression\ConstantExpression; use Twig\Node\Expression\FunctionExpression; @@ -50,7 +51,7 @@ public function compile(Compiler $compiler): void $labelIsExpression = false; // Only insert the label into the array if it is not empty - if (!twig_test_empty($label->getAttribute('value'))) { + if (null !== $label->getAttribute('value') && false !== $label->getAttribute('value') && '' !== (string) $label->getAttribute('value')) { $originalVariables = $variables; $variables = new ArrayExpression([], $lineno); $labelKey = new ConstantExpression('label', $lineno); @@ -97,7 +98,12 @@ public function compile(Compiler $compiler): void // Check at runtime whether the label is empty. // If not, add it to the array at runtime. - $compiler->raw('(twig_test_empty($_label_ = '); + if (method_exists(CoreExtension::class, 'testEmpty')) { + $compiler->raw('(CoreExtension::testEmpty($_label_ = '); + } else { + $compiler->raw('(twig_test_empty($_label_ = '); + } + $compiler->subcompile($label); $compiler->raw(') ? [] : ["label" => $_label_])'); } diff --git a/Tests/Node/SearchAndRenderBlockNodeTest.php b/Tests/Node/SearchAndRenderBlockNodeTest.php index 42cb1762..b259990e 100644 --- a/Tests/Node/SearchAndRenderBlockNodeTest.php +++ b/Tests/Node/SearchAndRenderBlockNodeTest.php @@ -15,6 +15,7 @@ use Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode; use Twig\Compiler; use Twig\Environment; +use Twig\Extension\CoreExtension; use Twig\Loader\LoaderInterface; use Twig\Node\Expression\ArrayExpression; use Twig\Node\Expression\ConditionalExpression; @@ -226,8 +227,9 @@ public function testCompileLabelWithLabelThatEvaluatesToNull() // https://github.com/symfony/symfony/issues/5029 $this->assertEquals( sprintf( - '$this->env->getRuntime(\'Symfony\Component\Form\FormRenderer\')->searchAndRenderBlock(%s, \'label\', (twig_test_empty($_label_ = ((true) ? (null) : (null))) ? [] : ["label" => $_label_]))', - $this->getVariableGetter('form') + '$this->env->getRuntime(\'Symfony\Component\Form\FormRenderer\')->searchAndRenderBlock(%s, \'label\', (%s($_label_ = ((true) ? (null) : (null))) ? [] : ["label" => $_label_]))', + $this->getVariableGetter('form'), + method_exists(CoreExtension::class, 'testEmpty') ? 'CoreExtension::testEmpty' : 'twig_test_empty' ), trim($compiler->compile($node)->getSource()) ); @@ -263,8 +265,9 @@ public function testCompileLabelWithLabelThatEvaluatesToNullAndAttributes() // https://github.com/symfony/symfony/issues/5029 $this->assertEquals( sprintf( - '$this->env->getRuntime(\'Symfony\Component\Form\FormRenderer\')->searchAndRenderBlock(%s, \'label\', ["foo" => "bar", "label" => "value in attributes"] + (twig_test_empty($_label_ = ((true) ? (null) : (null))) ? [] : ["label" => $_label_]))', - $this->getVariableGetter('form') + '$this->env->getRuntime(\'Symfony\Component\Form\FormRenderer\')->searchAndRenderBlock(%s, \'label\', ["foo" => "bar", "label" => "value in attributes"] + (%s($_label_ = ((true) ? (null) : (null))) ? [] : ["label" => $_label_]))', + $this->getVariableGetter('form'), + method_exists(CoreExtension::class, 'testEmpty') ? 'CoreExtension::testEmpty' : 'twig_test_empty' ), trim($compiler->compile($node)->getSource()) );