如何覆盖 symfony 中自定义字段的模板(用于在 easyadmin 中编辑)

问题描述 投票:0回答:1

我使用 A2lix 进行翻译,并使用 easyadmin。我创建了一个“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,
            ]);
    }

在我的EntityCrudController.php中,我使用这个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();
[...]

}

我的树枝模板“translation_field.html.twig”有带有语言的选项卡:

{# @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>
[...]

当我看到我家的详细信息时,没关系,这是我使用的模板“translation_field.html.twig”。 Detail: my template is used

当我点击“编辑”时,这不是我的模板,这是该字段的 Easyadmin 标准模板,我没有包含所有语言的选项卡。 Edit: my template is not used

我已经阅读了 symfony 文档并尝试理解为什么我的模板在编辑时没有被使用。看来我的模板应该用于详细信息并使用代码进行编辑 ->setTemplatePath('admin/field/translation_field.html.twig')

在 easyadmin 中编辑我的实体时,如何定义为此字段使用我的模板?

symfony twig easyadmin a2lix-translation
1个回答
0
投票

我也遇到同样的问题,请问你找到方法了吗?我也一直在找。谢谢!

© www.soinside.com 2019 - 2024. All rights reserved.