src/Common/Form/CollectionType.php line 13

Open in your IDE?
  1. <?php
  2. namespace Common\Form;
  3. use Symfony\Component\Form\AbstractType;
  4. use Symfony\Component\Form\Extension\Core\Type\CollectionType as SymfonyCollectionType;
  5. use Symfony\Component\Form\Extension\Core\Type\TextType;
  6. use Symfony\Component\Form\FormInterface;
  7. use Symfony\Component\Form\FormView;
  8. use Symfony\Component\OptionsResolver\Options;
  9. use Symfony\Component\OptionsResolver\OptionsResolver;
  10. class CollectionType extends AbstractType
  11. {
  12.     public function buildView(FormView $viewFormInterface $form, array $options): void
  13.     {
  14.         $view->vars array_replace(
  15.             $view->vars,
  16.             [
  17.                 'allow_add' => $options['allow_add'],
  18.                 'allow_delete' => $options['allow_delete'],
  19.                 'allow_sequence' => $options['allow_sequence'],
  20.                 'add_button_options' => $options['add_button_options'],
  21.                 'delete_button_options' => $options['delete_button_options'],
  22.                 'prototype_name' => $options['prototype_name'],
  23.             ]
  24.         );
  25.         if ($form->getConfig()->hasAttribute('prototype')) {
  26.             $view->vars['prototype'] = $form->getConfig()->getAttribute('prototype')->createView($view);
  27.         }
  28.     }
  29.     public function configureOptions(OptionsResolver $resolver): void
  30.     {
  31.         $resolver->setDefaults(
  32.             [
  33.                 'allow_add' => false,
  34.                 'allow_delete' => false,
  35.                 'allow_sequence' => false,
  36.                 'prototype' => true,
  37.                 'prototype_name' => '__name__',
  38.                 'add_button_options' => [
  39.                     'label' => 'lbl.Add',
  40.                 ],
  41.                 'delete_button_options' => [
  42.                     'label' => 'lbl.Delete',
  43.                 ],
  44.                 'entry_options' => [],
  45.                 'entry_type' => TextType::class,
  46.             ]
  47.         );
  48.         $resolver->setNormalizer(
  49.             'entry_options',
  50.             function (Options $options$value) {
  51.                 $value['block_name'] = 'entry';
  52.                 return $value;
  53.             }
  54.         );
  55.     }
  56.     public function getParent(): string
  57.     {
  58.         return SymfonyCollectionType::class;
  59.     }
  60.     public function getBlockPrefix(): string
  61.     {
  62.         return 'bootstrap_collection';
  63.     }
  64. }