CollectionTypeExtension::getExtendedType()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\DoctrinePHPCRAdminBundle\Form\Extension;
15
16
use Sonata\DoctrinePHPCRAdminBundle\Form\Listener\CollectionOrderListener;
17
use Sonata\Form\Type\CollectionType;
18
use Symfony\Component\Form\AbstractTypeExtension;
19
use Symfony\Component\Form\FormBuilderInterface;
20
use Symfony\Component\Form\FormEvents;
21
22
/**
23
 * Extend the sonata collection type to sort the collection so the reordering
24
 * is automatically persisted in phpcr-odm.
25
 */
26
class CollectionTypeExtension extends AbstractTypeExtension
27
{
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function buildForm(FormBuilderInterface $builder, array $options): void
32
    {
33
        if ('doctrine_phpcr' !== $options['sonata_field_description']->getAdmin()->getManagerType() || !$options['sonata_field_description']->getOption('sortable')) {
34
            return;
35
        }
36
        $listener = new CollectionOrderListener($options['sonata_field_description']->getName());
37
        $builder->addEventListener(FormEvents::SUBMIT, [$listener, 'onSubmit']);
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function getExtendedType()
44
    {
45
        return self::getExtendedTypes()[0];
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public static function getExtendedTypes()
52
    {
53
        return [
54
            CollectionType::class,
55
        ];
56
    }
57
}
58