Skip to content

Commit 2a05eba

Browse files
committed
Merge pull request #122 from symfony-cmf/force_request_locale
make it possible to always force the current request locale to be set on the model
2 parents 58089d0 + baf8fba commit 2a05eba

File tree

5 files changed

+25
-1
lines changed

5 files changed

+25
-1
lines changed

Controller/RestController.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Cmf\Bundle\CreateBundle\Controller;
1313

14+
use Symfony\Cmf\Bundle\CoreBundle\Translatable\TranslatableInterface;
1415
use Symfony\Cmf\Bundle\CreateBundle\Security\AccessCheckerInterface;
1516
use Symfony\Component\HttpFoundation\Request;
1617
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
@@ -54,25 +55,33 @@ class RestController
5455
*/
5556
protected $accessChecker;
5657

58+
/**
59+
* @var boolean
60+
*/
61+
protected $forceRequestLocale;
62+
5763
/**
5864
* @param ViewHandlerInterface $viewHandler
5965
* @param RdfMapperInterface $rdfMapper
6066
* @param RdfTypeFactory $typeFactory
6167
* @param RestService $restHandler
6268
* @param AccessCheckerInterface $accessChecker
69+
* @param boolean $forceRequestLocale
6370
*/
6471
public function __construct(
6572
ViewHandlerInterface $viewHandler,
6673
RdfMapperInterface $rdfMapper,
6774
RdfTypeFactory $typeFactory,
6875
RestService $restHandler,
69-
AccessCheckerInterface $accessChecker
76+
AccessCheckerInterface $accessChecker,
77+
$forceRequestLocale
7078
) {
7179
$this->viewHandler = $viewHandler;
7280
$this->rdfMapper = $rdfMapper;
7381
$this->typeFactory = $typeFactory;
7482
$this->restHandler = $restHandler;
7583
$this->accessChecker = $accessChecker;
84+
$this->forceRequestLocale = $forceRequestLocale;
7685
}
7786

7887
protected function getModelBySubject(Request $request, $subject)
@@ -82,6 +91,10 @@ protected function getModelBySubject(Request $request, $subject)
8291
throw new NotFoundHttpException($subject.' not found');
8392
}
8493

94+
if ($this->forceRequestLocale && $model instanceof TranslatableInterface) {
95+
$model->setLocale($request->getLocale());
96+
}
97+
8598
return $model;
8699
}
87100

DependencyInjection/CmfCreateExtension.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ public function load(array $configs, ContainerBuilder $container)
7676
$container->setParameter($this->getAlias().'.rest.controller.class', $config['rest_controller_class']);
7777
}
7878

79+
if ($config['rest_force_request_locale']) {
80+
$bundles = $container->getParameter('kernel.bundles');
81+
if (!isset($bundles['CmfCoreBundle'])) {
82+
throw new InvalidConfigurationException('You need to enable "CmfCoreBundle" when activating the "rest_force_request_locale" option');
83+
}
84+
}
85+
$container->setParameter($this->getAlias().'.rest.force_request_locale', $config['rest_force_request_locale']);
86+
7987
$this->loadSecurity($config['security'], $loader, $container);
8088

8189
$hasMapper = false;

DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function getConfigTreeBuilder()
3434
->fixXmlConfig('rdf_config_dir', 'rdf_config_dirs')
3535
->children()
3636
->scalarNode('rest_controller_class')->defaultFalse()->end()
37+
->booleanNode('rest_force_request_locale')->defaultFalse()->end()
3738
->arrayNode('map')
3839
->useAttributeAsKey('name')
3940
->prototype('scalar')->end()

Resources/config/services.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
<argument type="service" id="cmf_create.rdf_type_factory"/>
4949
<argument type="service" id="cmf_create.rest.handler"/>
5050
<argument type="service" id="cmf_create.security.checker"/>
51+
<argument>%cmf_create.rest.force_request_locale%</argument>
5152
</service>
5253

5354
<service id="cmf_create.security.role_access_checker" class="%cmf_create.security.role_access_checker.class%">

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"symfony-cmf/testing": "~1.1"
2727
},
2828
"suggest": {
29+
"symfony-cmf/core-bundle": "To be able to enable 'rest_force_request_locale', ~1.0",
2930
"symfony-cmf/media-bundle": "When using the default image support, ~1.1"
3031
},
3132
"autoload": {

0 commit comments

Comments
 (0)