Symfony Edit In Place¶
The Symfony Edit In Place feature allow to edit translations directly in the context of a page, without altering the display styles and presentation. It provides an easy to use interface, even in production.
Users are able to change any translated text directly on the web page, and save them to the configured translation configurations.
Limitations and trade-off¶
- Some translated string can’t be translated via this feature, like HTML Input placeholder or title tag for example. The JavaScript part is using ContentTools by Anthony Blackshaw, which use the HTML contenteditable attribute;
- Upon saving, the Symfony translation cache is re-generated to allow the user to see the new content. This can be an issue on read-only deployments.
Configuration¶
# config/config.yaml
translation:
# ..
edit_in_place:
enabled: true
config_name: default # The configuration to use
activator: php_translation.edit_in_place.activator # The activator service id
# ..
# config/routing.yaml
_translation_edit_in_place:
resource: '@TranslationBundle/Resources/config/routing_edit_in_place.yaml'
prefix: /admin
Note
When you include the routing_edit_in_place.yaml
to expose the controller
that saves the modifications you should be aware of the following:
- The routes must be in a protected area of your application
- The routes should be in the production routing file if you want allow real users to use the feature.
Note
Make sure the Bundle assets are installed via bin/console assets:install
Usage¶
To see the editor options on a page, the php_translation.edit_in_place.activator
service needs to allow the Request. By default we provide a simple Activator based on a flag stored in the Symfony Session.
You can activate the editor by calling:
$container->get('php_translation.edit_in_place.activator')->activate();
Then browse your website and you should see the blue Edit button on the top left corner. If you change a translation and hit the Save button, the modifications are saved for the current locale. So if you want to edit a German translation you have to go on the German version of your website.
You can deactivate the editor by calling:
$container->get('php_translation.edit_in_place.activator')->deactivate();
Those calls have to be implemented by yourself.
Building your own Activator¶
You can change the way the editor is activated by building your own Activator service, all you have to do in implement the Translation\Bundle\EditInPlace\ActivatorInterface
interface.
For example if you wish to display the editor based on a specific authorization role you could implement it that way:
<?php
namespace AppBundle;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
use Translation\Bundle\EditInPlace\ActivatorInterface;
class RoleActivator implements ActivatorInterface
{
/**
* @var AuthorizationCheckerInterface
*/
private $authorizationChecker;
public function __construct(AuthorizationCheckerInterface $authorizationChecker)
{
$this->authorizationChecker = $authorizationChecker;
}
/**
* {@inheritdoc}
*/
public function checkRequest(Request $request = null)
{
try {
return $this->authorizationChecker->isGranted(['ROLE_ADMIN']);
} catch (AuthenticationCredentialsNotFoundException $e) {
return false;
}
}
}
# services.yaml
services:
my_activator:
class: AppBundle\RoleActivator
arguments: ["@security.authorization_checker"]
And then use this new activator in the bundle configuration:
# config/config.yaml
translation:
# ..
edit_in_place:
activator: my_activator
# ..
The Editor toolbox for HTML¶
What is allowed inside the edited text is handled by our JavaScript. So if you follow the Best practices and finish your translation keys with .html
when you want to allow HTML, the editor comes with full power:
Please refer to ContentTools documentation for more information.