Completed
Pull Request — master (#1559)
by Grégoire
02:43
created

GalleryAdmin::postUpdate()   A

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 1
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\MediaBundle\Admin;
15
16
use Sonata\AdminBundle\Admin\AbstractAdmin;
17
use Sonata\AdminBundle\Datagrid\DatagridMapper;
18
use Sonata\AdminBundle\Datagrid\ListMapper;
19
use Sonata\AdminBundle\Form\FormMapper;
20
use Sonata\Form\Type\CollectionType;
21
use Sonata\MediaBundle\Provider\Pool;
22
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
23
24
class GalleryAdmin extends AbstractAdmin
25
{
26
    /**
27
     * @var Pool
28
     */
29
    protected $pool;
30
31
    protected $classnameLabel = 'Gallery';
32
33
    /**
34
     * @param string $code
35
     * @param string $class
36
     * @param string $baseControllerName
37
     * @param Pool   $pool
38
     */
39
    public function __construct($code, $class, $baseControllerName, Pool $pool)
40
    {
41
        parent::__construct($code, $class, $baseControllerName);
42
43
        $this->pool = $pool;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function prePersist($gallery): void
50
    {
51
        $parameters = $this->getPersistentParameters();
52
53
        $gallery->setContext($parameters['context']);
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function postUpdate($gallery)
60
    {
61
        $gallery->reorderGalleryHasMedia();
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function getPersistentParameters()
68
    {
69
        $parameters = parent::getPersistentParameters();
70
71
        if (!$this->hasRequest()) {
72
            return $parameters;
73
        }
74
75
        return array_merge($parameters, [
76
            'context' => $this->getRequest()->get('context', $this->pool->getDefaultContext()),
77
        ]);
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function getNewInstance()
84
    {
85
        $gallery = parent::getNewInstance();
86
87
        if ($this->hasRequest()) {
88
            $gallery->setContext($this->getRequest()->get('context'));
89
        }
90
91
        return $gallery;
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    protected function configureFormFields(FormMapper $formMapper): void
98
    {
99
        // define group zoning
100
        $formMapper
101
            ->with('Gallery', ['class' => 'col-md-9'])->end()
102
            ->with('Options', ['class' => 'col-md-3'])->end()
103
        ;
104
105
        $context = $this->getPersistentParameter('context');
106
107
        if (!$context) {
108
            $context = $this->pool->getDefaultContext();
109
        }
110
111
        $formats = [];
112
        foreach ((array) $this->pool->getFormatNamesByContext($context) as $name => $options) {
113
            $formats[$name] = $name;
114
        }
115
116
        $contexts = [];
117
        foreach ((array) $this->pool->getContexts() as $contextItem => $format) {
118
            $contexts[$contextItem] = $contextItem;
119
        }
120
121
        $formMapper
122
            ->with('Options')
123
                ->add('context', ChoiceType::class, ['choices' => $contexts])
124
                ->add('enabled', null, ['required' => false])
125
                ->add('name')
126
                ->ifTrue($formats)
0 ignored issues
show
Documentation introduced by
$formats is of type array, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
127
                    ->add('defaultFormat', ChoiceType::class, ['choices' => $formats])
128
                ->ifEnd()
129
            ->end()
130
            ->with('Gallery')
131
                ->add('galleryItems', CollectionType::class, ['by_reference' => false], [
132
                    'edit' => 'inline',
133
                    'inline' => 'table',
134
                    'sortable' => 'position',
135
                    'link_parameters' => ['context' => $context],
136
                    'admin_code' => 'sonata.media.admin.gallery_item',
137
                ])
138
            ->end()
139
        ;
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145
    protected function configureListFields(ListMapper $listMapper): void
146
    {
147
        $listMapper
148
            ->addIdentifier('name')
149
            ->add('enabled', 'boolean', ['editable' => true])
150
            ->add('context', 'trans', ['catalogue' => 'SonataMediaBundle'])
151
            ->add('defaultFormat', 'trans', ['catalogue' => 'SonataMediaBundle'])
152
        ;
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158
    protected function configureDatagridFilters(DatagridMapper $datagridMapper): void
159
    {
160
        $datagridMapper
161
            ->add('name')
162
            ->add('enabled')
163
            ->add('context', null, [
164
                'show_filter' => false,
165
            ])
166
        ;
167
    }
168
}
169