1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Setono\SyliusStockMovementPlugin\Form\Type; |
6
|
|
|
|
7
|
|
|
use function Safe\array_replace; |
8
|
|
|
use Sylius\Component\Registry\ServiceRegistryInterface; |
9
|
|
|
use Symfony\Component\Form\AbstractType; |
10
|
|
|
use Symfony\Component\Form\Extension\Core\Type\CollectionType; |
11
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
12
|
|
|
use Symfony\Component\Form\FormInterface; |
13
|
|
|
use Symfony\Component\Form\FormView; |
14
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
15
|
|
|
|
16
|
|
|
abstract class AbstractConfigurationCollectionType extends AbstractType |
17
|
|
|
{ |
18
|
|
|
/** @var ServiceRegistryInterface */ |
19
|
|
|
protected $registry; |
20
|
|
|
|
21
|
|
|
public function __construct(ServiceRegistryInterface $registry) |
22
|
|
|
{ |
23
|
|
|
$this->registry = $registry; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function buildForm(FormBuilderInterface $builder, array $options): void |
27
|
|
|
{ |
28
|
|
|
$prototypes = []; |
29
|
|
|
foreach (array_keys($this->registry->all()) as $type) { |
30
|
|
|
$formBuilder = $builder->create( |
31
|
|
|
$options['prototype_name'], |
32
|
|
|
$options['entry_type'], |
33
|
|
|
array_replace( |
34
|
|
|
$options['entry_options'], |
35
|
|
|
['configuration_type' => $type] |
36
|
|
|
) |
37
|
|
|
); |
38
|
|
|
|
39
|
|
|
$prototypes[$type] = $formBuilder->getForm(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$builder->setAttribute('prototypes', $prototypes); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function buildView(FormView $view, FormInterface $form, array $options): void |
46
|
|
|
{ |
47
|
|
|
$view->vars['prototypes'] = []; |
48
|
|
|
|
49
|
|
|
foreach ($form->getConfig()->getAttribute('prototypes') as $type => $prototype) { |
50
|
|
|
/** @var FormInterface $prototype */ |
51
|
|
|
$view->vars['prototypes'][$type] = $prototype->createView($view); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function configureOptions(OptionsResolver $resolver): void |
56
|
|
|
{ |
57
|
|
|
$resolver->setDefaults([ |
58
|
|
|
'allow_add' => true, |
59
|
|
|
'allow_delete' => true, |
60
|
|
|
'by_reference' => false, |
61
|
|
|
'error_bubbling' => false, |
62
|
|
|
]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getParent(): string |
66
|
|
|
{ |
67
|
|
|
return CollectionType::class; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|