From 6e128d32b2651332986b41204f0b2269d0b73030 Mon Sep 17 00:00:00 2001 From: Jordi Sala Morales Date: Sat, 17 Mar 2018 12:45:12 +0100 Subject: [PATCH] Fix Scope widening and missing request (#306) * Fix Scope widening and missing request * Add UPGRADE note --- .../A2lixTranslationFormExtension.php | 13 +++++++++ Form/Type/TranslatedEntityType.php | 27 +++++++++++++++---- UPGRADE-2.x.md | 9 +++++++ 3 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 UPGRADE-2.x.md diff --git a/DependencyInjection/A2lixTranslationFormExtension.php b/DependencyInjection/A2lixTranslationFormExtension.php index 3bbd7d9..9775e9e 100644 --- a/DependencyInjection/A2lixTranslationFormExtension.php +++ b/DependencyInjection/A2lixTranslationFormExtension.php @@ -14,7 +14,9 @@ use Symfony\Component\Config\Definition\Processor; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\Loader; +use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\HttpKernel\DependencyInjection\Extension; /** @@ -44,5 +46,16 @@ public function load(array $configs, ContainerBuilder $container) $container->setParameter('a2lix_translation_form.templating', $config['templating']); $container->setAlias('a2lix_translation_form.manager_registry', $config['manager_registry']); + + $translatedEntityType = $container->getDefinition('a2lix_translation_form.default.type.translatedEntity'); + + // BC for SF 2.3 + if (class_exists('Symfony\Component\HttpFoundation\RequestStack')) { + $translatedEntityType->addMethodCall('setRequestStack', [new Reference('request_stack')]); + } else { + $translatedEntityType->addMethodCall('setRequest', [ + new Reference('request', ContainerInterface::NULL_ON_INVALID_REFERENCE, false), + ]); + } } } diff --git a/Form/Type/TranslatedEntityType.php b/Form/Type/TranslatedEntityType.php index 9015abc..50637a5 100644 --- a/Form/Type/TranslatedEntityType.php +++ b/Form/Type/TranslatedEntityType.php @@ -14,6 +14,7 @@ use Doctrine\ORM\EntityRepository; use Symfony\Component\Form\AbstractType; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\OptionsResolver\Options; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\OptionsResolver\OptionsResolverInterface; @@ -26,12 +27,19 @@ class TranslatedEntityType extends AbstractType { private $request; + private $requestStack; + // BC for SF 2.3 public function setRequest(Request $request = null) { $this->request = $request; } + public function setRequestStack(RequestStack $requestStack) + { + $this->requestStack = $requestStack; + } + /** * @param OptionsResolver $resolver */ @@ -52,11 +60,7 @@ public function configureOptions(OptionsResolver $resolver) ->join('e.translations', 't'); }, $optionProperty => function (Options $options) { - if (null === $this->request) { - throw new \Exception('Error while getting request'); - } - - return $options['translation_path'].'['.$this->request->getLocale().'].'.$options['translation_property']; + return $options['translation_path'].'['.$this->getLocale().'].'.$options['translation_property']; }, ]); } @@ -85,4 +89,17 @@ public function getBlockPrefix() { return 'a2lix_translatedEntity'; } + + private function getLocale() + { + if ($this->requestStack) { + return $this->requestStack->getCurrentRequest()->getLocale(); + } + + if ($this->request) { + return $this->request->getLocale(); + } + + throw new \Exception('Error while getting request'); + } } diff --git a/UPGRADE-2.x.md b/UPGRADE-2.x.md new file mode 100644 index 0000000..ee36cf8 --- /dev/null +++ b/UPGRADE-2.x.md @@ -0,0 +1,9 @@ +UPGRADE 2.x +=========== + +## Deprecated `TranslatedEntityType::setRequest` + +If you are extending `TranslatedEntityType`, you should know that +`setRequest` is deprecated and will be removed on next major. Instead +you might use `setRequestStack` unless you are using SymfonyF 2.3 +(In this case we recommend updating to atleast Symfony 2.8)