Translating Doctrine Entities with Symfony forms

As described here I’m using KnpLabs\DoctrineBehaviors for Doctrine2 entity translations. While using this bundle we have nice interface for translations but usually we also need some UI (forms) for managing such translations from user interface.

We can always write our own dedicated solution but there is already a nice bundle that will do the job for us: A2LiX TranslationFormBundle. For this example I will use Post entity introduced in previous blog entry.

Installation

To install TranslationBundle please follow instructions from the official documentation.

Note: I’m using 2.1.2 version of TranslationFormBundle and only registered new A2lix\TranslationFormBundle\A2lixTranslationFormBundle() in AppKernel.php.

For the purpose of this entry I’m using configuration:

This configuration can be later overridden in specific translation form field.

Creating form

This bundle offers some special form types that can be used for doctrine entities translations. To edit Post entity from previous blog entry we need to create form type as in the example:

This bundle also offers more advanced configuration that can be found in official documentation.

Usage

Controller action looks like we are updating entity without translations – translation logic is handled by TranslationsType form type:

To display translation form I’m also using standard Twig form widgets:

Note: I’m using bootstrap 3.3.7 for this example.

And the final result should be close to: