I have a custom CollectionType-Form-Class and I want to write some stuff in the theme for the entries of this type.
The problem is that this is somewhat of a "general FormType" in this project, so others will use this CollectionType, too. I don't know their forms and stuff, so I can't make use of the automatically generated block names like _my_form_my_collection_entry_widget and so on.
I have to create a reusable CollectionType with add- / remove-buttons, so that my colleagues don't always have to manually add those buttons and the necessary JS. I wanted to add the remove-button after every entry. Of course I tried the widget-block of my CollectionType, loop through the children, render the rows of those children and add my remove-button, but unfortunately, with this approach, the prototype-data-attribute got lost.
I've set the "block_prefix" for the CollectionType and I can use block_prefix_widget and block_prefix_row, but what I really need is block_prefix_entry_widget.
I can use collection_entry_widget, this would be the correct block, but it affects all collections, not just my custom one.
My code roughly looks like this:
class ProjectFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->add('project_date_span_collection', ExpandableCollectionType::class, [
'entry_type' => DateSpanType::class,
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
]);
}
}
class ExpandableCollectionType extends AbstractType
{
public const BLOCK_PREFIX = 'expandable_collection';
public function getBlockPrefix(): string
{
return self::BLOCK_PREFIX;
}
public function getParent(): string
{
return CollectionType::class;
}
}
class DateSpanType extends AbstractType
{
public const BLOCK_PREFIX = 'date_span';
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('from', DateType::class, [
'label' => 'From',
'label_attr' => ['class' => 'label-date-span-from'],
'widget' => 'choice',
'format' => 'dd.MM.yyyy',
'html5' => false,
])
->add('to', DateType::class, [
'label' => 'To',
'label_attr' => ['class' => 'label-date-span-to'],
'widget' => 'choice',
'format' => 'dd.MM.yyyy',
'html5' => false,
]);
}
public function getBlockPrefix(): string
{
return self::BLOCK_PREFIX;
}
}
// example controller code:
// .....
$form = $this->createFormBuilder(ProjectFormType::class)
->getForm();
// .....
return $this->render('/form.html.twig', array('form' => $form->createView()));
I know I might have stuff like _project_form_project_date_span_collection_entry_widget or stuff like that but that won't suffice, because this CollectionType will be used in many different forms with different names and also with different entry types. I would need something like expandable_collection_entry_widget to cover all the different occurences.
I tried many different combinations of block names, many didn't work, the working ones weren't what I needed.
I also added var_dump($blockName) in vendor/symfony/twig-bridge/Form/TwigRendererEngine.php, but all I've seen where those automatically generated block names that won't do the trick for my needs.