Refactor Twig extensions to use attributes for declaring functions and filters, remove AbstractExtension inheritance, and clean up related service definitions.

This commit is contained in:
2025-12-19 12:23:20 +01:00
parent 0d42ed9262
commit 741f655cfc
19 changed files with 72 additions and 244 deletions

View File

@@ -22,7 +22,7 @@ use Twig\TwigFunction;
* * chill_custom_field_widget : to render the value of the custom field,
* * chill_custom_field_label : to render the label of the custom field,.
*/
class CustomFieldRenderingTwig extends AbstractExtension
class CustomFieldRenderingTwig
{
/**
* @var array<string, string> The default parameters
@@ -33,29 +33,6 @@ class CustomFieldRenderingTwig extends AbstractExtension
public function __construct(private readonly CustomFieldsHelper $customFieldsHelper) {}
/**
* (non-PHPdoc).
*
* @see Twig_Extension::getFunctions()
*/
#[\Override]
public function getFunctions()
{
return [
new TwigFunction('chill_custom_field_widget', $this->renderWidget(...), [
'is_safe' => [
'html',
],
]),
new TwigFunction('chill_custom_field_label', $this->renderLabel(...), [
'is_safe' => [
'html',
],
'needs_environment' => true,
]),
];
}
/** (non-PHPdoc).
* @see Twig_ExtensionInterface::getName()
*/
@@ -79,6 +56,9 @@ class CustomFieldRenderingTwig extends AbstractExtension
*
* @return string HTML representation of the custom field label
*/
#[\Twig\Attribute\AsTwigFunction('chill_custom_field_label', isSafe: [
'html',
], needsEnvironment: true)]
public function renderLabel(Environment $env, CustomField $customField, array $params = []): string
{
$resolvedParams = array_merge($this->defaultParams, $params);
@@ -97,6 +77,9 @@ class CustomFieldRenderingTwig extends AbstractExtension
*
* @return string HTML representation of the custom field value, as described in the CustomFieldInterface. Is HTML safe
*/
#[\Twig\Attribute\AsTwigFunction('chill_custom_field_widget', isSafe: [
'html',
])]
public function renderWidget(array $fields, CustomField $customField, $documentType = 'html')
{
return $this->customFieldsHelper