Skip to content

Do only not escape twit filter output when EditInPlace is false #102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Resources/config/edit_in_place.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ services:
class: Translation\Bundle\Twig\TranslationExtension
arguments:
- '@php_translator.edit_in_place.xtrans_html_translator'
calls:
- [setActivator, ['@php_translation.edit_in_place.activator']]
- [setRequestStack, ['@request_stack']]
tags:
- { name: 'twig.extension' }

43 changes: 41 additions & 2 deletions Twig/TranslationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

namespace Translation\Bundle\Twig;

use Symfony\Component\HttpFoundation\RequestStack;
use Translation\Bundle\EditInPlace\ActivatorInterface;

/**
* Override the `trans` functions `is_safe` option to allow HTML output from the
* translator. This extension is used by for the EditInPlace feature.
Expand All @@ -19,17 +22,53 @@
*/
class TranslationExtension extends \Symfony\Bridge\Twig\Extension\TranslationExtension
{
/**
* @var ActivatorInterface
*/
private $activator;

/**
* @var RequestStack
*/
private $requestStack;

/**
* {@inheritdoc}
*/
public function getFilters()
{
return [
new \Twig_SimpleFilter('trans', [$this, 'trans'], ['is_safe' => ['html']]),
new \Twig_SimpleFilter('transchoice', [$this, 'transchoice'], ['is_safe' => ['html']]),
new \Twig_SimpleFilter('trans', [$this, 'trans'], ['is_safe_callback' => [$this, 'isSafe']]),
new \Twig_SimpleFilter('transchoice', [$this, 'transchoice'], ['is_safe_callback' => [$this, 'isSafe']]),
];
}

/**
* Escape output if the EditInPlace is disabled.
*
* @return array
*/
public function isSafe($node)
{
return $this->activator->checkRequest($this->requestStack->getMasterRequest()) ? [] : ['html'];
}

/**
* @param ActivatorInterface $activator
*/
public function setActivator(ActivatorInterface $activator)
{
$this->activator = $activator;
}

/**
* @param RequestStack $requestStack
*/
public function setRequestStack(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}

/**
* {@inheritdoc}
*/
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"matthiasnoback/symfony-dependency-injection-test": "^1.0",
"guzzlehttp/psr7": "^1.3",
"nyholm/nsa": "^1.0",
"nyholm/symfony-bundle-test": "^1.0.1"
"nyholm/symfony-bundle-test": "^1.0.2"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will fix:

Symfony\Component\DependencyInjection\Exception\LogicException: Annotations cannot be enabled as the Doctrine Cache library is not installed.

},
"suggest": {
"php-http/httplug-bundle": "To easier configure your httplug clients."
Expand Down