diff --git a/doc/deprecated.rst b/doc/deprecated.rst index a63b447bb..a648b054a 100644 --- a/doc/deprecated.rst +++ b/doc/deprecated.rst @@ -11,9 +11,8 @@ Functions * The ``twig_test_iterable`` function is deprecated; use the native PHP ``is_iterable`` function instead. -* The ``attribute`` function is deprecated as of Twig 3.15 and will be removed - in Twig 4.0. Use the ``.`` operator instead and wrap the name with - parenthesis: +* The ``attribute`` function is deprecated as of Twig 3.15. Use the ``.`` + operator instead and wrap the name with parenthesis: .. code-block:: twig @@ -27,6 +26,8 @@ Functions {{ object.(method)(arguments) }} {{ array[item] }} + Note that it won't be removed in 4.0 to allow a smoother upgrade path. + Extensions ---------- diff --git a/doc/functions/attribute.rst b/doc/functions/attribute.rst index e1693b2a1..6587921da 100644 --- a/doc/functions/attribute.rst +++ b/doc/functions/attribute.rst @@ -5,6 +5,8 @@ The ``attribute`` filter is deprecated as of Twig 3.15. Use the ``.`` operator that now accepts any expression when wrapped with parenthesis. + Note that this filter will still be available in Twig 4.0 to allow a + smoother upgrade path. The ``attribute`` function can be used to access a "dynamic" attribute of a variable: diff --git a/src/Extension/CoreExtension.php b/src/Extension/CoreExtension.php index 664f1eee2..e0e6ed5d3 100644 --- a/src/Extension/CoreExtension.php +++ b/src/Extension/CoreExtension.php @@ -1966,11 +1966,14 @@ final class CoreExtension extends AbstractExtension $fakeFunction = new TwigFunction('attribute', fn ($variable, $attribute, $arguments = null) => null); $args = (new CallableArgumentsExtractor($fakeNode, $fakeFunction))->extractArguments($args); + /* + Deprecation to uncomment sometimes during the lifetime of the 4.x branch $src = $parser->getStream()->getSourceContext(); $dep = new DeprecatedCallableInfo('twig/twig', '3.15', 'The "attribute" function is deprecated, use the "." notation instead.'); $dep->setName('attribute'); $dep->setType('function'); $dep->triggerDeprecation($src->getPath() ?: $src->getName(), $line); + */ return new GetAttrExpression($args[0], $args[1], $args[2] ?? null, Template::ANY_CALL, $line); } diff --git a/tests/Fixtures/functions/attribute.legacy.test b/tests/Fixtures/functions/attribute.legacy.test index e8c5863e9..31cca8c46 100644 --- a/tests/Fixtures/functions/attribute.legacy.test +++ b/tests/Fixtures/functions/attribute.legacy.test @@ -1,16 +1,5 @@ --TEST-- "attribute" function ---DEPRECATION-- -Since twig/twig 3.15: Twig Function "attribute" is deprecated; use "The "attribute" function is deprecated, use the "." notation instead." instead in index.twig at line 2. -Since twig/twig 3.15: Twig Function "attribute" is deprecated; use "The "attribute" function is deprecated, use the "." notation instead." instead in index.twig at line 3. -Since twig/twig 3.15: Twig Function "attribute" is deprecated; use "The "attribute" function is deprecated, use the "." notation instead." instead in index.twig at line 4. -Since twig/twig 3.15: Twig Function "attribute" is deprecated; use "The "attribute" function is deprecated, use the "." notation instead." instead in index.twig at line 5. -Since twig/twig 3.15: Twig Function "attribute" is deprecated; use "The "attribute" function is deprecated, use the "." notation instead." instead in index.twig at line 6. -Since twig/twig 3.15: Twig Function "attribute" is deprecated; use "The "attribute" function is deprecated, use the "." notation instead." instead in index.twig at line 7. -Since twig/twig 3.15: Twig Function "attribute" is deprecated; use "The "attribute" function is deprecated, use the "." notation instead." instead in index.twig at line 8. -Since twig/twig 3.15: Twig Function "attribute" is deprecated; use "The "attribute" function is deprecated, use the "." notation instead." instead in index.twig at line 9. -Since twig/twig 3.15: Twig Function "attribute" is deprecated; use "The "attribute" function is deprecated, use the "." notation instead." instead in index.twig at line 10. -Since twig/twig 3.15: Twig Function "attribute" is deprecated; use "The "attribute" function is deprecated, use the "." notation instead." instead in index.twig at line 11. --TEMPLATE-- {{ attribute(obj, method) }} {{ attribute(variable=obj, attribute=method) }} diff --git a/tests/Fixtures/tests/defined_for_attribute.legacy.test b/tests/Fixtures/tests/defined_for_attribute.legacy.test index 1117ff3e9..5fd2fe3f2 100644 --- a/tests/Fixtures/tests/defined_for_attribute.legacy.test +++ b/tests/Fixtures/tests/defined_for_attribute.legacy.test @@ -1,10 +1,5 @@ --TEST-- "defined" support for attribute ---DEPRECATION-- -Since twig/twig 3.15: Twig Function "attribute" is deprecated; use "The "attribute" function is deprecated, use the "." notation instead." instead in index.twig at line 2. -Since twig/twig 3.15: Twig Function "attribute" is deprecated; use "The "attribute" function is deprecated, use the "." notation instead." instead in index.twig at line 3. -Since twig/twig 3.15: Twig Function "attribute" is deprecated; use "The "attribute" function is deprecated, use the "." notation instead." instead in index.twig at line 4. -Since twig/twig 3.15: Twig Function "attribute" is deprecated; use "The "attribute" function is deprecated, use the "." notation instead." instead in index.twig at line 5. --TEMPLATE-- {{ attribute(nested, "definedVar") is defined ? 'ok' : 'ko' }} {{ attribute(nested, "undefinedVar") is not defined ? 'ok' : 'ko' }}