Fix usage of twig environment in twig custom fields rendering extensions

This commit is contained in:
Julien Fastré 2023-08-02 22:47:56 +02:00
parent dbccf7ff80
commit e2b500ea5f
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
3 changed files with 15 additions and 40 deletions

View File

@ -16,6 +16,7 @@ use Chill\CustomFieldsBundle\Service\CustomFieldsHelper;
use Symfony\Component\DependencyInjection\Container; use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerAwareInterface; use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Twig\Environment;
use Twig\Extension\AbstractExtension; use Twig\Extension\AbstractExtension;
use Twig\TwigFunction; use Twig\TwigFunction;
@ -24,15 +25,14 @@ use Twig\TwigFunction;
* * chill_custom_field_widget : to render the value of the custom field, * * chill_custom_field_widget : to render the value of the custom field,
* * chill_custom_field_label : to render the label of the custom field,. * * chill_custom_field_label : to render the label of the custom field,.
*/ */
class CustomFieldRenderingTwig extends AbstractExtension implements ContainerAwareInterface class CustomFieldRenderingTwig extends AbstractExtension
{ {
private ?\Symfony\Component\DependencyInjection\ContainerInterface $container = null;
/** /**
* @var array The default parameters * @var array The default parameters
*/ */
private array $defaultParams = [ private array $defaultParams = [
'label_layout' => 'ChillCustomFieldsBundle:CustomField:render_label.html.twig', 'label_layout' => '@ChillCustomFields/CustomField/render_label.html.twig',
]; ];
public function __construct(private readonly CustomFieldsHelper $customFieldsHelper) public function __construct(private readonly CustomFieldsHelper $customFieldsHelper)
@ -56,6 +56,7 @@ class CustomFieldRenderingTwig extends AbstractExtension implements ContainerAwa
'is_safe' => [ 'is_safe' => [
'html', 'html',
], ],
'needs_environment' => true,
]), ]),
new TwigFunction('chill_custom_field_is_empty', $this->isEmptyValue(...)), new TwigFunction('chill_custom_field_is_empty', $this->isEmptyValue(...)),
]; ];
@ -83,12 +84,11 @@ class CustomFieldRenderingTwig extends AbstractExtension implements ContainerAwa
* *
* @return string HTML representation of the custom field label. * @return string HTML representation of the custom field label.
*/ */
public function renderLabel(CustomField $customField, array $params = []) public function renderLabel(Environment $env, CustomField $customField, array $params = [])
{ {
$resolvedParams = array_merge($this->defaultParams, $params); $resolvedParams = array_merge($this->defaultParams, $params);
return $this->container->get('templating') return $env->render($resolvedParams['label_layout'], ['customField' => $customField]);
->render($resolvedParams['label_layout'], ['customField' => $customField]);
} }
/** /**
@ -107,14 +107,4 @@ class CustomFieldRenderingTwig extends AbstractExtension implements ContainerAwa
return $this->customFieldsHelper return $this->customFieldsHelper
->renderCustomField($fields, $customField, $documentType); ->renderCustomField($fields, $customField, $documentType);
} }
/**
* (non-PHPdoc).
*
* @see \Symfony\Component\DependencyInjection\ContainerAwareInterface::setContainer()
*/
public function setContainer(?ContainerInterface $container = null)
{
$this->container = $container;
}
} }

View File

@ -14,6 +14,7 @@ namespace Chill\CustomFieldsBundle\Templating\Twig;
use Symfony\Component\DependencyInjection\Container; use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerAwareInterface; use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Twig\Environment;
use Twig\Extension\AbstractExtension; use Twig\Extension\AbstractExtension;
use Twig\TwigFunction; use Twig\TwigFunction;
@ -22,15 +23,13 @@ use Twig\TwigFunction;
* * chill_custom_fields_group_widget : to render the value of a custom field * * chill_custom_fields_group_widget : to render the value of a custom field
* * group. * * group.
*/ */
class CustomFieldsGroupRenderingTwig extends AbstractExtension implements ContainerAwareInterface final class CustomFieldsGroupRenderingTwig extends AbstractExtension
{ {
private ?\Symfony\Component\DependencyInjection\ContainerInterface $container = null;
/** /**
* @var array The default parameters * @var array The default parameters
*/ */
private array $defaultParams = [ private array $defaultParams = [
'layout' => 'ChillCustomFieldsBundle:CustomFieldsGroup:render.html.twig', 'layout' => '@ChillCustomFields/CustomFieldsGroup/render.html.twig',
'show_empty' => true, 'show_empty' => true,
]; ];
@ -54,6 +53,7 @@ class CustomFieldsGroupRenderingTwig extends AbstractExtension implements Contai
'is_safe' => [ 'is_safe' => [
'html', 'html',
], ],
'needs_environment' => true,
]), ]),
]; ];
} }
@ -82,24 +82,13 @@ class CustomFieldsGroupRenderingTwig extends AbstractExtension implements Contai
* @return string HTML representation of the custom field group value, as described in * @return string HTML representation of the custom field group value, as described in
* the CustomFieldInterface. Is HTML safe * the CustomFieldInterface. Is HTML safe
*/ */
public function renderWidget(array $fields, $customFielsGroup, $documentType = 'html', array $params = []) public function renderWidget(Environment $env, array $fields, $customFielsGroup, $documentType = 'html', array $params = [])
{ {
$resolvedParams = array_merge($this->defaultParams, $params); $resolvedParams = array_merge($this->defaultParams, $params);
return $this->container->get('templating') return $env->render($resolvedParams['layout'], [
->render($resolvedParams['layout'], [
'cFGroup' => $customFielsGroup, 'cFGroup' => $customFielsGroup,
'cFData' => $fields, 'cFData' => $fields,
'show_empty' => $resolvedParams['show_empty'], ]); 'show_empty' => $resolvedParams['show_empty'], ]);
} }
/**
* (non-PHPdoc).
*
* @see \Symfony\Component\DependencyInjection\ContainerAwareInterface::setContainer()
*/
public function setContainer(?ContainerInterface $container = null)
{
$this->container = $container;
}
} }

View File

@ -102,15 +102,11 @@ services:
class: Chill\CustomFieldsBundle\Templating\Twig\CustomFieldRenderingTwig class: Chill\CustomFieldsBundle\Templating\Twig\CustomFieldRenderingTwig
arguments: arguments:
- "@chill.custom_field.helper" - "@chill.custom_field.helper"
calls:
- [setContainer, ["@service_container"]]
tags: tags:
- { name: twig.extension } - { name: twig.extension }
chill.custom_field.twig.custom_fields_group_rendering: chill.custom_field.twig.custom_fields_group_rendering:
class: Chill\CustomFieldsBundle\Templating\Twig\CustomFieldsGroupRenderingTwig class: Chill\CustomFieldsBundle\Templating\Twig\CustomFieldsGroupRenderingTwig
calls:
- [setContainer, ["@service_container"]]
arguments: arguments:
- "%chill_custom_fields.show_empty_values%" - "%chill_custom_fields.show_empty_values%"
tags: tags: