|
11 | 11 |
|
12 | 12 | namespace Translation\Bundle\Twig; |
13 | 13 |
|
| 14 | +use Symfony\Component\HttpFoundation\RequestStack; |
| 15 | +use Translation\Bundle\EditInPlace\ActivatorInterface; |
| 16 | + |
14 | 17 | /** |
15 | 18 | * Override the `trans` functions `is_safe` option to allow HTML output from the |
16 | 19 | * translator. This extension is used by for the EditInPlace feature. |
|
19 | 22 | */ |
20 | 23 | class TranslationExtension extends \Symfony\Bridge\Twig\Extension\TranslationExtension |
21 | 24 | { |
| 25 | + /** |
| 26 | + * @var ActivatorInterface |
| 27 | + */ |
| 28 | + private $activator; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var RequestStack |
| 32 | + */ |
| 33 | + private $requestStack; |
| 34 | + |
22 | 35 | /** |
23 | 36 | * {@inheritdoc} |
24 | 37 | */ |
25 | 38 | public function getFilters() |
26 | 39 | { |
27 | 40 | return [ |
28 | | - new \Twig_SimpleFilter('trans', [$this, 'trans'], ['is_safe' => ['html']]), |
29 | | - new \Twig_SimpleFilter('transchoice', [$this, 'transchoice'], ['is_safe' => ['html']]), |
| 41 | + new \Twig_SimpleFilter('trans', [$this, 'trans'], ['is_safe_callback' => [$this, 'isSafe']]), |
| 42 | + new \Twig_SimpleFilter('transchoice', [$this, 'transchoice'], ['is_safe_callback' => [$this, 'isSafe']]), |
30 | 43 | ]; |
31 | 44 | } |
32 | 45 |
|
| 46 | + /** |
| 47 | + * Escape output if the EditInPlace is disabled. |
| 48 | + * |
| 49 | + * @return array |
| 50 | + */ |
| 51 | + public function isSafe($node) |
| 52 | + { |
| 53 | + return $this->activator->checkRequest($this->requestStack->getMasterRequest()) ? [] : ['html']; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @param ActivatorInterface $activator |
| 58 | + */ |
| 59 | + public function setActivator(ActivatorInterface $activator) |
| 60 | + { |
| 61 | + $this->activator = $activator; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @param RequestStack $requestStack |
| 66 | + */ |
| 67 | + public function setRequestStack(RequestStack $requestStack) |
| 68 | + { |
| 69 | + $this->requestStack = $requestStack; |
| 70 | + } |
| 71 | + |
33 | 72 | /** |
34 | 73 | * {@inheritdoc} |
35 | 74 | */ |
|
0 commit comments