Easyadminbundle: A way to use translatable entities ?

Created on 6 Feb 2016  路  10Comments  路  Source: EasyCorp/EasyAdminBundle

Hi, is there an easy way to use translatable entities ? And where could I find an exemple ?
Thanks

feature

Most helpful comment

I make it work we a minimal few adjustments. I use the bundle suggested by @mkalisz77 in combination with https://github.com/a2lix/I18nDoctrineBundle, and follow all the instructions in http://a2lix.fr/bundles/translation-form/. The changes was:

1- Create a virtual property for each property in the translation class and force to return one specific language (work with the default language too but I needed spanish).
2- Change the configured doctrine filter for getting all languages on edit form:

oneLocale:
    class: A2lix\I18nDoctrineBundle\Doctrine\ORM\Filter\OneLocaleFilter

to

oneLocale:
      class: A2lix\I18nDoctrineBundle\Doctrine\ORM\Filter\ManyLocalesFilter

3- Also I needed to force the session language in my back-end so in my AdminController I have:

protected function initialize(Request $request)
    {
        $this->get('translator')->setLocale('es');
        if ($locale = $request->attributes->get('_locale')) {
            $request->getSession()->set('_locale', 'es');
        } else {
            // if no explicit locale has been set on this request, use one from the session
            $request->setLocale($request->getSession()->get('_locale', 'es'));
        }
        parent::initialize($request);
    }

With that in place just need to do:

easy_admin:
    entities:
        Accesorios:
            class: AppBundle\Entity\Accesorio
            form:
                fields:
                    - { property: 'translations', type: 'a2lix_translations'}

Hope this help you.

All 10 comments

I haven't used these "translatable entities", but after reading a bit about them, I'd say it's "impossible" to manage them in an easy way with EasyAdmin. I'll leave this issue open in case someone with experience in this wants to share his/her opinions. Thanks!

Thank you, I'm trying to use your admin with gedmo translation ORM, passing the $lang in the new object action. It works fine but there a lot to do (handling the differents translation in the template etc...)

Yep, it's VERY hard to do. I never tried and I'm not in a hurry for this, because I know it's gonna be TOUGH. Using translatable entities is already hard natively, so with forms it's cumbersome...

BUT, if you're using one external {ClassName}Translations entity, you might update it automatically based on virtual fields in your forms, which might be a good start and not that hard if the entity has 1 or 2 translatable fields (and if you have 2 or 3 locales max, else it'd be a pain).

Maybe this https://github.com/a2lix/TranslationFormBundle can help. This bundle isn't compatible with gedmo.

I make it work we a minimal few adjustments. I use the bundle suggested by @mkalisz77 in combination with https://github.com/a2lix/I18nDoctrineBundle, and follow all the instructions in http://a2lix.fr/bundles/translation-form/. The changes was:

1- Create a virtual property for each property in the translation class and force to return one specific language (work with the default language too but I needed spanish).
2- Change the configured doctrine filter for getting all languages on edit form:

oneLocale:
    class: A2lix\I18nDoctrineBundle\Doctrine\ORM\Filter\OneLocaleFilter

to

oneLocale:
      class: A2lix\I18nDoctrineBundle\Doctrine\ORM\Filter\ManyLocalesFilter

3- Also I needed to force the session language in my back-end so in my AdminController I have:

protected function initialize(Request $request)
    {
        $this->get('translator')->setLocale('es');
        if ($locale = $request->attributes->get('_locale')) {
            $request->getSession()->set('_locale', 'es');
        } else {
            // if no explicit locale has been set on this request, use one from the session
            $request->setLocale($request->getSession()->get('_locale', 'es'));
        }
        parent::initialize($request);
    }

With that in place just need to do:

easy_admin:
    entities:
        Accesorios:
            class: AppBundle\Entity\Accesorio
            form:
                fields:
                    - { property: 'translations', type: 'a2lix_translations'}

Hope this help you.

if ($locale = $request->attributes->get('_locale')) {

Isn't this code incorrect, shouldn't = be changed for === ?

Mmm, I see your point, I copied exactly as it was in http://symfony.com/doc/current/cookbook/session/locale_sticky_session.html#creating-a-localelistener and since it work I did not pay much attention. Well, I will be pending, but it seem to be a trick, thanks.

Ok, I understand now, you're right then, but actually you don't use the $locale var at all and set the locale to es manually so you could remove the $locale = part, unless you want to use the $locale var.

I'm closing this issue because it seems @abdielcs explained the steps to get this problem solved. Thank you all for the discussion.

Hello,
i want to use translatable entity on EA 3 but on edit form I have and error The value of the "translations" field of the entity with ID = "1" can't be converted into a string, so it cannot be represented by a TextField.

Was this page helpful?
0 / 5 - 0 ratings