PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, May 12, 2022

[FIXED] How to configure a Translatable Entity in EasyAdmin?

 May 12, 2022     easyadmin, symfony, translation     No comments   

Issue

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()
        ];
    }


Answered By - Kez
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing