Completed
Push — master ( 31a9ea...5d6556 )
by
unknown
03:38 queued 11s
created

src/Admin/GalleryAdmin.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
/**
25
 * @final since sonata-project/media-bundle 3.21.0
26
 */
27
class GalleryAdmin extends AbstractAdmin
28
{
29
    /**
30
     * @var Pool
31
     */
32
    protected $pool;
33
34
    protected $classnameLabel = 'Gallery';
35
36
    /**
37
     * @param string $code
38
     * @param string $class
39
     * @param string $baseControllerName
40
     */
41
    public function __construct($code, $class, $baseControllerName, Pool $pool)
42
    {
43
        parent::__construct($code, $class, $baseControllerName);
44
45
        $this->pool = $pool;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function prePersist($gallery): void
52
    {
53
        $parameters = $this->getPersistentParameters();
54
55
        $gallery->setContext($parameters['context']);
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function postUpdate($gallery)
62
    {
63
        $gallery->reorderGalleryHasMedia();
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function getPersistentParameters()
70
    {
71
        $parameters = parent::getPersistentParameters();
72
73
        if (!$this->hasRequest()) {
74
            return $parameters;
75
        }
76
77
        return array_merge($parameters, [
78
            'context' => $this->getRequest()->get('context', $this->pool->getDefaultContext()),
79
        ]);
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function getNewInstance()
86
    {
87
        $gallery = parent::getNewInstance();
88
89
        if ($this->hasRequest()) {
90
            $gallery->setContext($this->getRequest()->get('context'));
91
        }
92
93
        return $gallery;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    protected function configureFormFields(FormMapper $formMapper): void
100
    {
101
        // define group zoning
102
        $formMapper
103
            ->with('Gallery', ['class' => 'col-md-9'])->end()
104
            ->with('Options', ['class' => 'col-md-3'])->end()
105
        ;
106
107
        $context = $this->getPersistentParameter('context');
108
109
        if (!$context) {
110
            $context = $this->pool->getDefaultContext();
111
        }
112
113
        $formats = [];
114
        foreach ((array) $this->pool->getFormatNamesByContext($context) as $name => $options) {
115
            $formats[$name] = $name;
116
        }
117
118
        $contexts = [];
119
        foreach ((array) $this->pool->getContexts() as $contextItem => $format) {
120
            $contexts[$contextItem] = $contextItem;
121
        }
122
123
        $formMapper
124
            ->with('Options')
125
                ->add('context', ChoiceType::class, [
126
                    'choices' => $contexts,
127
                    'choice_translation_domain' => 'SonataMediaBundle',
128
                ])
129
                ->add('enabled', null, ['required' => false])
130
                ->add('name')
131
                ->ifTrue($formats)
0 ignored issues
show
$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...
132
                    ->add('defaultFormat', ChoiceType::class, ['choices' => $formats])
133
                ->ifEnd()
134
            ->end()
135
            ->with('Gallery')
136
                ->add('galleryItems', CollectionType::class, ['by_reference' => false], [
137
                    'edit' => 'inline',
138
                    'inline' => 'table',
139
                    'sortable' => 'position',
140
                    'link_parameters' => ['context' => $context],
141
                    'admin_code' => 'sonata.media.admin.gallery_item',
142
                ])
143
            ->end()
144
        ;
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150
    protected function configureListFields(ListMapper $listMapper): void
151
    {
152
        $listMapper
153
            ->addIdentifier('name')
154
            ->add('enabled', 'boolean', ['editable' => true])
155
            ->add('context', 'trans', ['catalogue' => 'SonataMediaBundle'])
156
            ->add('defaultFormat', 'trans', ['catalogue' => 'SonataMediaBundle'])
157
        ;
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163
    protected function configureDatagridFilters(DatagridMapper $datagridMapper): void
164
    {
165
        $datagridMapper
166
            ->add('name')
167
            ->add('enabled')
168
            ->add('context', null, [
169
                'show_filter' => false,
170
            ])
171
        ;
172
    }
173
}
174