1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LAG\AdminBundle\Form\Type\Select2; |
6
|
|
|
|
7
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
8
|
|
|
use Symfony\Bridge\Doctrine\Form\Type\EntityType; |
|
|
|
|
9
|
|
|
use Symfony\Component\Form\AbstractType; |
10
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
11
|
|
|
use Symfony\Component\Form\FormEvent; |
12
|
|
|
use Symfony\Component\Form\FormEvents; |
13
|
|
|
use Symfony\Component\Form\FormInterface; |
14
|
|
|
use Symfony\Component\Form\FormView; |
15
|
|
|
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; |
16
|
|
|
use Symfony\Component\OptionsResolver\Options; |
17
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
18
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
19
|
|
|
|
20
|
|
|
class Select2EntityType extends AbstractType |
21
|
|
|
{ |
22
|
|
|
private ManagerRegistry $registry; |
23
|
|
|
|
24
|
|
|
public function __construct(ManagerRegistry $registry) |
25
|
|
|
{ |
26
|
|
|
$this->registry = $registry; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function getParent(): string |
30
|
|
|
{ |
31
|
|
|
return EntityType::class; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function configureOptions(OptionsResolver $resolver) |
35
|
|
|
{ |
36
|
|
|
$resolver |
37
|
|
|
->setDefaults([ |
38
|
|
|
'allow_add' => false, |
39
|
|
|
'create_property_path' => null, |
40
|
|
|
'select2_options' => [], |
41
|
|
|
]) |
42
|
|
|
->setAllowedTypes('allow_add', 'boolean') |
43
|
|
|
->setAllowedTypes('create_property_path', ['string', 'null']) |
44
|
|
|
->setAllowedTypes('select2_options', 'array') |
45
|
|
|
->setNormalizer('create_property_path', function (Options $options, $value) { |
46
|
|
|
if ($options->offsetGet('allow_add') && !$value) { |
47
|
|
|
throw new InvalidOptionsException('The options "create_property_path" should be defined when "allow_add" is set to true'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $value; |
51
|
|
|
}) |
52
|
|
|
->setNormalizer('select2_options', function (Options $options, $value) { |
53
|
|
|
if ($options->offsetGet('allow_add')) { |
54
|
|
|
$value['tags'] = json_encode(true); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $value; |
58
|
|
|
}) |
59
|
|
|
; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function buildForm(FormBuilderInterface $builder, array $options) |
63
|
|
|
{ |
64
|
|
|
$builder |
65
|
|
|
->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($options) { |
66
|
|
|
$manager = $this->registry->getManagerForClass($options['class']); |
67
|
|
|
$repository = $manager->getRepository($options['class']); |
68
|
|
|
$propertyAccessor = PropertyAccess::createPropertyAccessor(); |
69
|
|
|
$data = []; |
70
|
|
|
|
71
|
|
|
if (!is_iterable($event->getData())) { |
72
|
|
|
return; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
foreach ($event->getData() as $identifier) { |
76
|
|
|
$value = $repository->find($identifier); |
77
|
|
|
|
78
|
|
|
if ($value === null) { |
79
|
|
|
$value = new $options['class'](); |
80
|
|
|
$propertyAccessor->setValue($value, $options['create_property_path'], $identifier); |
81
|
|
|
$manager->persist($value); |
82
|
|
|
$manager->flush(); |
83
|
|
|
$identifier = $value->getId(); |
84
|
|
|
} |
85
|
|
|
$data[] = $identifier; |
86
|
|
|
} |
87
|
|
|
$event->setData($data); |
88
|
|
|
}) |
89
|
|
|
; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function buildView(FormView $view, FormInterface $form, array $options) |
93
|
|
|
{ |
94
|
|
|
$view->vars['attr']['data-controller'] = 'select2'; |
95
|
|
|
$view->vars['attr']['data-options'] = json_encode($options['select2_options']); |
96
|
|
|
$view->vars['attr']['data-allow-add'] = json_encode($options['allow_add']); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths