Refactor ChillTwigRoutingHelper to extend AbstractExtension and improve Twig functionality

- Updated `ChillTwigRoutingHelper` to extend `AbstractExtension` for consistency with Twig best practices.
- Moved `chill_return_path_label` from a Twig attribute to a filter within `getFilters`.
- Removed unnecessary dependency on `RequestStack` in `services/templating.yaml`.
This commit is contained in:
2025-11-03 15:41:54 +01:00
parent 590f7c1055
commit adab2ffe63
2 changed files with 14 additions and 4 deletions

View File

@@ -13,7 +13,12 @@ namespace Chill\MainBundle\Templating;
use Symfony\Bridge\Twig\Extension\RoutingExtension;
use Symfony\Component\HttpFoundation\RequestStack;
use Twig\Attribute\AsTwigFunction;
use Twig\ExpressionParser\ExpressionParserInterface;
use Twig\Extension\AbstractExtension;
use Twig\Extension\ExtensionInterface;
use Twig\Node\Node;
use Twig\TwigFilter;
use Twig\TwigFunction;
/**
@@ -21,7 +26,7 @@ use Twig\TwigFunction;
*
* The logic of the function is based on the original routing extension.
*/
class ChillTwigRoutingHelper
class ChillTwigRoutingHelper extends AbstractExtension
{
/**
* @var RoutingExtension
@@ -41,7 +46,7 @@ class ChillTwigRoutingHelper
$this->originalExtension = $originalExtension;
}
public function getFunctions()
public function getFunctions(): array
{
return [
new TwigFunction('chill_return_path_or', $this->getReturnPathOr(...), ['is_safe_callback' => $this->isUrlGenerationSafe(...)]),
@@ -73,7 +78,6 @@ class ChillTwigRoutingHelper
return $this->originalExtension->getPath($name, $params, $relative);
}
#[\Twig\Attribute\AsTwigFilter('chill_return_path_label')]
public function getLabelReturnPath($default)
{
$request = $this->requestStack->getCurrentRequest();
@@ -149,4 +153,11 @@ class ChillTwigRoutingHelper
{
return $this->originalExtension->isUrlGenerationSafe($argsNode);
}
public function getFilters(): array
{
return [
new TwigFilter('chill_return_path_label', $this->getLabelReturnPath(...)),
];
}
}

View File

@@ -26,7 +26,6 @@ services:
Chill\MainBundle\Templating\ChillTwigRoutingHelper:
arguments:
$requestStack: '@Symfony\Component\HttpFoundation\RequestStack'
$originalExtension: '@twig.extension.routing'
autoconfigure: true
autowire: true