Passed
Push — master ( 960a24...ce8b6f )
by Gerhard
20:20 queued 10:00
created

EntityTypeExtension::getExtendedTypes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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