a2lix / TranslationFormBundle

Ease translations with some dedicated Symfony form types
https://a2lix.fr/bundles/translation-form
MIT License
330 stars 138 forks source link

Translation Form including Collection of Collection and EasyAdmin Integration #382

Closed Adraesh closed 1 month ago

Adraesh commented 1 year ago

Hello,

I can't manage to have A2Lix Translation Form working in an EasyAdmin context whenever my translations contents a Collection of Collection.

The translations form is displaying but the style is broken and the "allow_add" and "allow_delete" option are not working. I am using the provided "@A2lixTranslationForm/bootstrap_5_layout.html.twig" as FormTheme.

Attached a screenshot.

On top you will see a test using a native CollectionField from EasyAdmin and using the same custom form type as bellow. It's working fine style is ok and allow_add and delete are working. The code:

CollectionField::new('options')->setFormTypeOptions([
                'entry_type' => PackageTranslationType::class
            ])->onlyOnForms(),

Bellow the integrated custom form type into the A2lix form, style is broken and allow_add and allow_delete are not working. The code:

TranslationField::new('translations', null,
                [
                    'name' => [
                        'field_type' => TextType::class,
                    ],
                    'options' => [
                        'field_type' => CollectionType::class,
                        'entry_type' => PackageTranslationType::class,
                        'entry_options' => [
                            'label' => false,
                        ],
                        'allow_add' => true,
                        'allow_delete' => true,
                        'prototype' => true,
                        'prototype_name' => '__option__',
                    ],
                ],
            )->setFormTypeOptions([
                'label' => false,
            ])->hideOnIndex()

Screenshot 2023-04-04 at 11 15 19

Any help would be really appreciated, I am stuck here :).

EDIT: My PackageTranslationType looks like this:

<?php

namespace App\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class PackageTranslationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('title', TextType::class)
            ->add('content', CollectionType::class, [
                'entry_type' => TextType::class,
                'entry_options' => [
                    'label' => false,
                ],
                'allow_add' => true,
                'allow_delete' => true,
                'prototype' => true,
                'prototype_name' => '__content__',
            ]);
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
        ]);
    }
}
cesar-cardinale commented 1 year ago

Find out that you just need to add ->addCssClass('form-group field-collection')to your TranslationField::new( function and you need to add the JS code to trigger the add event form.