EasyCorp / EasyAdminBundle

EasyAdmin is a fast, beautiful and modern admin generator for Symfony applications.
MIT License
4.09k stars 1.02k forks source link

TextEditorType : Impossible to access an attribute ("customOptions") on a null variable. #4224

Closed magiceupho closed 2 years ago

magiceupho commented 3 years ago

I tried to use the EasyCorp\Bundle\EasyAdminBundle\Form\Type\TextEditorType in a FormType in order to use this Form in a CollectionField.

Here is my FormType:

class ParagraphType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('content', TextEditorType::class, ['attr' => ['rows' => 10]])
            ->add('photos', PhotosChoiceType::class, ['multiple' => true])
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Paragraph::class,
            'translation_domain' => 'forms'
        ]);
    }
}

I obtain this error: Error

If I write null instead of TextEditorType::class, the simple text field is displayed.

Could you help me please ?

evaldasAi commented 3 years ago

I made a temporal fix, for cruds that has translated entities, I'm setting new template

    public function configureCrud(Crud $crud): Crud
    {
        return $crud
            ->setFormThemes(['admin/themes/ea_form_theme.html.twig'])
            ;
    }

I've copied content from vendor/easycorp/easyadmin-bundle/src/Resources/views/crud/form_theme.html.twig and fixed {% block ea_text_editor_widget %} {% set numOfRows = form.vars.ea_crud_form.ea_field ? form.vars.ea_crud_form.ea_field.customOptions.get('numOfRows') : 4 %}

magiceupho commented 3 years ago

Ok thx this fixes here too. Is it possible to correct this bug plz ?

javiereguiluz commented 3 years ago

@magiceupho we're sorry but EasyAdmin form types are not general-purpose form types. They only work when using them inside an EasyAdmin backend, not in a custom-made Symfony Form. Sorry!

magiceupho commented 3 years ago

@javiereguiluz Thank you for your response, but you were a bit quick in your answer...

I use EasyAdmin for my backend ! I want to use the CollectionField in a EasyAdmin dashboard ! In order to embed a collection of relation entity forms in the main form. This field must/can use a FormType, right ? That's why I've created the FormType for the relation entity and a field is a textarea for which I want the TextEditorField with Trix. Therefore, I've tried to use your TextEditorType in this FormType.

If my way to achieve this is wrong, how can I do this please ?

Here is my code:

Can you help me please ? I would like to understand how to setup this please.

magiceupho commented 3 years ago

Up.

Is it possible to use CollectionField inside another CollectionField ?

Thx.

treztreiz commented 3 years ago

@javiereguiluz Thank you for your response, but you were a bit quick in your answer...

I use EasyAdmin for my backend ! I want to use the CollectionField in a EasyAdmin dashboard ! In order to embed a collection of relation entity forms in the main form. This field must/can use a FormType, right ? That's why I've created the FormType for the relation entity and a field is a textarea for which I want the TextEditorField with Trix. Therefore, I've tried to use your TextEditorType in this FormType.

If my way to achieve this is wrong, how can I do this please ?

Here is my code:

* The relation entity FormType:
class ParagraphType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('content', TextEditorType::class, ['attr' => ['rows' => 10]])
            ->add('photos', PhotosChoiceType::class, ['multiple' => true])
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Paragraph::class,
        ]);
    }
}
* the main Form fields in my CrudController:
public function configureFields(string $pageName): iterable
    {
        return [
            FormField::addPanel(),
            TextField::new('name'),
            BooleanField::new('draft'),
            TextareaField::new('excerpt'),
            AssociationField::new('categories'),
            AssociationField::new('tags'),

            FormField::addPanel(),
            CollectionField::new('paragraphs')
                ->setEntryType(ParagraphType::class)
                ->setFormTypeOptions(['by_reference' => false])
                ->allowAdd()
                ->allowDelete(),

            FormField::addPanel(),
            AssociationField::new('relatedGalleries'),
            DateTimeField::new('created_at'),
        ];
    }

Can you help me please ? I would like to understand how to setup this please.

Hey, did you find a solution :) ? I actually went trough the same process and now I'm wondering how to initialize the trix editor which does not get initialized by itself ( I also assume that I need to initalize it every time a new collection item is added )

treztreiz commented 3 years ago

In order to use the TextEditorType inside a CollectionField, I actually just had to insert the assets needed by the trix editor inside the form template. Here is my implementation :

{% block configured_stylesheets %} {{ parent() }}

{% endblock %}

{% block configured_javascripts %} {{ parent() }}

{% endblock %}


I had to do the same with the "crud/new" template :)
javiereguiluz commented 2 years ago

I now understand the issue. You have a custom form type which uses some EasyAdmin form type that needs some JS/CSS assets to work.

I'm afraid this is a limitation that we can't fix. We can't detect that some built-in EA form field is being used. So, the solution is what you showed: to manually include the needed assets. Although your solution works, hopefully there's a much simpler solution that doesn't require creating any template. Just add the text editor assets as you would do with your own custom assets. See https://symfony.com/bundles/EasyAdminBundle/current/design.html#adding-custom-web-assets

Closing as "fixed" then. Thanks!