Skip to content

Commit 05e5bd9

Browse files
committed
Issue #3477373 by finnsky, bbrala, longwave: Fix "Not passing an instance of "TwigFunction" when creating a function of type "FunctionExpression" is deprecated."
1 parent f723b40 commit 05e5bd9

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

.deprecation-ignore.txt

-4
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,4 @@
2929
%The "PHPUnit\\Framework\\TestCase::__construct\(\)" method is considered internal.*You should not extend it from "Drupal\\[^"]+"%
3030

3131
# Twig 3.
32-
%Since twig/twig 3.12: Not passing an instance of "TwigFunction" when creating a "attach_library" function of type "Twig\\Node\\Expression\\FunctionExpression" is deprecated.%
33-
%Since twig/twig 3.12: Not passing an instance of "TwigFunction" when creating a "add_component_context" function of type "Twig\\Node\\Expression\\FunctionExpression" is deprecated.%
34-
%Since twig/twig 3.12: Not passing an instance of "TwigFunction" when creating a "render_var" function of type "Twig\\Node\\Expression\\FunctionExpression" is deprecated.%
35-
%Since twig/twig 3.12: Not passing an instance of "TwigFunction" when creating a "validate_component_props" function of type "Twig\\Node\\Expression\\FunctionExpression" is deprecated.%
3632
%Since twig/twig 3.12: Twig Filter "spaceless" is deprecated%

lib/Drupal/Core/Template/ComponentNodeVisitor.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Drupal\Core\Render\Component\Exception\InvalidComponentException;
1010
use Drupal\Core\Theme\ComponentPluginManager;
1111
use Twig\Environment;
12+
use Twig\TwigFunction;
1213
use Twig\Node\Expression\ConstantExpression;
1314
use Twig\Node\Expression\FunctionExpression;
1415
use Twig\Node\ModuleNode;
@@ -55,17 +56,17 @@ public function leaveNode(Node $node, Environment $env): ?Node {
5556
$print_nodes[] = new PrintNode(new ConstantExpression(sprintf('<!-- %s Component start: %s -->', $emoji, $component_id), $line), $line);
5657
}
5758
$print_nodes[] = new PrintNode(new FunctionExpression(
58-
'attach_library',
59+
new TwigFunction('attach_library', [$env->getExtension(TwigExtension::class), 'attachLibrary']),
5960
new Node([new ConstantExpression($component->getLibraryName(), $line)]),
6061
$line
6162
), $line);
6263
$print_nodes[] = new PrintNode(new FunctionExpression(
63-
'add_component_context',
64+
new TwigFunction('add_component_context', [$env->getExtension(ComponentsTwigExtension::class), 'addAdditionalContext'], ['needs_context' => TRUE]),
6465
new Node([new ConstantExpression($component_id, $line)]),
6566
$line
6667
), $line);
6768
$print_nodes[] = new PrintNode(new FunctionExpression(
68-
'validate_component_props',
69+
new TwigFunction('validate_component_props', [$env->getExtension(ComponentsTwigExtension::class), 'validateProps'], ['needs_context' => TRUE]),
6970
new Node([new ConstantExpression($component_id, $line)]),
7071
$line
7172
), $line);

lib/Drupal/Core/Template/TwigNodeVisitor.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Drupal\Core\Template;
44

55
use Twig\Environment;
6+
use Twig\TwigFunction;
67
use Twig\Node\Expression\FilterExpression;
78
use Twig\Node\Expression\FunctionExpression;
89
use Twig\Node\Node;
@@ -47,7 +48,11 @@ public function leaveNode(Node $node, Environment $env): ?Node {
4748
$class = get_class($node);
4849
$line = $node->getTemplateLine();
4950
return new $class(
50-
new FunctionExpression('render_var', new Node([$node->getNode('expr')]), $line),
51+
new FunctionExpression(
52+
new TwigFunction('render_var', [$env->getExtension(TwigExtension::class), 'renderVar']),
53+
new Node([$node->getNode('expr')]),
54+
$line
55+
),
5156
$line
5257
);
5358
}

0 commit comments

Comments
 (0)