Skip to content

Commit 72129ad

Browse files
authored
Do only not escape twit filter output when EditInPlace is false (#102)
* Do only not escape twit filter output when EditInPlace is false This will fix #100 * cs * cs * Using is_safe_callback * cs
1 parent f08a7c5 commit 72129ad

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

Resources/config/edit_in_place.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ services:
2626
class: Translation\Bundle\Twig\TranslationExtension
2727
arguments:
2828
- '@php_translator.edit_in_place.xtrans_html_translator'
29+
calls:
30+
- [setActivator, ['@php_translation.edit_in_place.activator']]
31+
- [setRequestStack, ['@request_stack']]
2932
tags:
3033
- { name: 'twig.extension' }
3134

Twig/TranslationExtension.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace Translation\Bundle\Twig;
1313

14+
use Symfony\Component\HttpFoundation\RequestStack;
15+
use Translation\Bundle\EditInPlace\ActivatorInterface;
16+
1417
/**
1518
* Override the `trans` functions `is_safe` option to allow HTML output from the
1619
* translator. This extension is used by for the EditInPlace feature.
@@ -19,17 +22,53 @@
1922
*/
2023
class TranslationExtension extends \Symfony\Bridge\Twig\Extension\TranslationExtension
2124
{
25+
/**
26+
* @var ActivatorInterface
27+
*/
28+
private $activator;
29+
30+
/**
31+
* @var RequestStack
32+
*/
33+
private $requestStack;
34+
2235
/**
2336
* {@inheritdoc}
2437
*/
2538
public function getFilters()
2639
{
2740
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']]),
3043
];
3144
}
3245

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+
3372
/**
3473
* {@inheritdoc}
3574
*/

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"matthiasnoback/symfony-dependency-injection-test": "^1.0",
3636
"guzzlehttp/psr7": "^1.3",
3737
"nyholm/nsa": "^1.0",
38-
"nyholm/symfony-bundle-test": "^1.0.1"
38+
"nyholm/symfony-bundle-test": "^1.0.2"
3939
},
4040
"suggest": {
4141
"php-http/httplug-bundle": "To easier configure your httplug clients."

0 commit comments

Comments
 (0)