Closed magiceupho closed 2 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 %}
Ok thx this fixes here too. Is it possible to correct this bug plz ?
@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!
@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.
Up.
Is it possible to use CollectionField inside another CollectionField ?
Thx.
@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 )
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 :
The CrudController :
class FaqCrudController extends AbstractCrudController
{
...
public function configureCrud(Crud $crud): Crud
{
return $crud->overrideTemplate('crud/edit', 'admin/faq_edit.html.twig'); // Overriding the template in order to insert the assets
}
public function configureFields(string $pageName): iterable
{
...
yield CollectionField::new('items')
->setLabel('Questions')
->setEntryIsComplex(true)
->setEntryType(FaqItemType::class);
}
}
The FormType :
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
...
->add('answer', TextEditorType::class)
;
}
The template :
{% extends '@!EasyAdmin/crud/edit.html.twig' %}
{% block configured_stylesheets %} {{ parent() }}
{% endblock %}
{% block configured_javascripts %} {{ parent() }}
{% endblock %}
I had to do the same with the "crud/new" template :)
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!
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:
I obtain this error:
If I write
null
instead ofTextEditorType::class
, the simple text field is displayed.Could you help me please ?