1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: gseidel |
5
|
|
|
* Date: 16.01.18 |
6
|
|
|
* Time: 14:54 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Enhavo\Bundle\FormBundle\Form\Extension; |
10
|
|
|
|
11
|
|
|
use Doctrine\Common\Collections\Collection; |
12
|
|
|
use Symfony\Bridge\Doctrine\Form\Type\EntityType; |
13
|
|
|
use Symfony\Component\Form\AbstractTypeExtension; |
14
|
|
|
use Symfony\Component\Form\CallbackTransformer; |
15
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
16
|
|
|
use Symfony\Component\Form\FormEvent; |
17
|
|
|
use Symfony\Component\Form\FormEvents; |
18
|
|
|
use Symfony\Component\Form\FormInterface; |
19
|
|
|
use Symfony\Component\Form\FormView; |
20
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
21
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
22
|
|
|
|
23
|
|
|
class EntityTypeExtension extends AbstractTypeExtension |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
|
|
public function buildForm(FormBuilderInterface $builder, array $options) |
29
|
|
|
{ |
30
|
|
|
if($options['multiple'] && $options['sortable']) { |
31
|
|
|
$propertyAccessor = PropertyAccess::createPropertyAccessor(); |
32
|
|
|
$submittedData = null; |
33
|
|
|
$builder->addViewTransformer(new CallbackTransformer( |
34
|
|
|
function ($originalDescription) { |
35
|
|
|
return $originalDescription; |
36
|
|
|
}, |
37
|
|
|
function ($submittedDescription) use (&$submittedData) { |
38
|
|
|
$submittedData = $submittedDescription; |
39
|
|
|
return $submittedDescription; |
40
|
|
|
} |
41
|
|
|
)); |
42
|
|
|
|
43
|
|
|
$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) use (&$submittedData, $propertyAccessor, $options) { |
44
|
|
|
$data = $event->getForm()->getData(); |
45
|
|
|
if($data instanceof Collection) { |
46
|
|
|
foreach($data as $entry) { |
47
|
|
|
$id = $propertyAccessor->getValue($entry, 'id'); |
48
|
|
|
$position = array_search($id, $submittedData); |
49
|
|
|
$position++; |
50
|
|
|
$propertyAccessor->setValue($entry, $options['sort_property'], $position); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
return $data; |
54
|
|
|
}); |
55
|
|
|
|
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public function buildView(FormView $view, FormInterface $form, array $options) |
63
|
|
|
{ |
64
|
|
|
if($options['multiple'] && $options['sortable']) { |
65
|
|
|
$view->vars['attr']['data-select2-options'] = json_encode([ |
66
|
|
|
'multiple' => $options['multiple'], |
67
|
|
|
'sortable' => $options['sortable'], |
68
|
|
|
'count' => $options['count'] |
69
|
|
|
]); |
70
|
|
|
} |
71
|
|
|
$view->vars['count'] = $options['count']; |
72
|
|
|
$view->vars['multiple'] = $options['multiple']; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function configureOptions(OptionsResolver $resolver) |
76
|
|
|
{ |
77
|
|
|
$resolver->setDefaults([ |
78
|
|
|
'list' => false, |
79
|
|
|
'sortable' => false, |
80
|
|
|
'sort_property' => null, |
81
|
|
|
'count' => true, |
82
|
|
|
]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function getExtendedTypes(): iterable |
86
|
|
|
{ |
87
|
|
|
return [EntityType::class]; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|