I use A2lix for translation and I use easyadmin. I created a "TranslationField" :
<?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)
->setTemplatePath('admin/field/translation_field.html.twig')
->setTemplateName('translation_field')
->setLabel($label)
->setFormType(TranslationsType::class)
->setFormTypeOptions([
'default_locale' => 'fr',
'fields' => $fieldsConfig,
]);
}
In my EntityCrudController.php, I use this TranslationField
public function configureCrud(Crud $crud): Crud
{
return $crud
->setEntityLabelInSingular('Product')
->setEntityLabelInPlural('Products')
->setPageTitle("index", "Admin product")
->setPaginatorPageSize(20)
->addFormTheme('@FOSCKEditor/Form/ckeditor_widget.html.twig');
}
public function configureFields(string $pageName): iterable
{
yield TranslationField::new('translations', ' ', [
'name' => [
'field_type' => TextType::class,
'required' => true,
'label' => 'Name of the product FR',
'locale_options' => [
'fr' => ['label' => 'Name of the product FR'],
'en' => ['label' => 'Name of the product EN'],
'es' => ['label' => 'Name of the product ES'],
'it' => ['label' => 'Name of the product IT'],
'de' => ['label' => 'Name of the product DE'],
],
]
])
->setTemplatePath('admin/field/translation_field.html.twig')
->hideOnIndex();
[...]
}
My twig template "translation_field.html.twig" is to have tabs with languages :
{# @var ea \EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext #}
{# @var field \EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto #}
{# @var entity \EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto #}
{% set render_as_html = field.customOptions.get('renderAsHtml') %}
</dd></div></dl>
<div class="data-row">
<ul class="nav nav-tabs w-100 border-bottom-0" role="tablist">
{% set myArray = field.value|toArray %}
{% for key_langue, valueObject in myArray['collection'] %}
<li class="nav-item" role="presentation">
<a id="{{ key_langue|upper }}-home-tab" data-bs-toggle="pill" data-bs-target="#{{ key_langue|upper }}-home" class="border-bottom nav-link{% if key_langue == app.request.locale %} active{% endif %}" href="#{{ key_langue }}-tab" aria-controls="{{ key_langue }}-tab" role="tab" data-toggle="tab">
{{ key_langue|upper }}
</a>
</li>
{% endfor %}
</ul>
[...]
When I see the detail of my home, it's OK, this is my template "translation_field.html.twig" which is used. Detail: my template is used
When I click on "edit", this is not my template, this is the standard template of easyadmin for the field and I don't have tabs with all the languages. Edit: my template is not used
I've read the symfony doc and try to understand why my template is not used when I edit. It seems that my template should be used on detail and edit with the code ->setTemplatePath('admin/field/translation_field.html.twig')
How to define to use my template for this field when editing my entity in easyadmin ?