Skip to content

[Symfony73] Keep AbstractExtension when Twig extension overrides a built-in function/filter#968

Merged
TomasVotruba merged 1 commit into
mainfrom
skip-builtin-twig-override
Jul 23, 2026
Merged

[Symfony73] Keep AbstractExtension when Twig extension overrides a built-in function/filter#968
TomasVotruba merged 1 commit into
mainfrom
skip-builtin-twig-override

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

When a Twig extension overrides a built-in Twig function/filter (e.g. include, date, escape), the override only works while the class stays a classic extends AbstractExtension. The #[AsTwigFunction] / #[AsTwigFilter] attribute-based registration cannot override core Twig callables the same way.

GetFunctionsToAsTwigFunctionAttributeRector (and its filter sibling, via the shared transformer) now skips such built-in-named items: they stay in the array and the class keeps extends AbstractExtension. Other, non-built-in items in the same class are still converted.

Skip a pure built-in override

final class OverrideIncludeExtension extends AbstractExtension
{
    public function getFunctions(): array
    {
        return [
            // Override the built-in include function with higher priority
            new TwigFunction('include', $this->includeWithEvent(...), [
                'needs_environment' => true,
                'needs_context'     => true,
                'is_safe'           => ['html'],
            ]),
        ];
    }
    // ...
}

No change — the class keeps AbstractExtension because include is a built-in.

Convert custom, keep built-in

 final class KeepBuiltinConvertCustom extends AbstractExtension
 {
     public function getFunctions(): array
     {
         return [
             new TwigFunction('include', $this->includeWithEvent(...), ['needs_environment' => true]),
-            new TwigFunction('custom_function', [$this, 'customFunction']),
         ];
     }

     public function includeWithEvent(Environment $env, $template)
     {
         return $template;
     }

+    #[\Twig\Attribute\AsTwigFunction(name: 'custom_function')]
     public function customFunction($value)
     {
         return $value;
     }
 }

The custom function becomes an attribute; the include override stays registered the classic way, so AbstractExtension is preserved.

@TomasVotruba
TomasVotruba merged commit eadffcf into main Jul 23, 2026
8 checks passed
@TomasVotruba
TomasVotruba deleted the skip-builtin-twig-override branch July 23, 2026 12:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant