Completed
Push — master ( 119f8d...4ad486 )
by Paweł
10:35
created

SelectAttributeChoicesCollectionType::buildForm()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 13
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\AttributeBundle\Form\Type\AttributeType\Configuration;
13
14
use Symfony\Component\Form\AbstractType;
15
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
16
use Symfony\Component\Form\FormBuilderInterface;
17
use Symfony\Component\Form\FormEvent;
18
use Symfony\Component\Form\FormEvents;
19
20
/**
21
 * @author Anna Walasek <[email protected]>
22
 */
23
class SelectAttributeChoicesCollectionType extends AbstractType
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function buildForm(FormBuilderInterface $builder, array $options)
29
    {
30
        $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
31
            $data = $event->getData();
32
            $form = $event->getForm();
33
34
            if (null !== $data) {
35
                $fixedArray = [];
36
                foreach ($data as $key => $value) {
37
                    $newKey = strtolower(str_replace(' ', '_', $value));
38
                    $fixedArray[$newKey] = $value;
39
40
                    if ($form->offsetExists($key)) {
41
                        $form->offsetUnset($key);
42
                        $form->offsetSet(null, $newKey);
43
                    }
44
45
                }
46
47
                $event->setData($fixedArray);
48
            }
49
        });
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function getParent()
56
    {
57
        return CollectionType::class;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function getBlockPrefix()
64
    {
65
        return 'sylius_select_attribute_choices_collection';
66
    }
67
}
68