symfonytranslationeasyadmin

How to configure a Translatable Entity in EasyAdmin?


I'm using Translatable and EasyAdmin in a Symfony 5 project and I have configured 2 languages.

The issue is I need to be able to edit the different languages of a record in EasyAdmin, I have checked the docs of Translatable, EasyAdmin and Symfony, There is very little information about how to integrate database translations into EasyAdmin.

Therefore, I'm a bit stuck in terms of code, I have tried configuring setTranslationParameters() inside the entity CRUD controller and changing some configuration in the DashboardController however, I don't think this is the right approach.

Any suggestions of how to solve this issue? Thank you for your effort and time.


Solution

  • as of writing, this feature doesn't exist in EasyAdmin, please see the link to the answer to the issue on Github.

    https://github.com/EasyCorp/EasyAdminBundle/issues/4982

    However, a work around is possible with a different package:

    1. remove doctrine-extensions/DoctrineExtensions and then install KnpLabs/DoctrineBehaviors
    2. install a2lix/translation-form-bundle
    3. Create a translation field:
    <?php
    
    declare(strict_types=1);
    
    namespace App\Controller\Admin\Field;
    
    use A2lix\TranslationFormBundle\Form\Type\TranslationsType;
    use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldInterface;
    use EasyCorp\Bundle\EasyAdminBundle\Field\FieldTrait;
    
    final class TranslationField implements FieldInterface
    {
        use FieldTrait;
    
        public static function new(string $propertyName, ?string $label = null, array $fieldsConfig = []): self
        {
            return (new self())
                ->setProperty($propertyName)
                ->setLabel($label)
                ->setFormType(TranslationsType::class)
                ->setFormTypeOptions([
                    'default_locale' => 'cz',
                    'fields' => $fieldsConfig,
                ]);
        }
    }
    
    
    1. Use the TranslationField inside your admin CRUD controller:
        public function configureFields(string $pageName): iterable
        {
            return [
                TextField::new('title', 'title')->hideOnForm(),
                TranslationField::new('translations', 'translations', [
                    'title' => [
                        'field_type' => TextType::class,
                        'required' => true,
                    ]
                    // add more translatable properties into the array
                ])->setRequired(true)
                    ->hideOnIndex()
            ];
        }