Completed
Push — master ( 7c81ae...22b0b3 )
by
unknown
02:49 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
    public function prePersist($gallery): void
49
    {
50
        $parameters = $this->getPersistentParameters();
51
52
        $gallery->setContext($parameters['context']);
53
    }
54
55
    public function postUpdate($gallery)
56
    {
57
        $gallery->reorderGalleryItems();
58
    }
59
60
    public function getPersistentParameters()
61
    {
62
        $parameters = parent::getPersistentParameters();
63
64
        if (!$this->hasRequest()) {
65
            return $parameters;
66
        }
67
68
        return array_merge($parameters, [
69
            'context' => $this->getRequest()->get('context', $this->pool->getDefaultContext()),
70
        ]);
71
    }
72
73
    public function getNewInstance()
74
    {
75
        $gallery = parent::getNewInstance();
76
77
        if ($this->hasRequest()) {
78
            $gallery->setContext($this->getRequest()->get('context'));
79
        }
80
81
        return $gallery;
82
    }
83
84
    protected function configureFormFields(FormMapper $formMapper): void
85
    {
86
        // define group zoning
87
        $formMapper
88
            ->with('Gallery', ['class' => 'col-md-9'])->end()
89
            ->with('Options', ['class' => 'col-md-3'])->end()
90
        ;
91
92
        $context = $this->getPersistentParameter('context');
93
94
        if (!$context) {
95
            $context = $this->pool->getDefaultContext();
96
        }
97
98
        $formats = [];
99
        foreach ((array) $this->pool->getFormatNamesByContext($context) as $name => $options) {
100
            $formats[$name] = $name;
101
        }
102
103
        $contexts = [];
104
        foreach ((array) $this->pool->getContexts() as $contextItem => $format) {
105
            $contexts[$contextItem] = $contextItem;
106
        }
107
108
        $formMapper
109
            ->with('Options')
110
                ->add('context', ChoiceType::class, [
111
                    'choices' => $contexts,
112
                    'choice_translation_domain' => 'SonataMediaBundle',
113
                ])
114
                ->add('enabled', null, ['required' => false])
115
                ->add('name')
116
                ->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...
117
                    ->add('defaultFormat', ChoiceType::class, ['choices' => $formats])
118
                ->ifEnd()
119
            ->end()
120
            ->with('Gallery')
121
                ->add('galleryItems', CollectionType::class, ['by_reference' => false], [
122
                    'edit' => 'inline',
123
                    'inline' => 'table',
124
                    'sortable' => 'position',
125
                    'link_parameters' => ['context' => $context],
126
                    'admin_code' => 'sonata.media.admin.gallery_item',
127
                ])
128
            ->end()
129
        ;
130
    }
131
132
    protected function configureListFields(ListMapper $listMapper): void
133
    {
134
        $listMapper
135
            ->addIdentifier('name')
136
            ->add('enabled', 'boolean', ['editable' => true])
137
            ->add('context', 'trans', ['catalogue' => 'SonataMediaBundle'])
138
            ->add('defaultFormat', 'trans', ['catalogue' => 'SonataMediaBundle'])
139
        ;
140
    }
141
142
    protected function configureDatagridFilters(DatagridMapper $datagridMapper): void
143
    {
144
        $datagridMapper
145
            ->add('name')
146
            ->add('enabled')
147
            ->add('context', null, [
148
                'show_filter' => false,
149
            ])
150
        ;
151
    }
152
}
153