-
-
Notifications
You must be signed in to change notification settings - Fork 495
/
Copy pathMediaBlockService.php
232 lines (195 loc) · 7.57 KB
/
MediaBlockService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
declare(strict_types=1);
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\MediaBundle\Block;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Form\FormMapper as AdminFormMapper;
use Sonata\AdminBundle\Form\Type\ModelListType;
use Sonata\BlockBundle\Block\BlockContextInterface;
use Sonata\BlockBundle\Block\Service\AbstractBlockService;
use Sonata\BlockBundle\Block\Service\EditableBlockService;
use Sonata\BlockBundle\Form\Mapper\FormMapper;
use Sonata\BlockBundle\Meta\Metadata;
use Sonata\BlockBundle\Meta\MetadataInterface;
use Sonata\BlockBundle\Model\BlockInterface;
use Sonata\Form\Type\ImmutableArrayType;
use Sonata\Form\Validator\ErrorElement;
use Sonata\MediaBundle\Model\MediaInterface;
use Sonata\MediaBundle\Model\MediaManagerInterface;
use Sonata\MediaBundle\Provider\Pool;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Twig\Environment;
/**
* @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
*/
final class MediaBlockService extends AbstractBlockService implements EditableBlockService
{
/**
* @param AdminInterface<MediaInterface>|null $mediaAdmin
*/
public function __construct(
Environment $twig,
private Pool $pool,
private ?AdminInterface $mediaAdmin,
private MediaManagerInterface $mediaManager,
) {
parent::__construct($twig);
}
public function configureSettings(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'media' => false,
'title' => null,
'translation_domain' => null,
'icon' => null,
'class' => null,
'context' => false,
'mediaId' => null,
'format' => false,
'template' => '@SonataMedia/Block/block_media.html.twig',
]);
}
public function configureEditForm(FormMapper $form, BlockInterface $block): void
{
if (!$form instanceof AdminFormMapper) {
throw new \InvalidArgumentException('Media block requires to be used in the Admin context');
}
if (!$block->getSetting('mediaId') instanceof MediaInterface) {
$this->load($block);
}
$formatChoices = $this->getFormatChoices($block->getSetting('mediaId'));
$form->add('settings', ImmutableArrayType::class, [
'keys' => [
['title', TextType::class, [
'label' => 'form.label_title',
'required' => false,
]],
['translation_domain', TextType::class, [
'label' => 'form.label_translation_domain',
'required' => false,
]],
['icon', TextType::class, [
'label' => 'form.label_icon',
'required' => false,
]],
['class', TextType::class, [
'label' => 'form.label_class',
'required' => false,
]],
[$this->getMediaBuilder($form), null, []],
['format', ChoiceType::class, [
'required' => \count($formatChoices) > 0,
'choices' => $formatChoices,
'label' => 'form.label_format',
]],
],
'translation_domain' => 'SonataMediaBundle',
]);
}
public function execute(BlockContextInterface $blockContext, ?Response $response = null): Response
{
// make sure we have a valid format
$media = $blockContext->getBlock()->getSetting('mediaId');
if ($media instanceof MediaInterface) {
$choices = $this->getFormatChoices($media);
if (!\array_key_exists($blockContext->getSetting('format'), $choices)) {
$blockContext->setSetting('format', key($choices));
}
}
$template = $blockContext->getTemplate();
return $this->renderResponse($template, [
'media' => $blockContext->getSetting('mediaId'),
'block' => $blockContext->getBlock(),
'settings' => $blockContext->getSettings(),
], $response);
}
public function load(BlockInterface $block): void
{
$mediaId = $block->getSetting('mediaId');
if (null === $mediaId || $mediaId instanceof MediaInterface) {
return;
}
$media = $this->mediaManager->findOneBy(['id' => $mediaId]);
if (null === $media) {
return;
}
$block->setSetting('mediaId', $media);
}
public function prePersist(BlockInterface $block): void
{
$block->setSetting('mediaId', $block->getSetting('mediaId') instanceof MediaInterface ? $block->getSetting('mediaId')->getId() : null);
}
public function preUpdate(BlockInterface $block): void
{
$block->setSetting('mediaId', $block->getSetting('mediaId') instanceof MediaInterface ? $block->getSetting('mediaId')->getId() : null);
}
public function getMetadata(): MetadataInterface
{
return new Metadata('sonata.media.block.media', null, null, 'SonataMediaBundle', [
'class' => 'fa fa-picture-o',
]);
}
public function configureCreateForm(FormMapper $form, BlockInterface $block): void
{
$this->configureEditForm($form, $block);
}
public function validate(ErrorElement $errorElement, BlockInterface $block): void
{
}
/**
* @return array<string, string>
*/
private function getFormatChoices(?MediaInterface $media = null): array
{
if (!$media instanceof MediaInterface) {
return [];
}
$context = $media->getContext();
if (null === $context || !$this->pool->hasContext($context)) {
return [];
}
$formatChoices = [];
$formats = $this->pool->getFormatNamesByContext($context);
foreach ($formats as $code => $format) {
$formatChoices[$code] = $code;
}
return $formatChoices;
}
/**
* @param AdminFormMapper<object> $form
*/
private function getMediaBuilder(AdminFormMapper $form): FormBuilderInterface
{
if (null === $this->mediaAdmin) {
throw new \LogicException('The SonataAdminBundle is required to render the edit form.');
}
$fieldDescription = $this->mediaAdmin->createFieldDescription('media', [
'translation_domain' => 'SonataMediaBundle',
'edit' => 'list',
]);
$fieldDescription->setAssociationAdmin($this->mediaAdmin);
$formBuilder = $form->getFormBuilder()->create('mediaId', ModelListType::class, [
'sonata_field_description' => $fieldDescription,
'class' => $this->mediaAdmin->getClass(),
'model_manager' => $this->mediaAdmin->getModelManager(),
'label' => 'form.label_media',
]);
$formBuilder->addModelTransformer(new CallbackTransformer(
static fn (?MediaInterface $value): ?MediaInterface => $value,
static fn (?MediaInterface $value) => $value instanceof MediaInterface ? $value->getId() : $value
));
return $formBuilder;
}
}