Skip to content

Commit

Permalink
Fix Scope widening and missing request (#306)
Browse files Browse the repository at this point in the history
* Fix Scope widening and missing request

* Add UPGRADE note
  • Loading branch information
jordisala1991 authored Mar 17, 2018
1 parent 9eb745c commit 6e128d3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
13 changes: 13 additions & 0 deletions DependencyInjection/A2lixTranslationFormExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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),
]);
}
}
}
27 changes: 22 additions & 5 deletions Form/Type/TranslatedEntityType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
*/
Expand All @@ -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'];
},
]);
}
Expand Down Expand Up @@ -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');
}
}
9 changes: 9 additions & 0 deletions UPGRADE-2.x.md
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 6e128d3

Please sign in to comment.