-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
FieldTrait.php
461 lines (353 loc) · 12.4 KB
/
FieldTrait.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
<?php
namespace EasyCorp\Bundle\EasyAdminBundle\Field;
use EasyCorp\Bundle\EasyAdminBundle\Config\Asset;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Config\KeyValueStore;
use EasyCorp\Bundle\EasyAdminBundle\Config\Option\TextAlign;
use EasyCorp\Bundle\EasyAdminBundle\Dto\AssetDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
use Symfony\Component\ExpressionLanguage\Expression;
use Symfony\Contracts\Translation\TranslatableInterface;
/**
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
*/
trait FieldTrait
{
private FieldDto $dto;
private function __construct()
{
$this->dto = new FieldDto();
}
public function __clone(): void
{
$this->dto = clone $this->dto;
}
public function setFieldFqcn(string $fieldFqcn): self
{
$this->dto->setFieldFqcn($fieldFqcn);
return $this;
}
public function setProperty(string $propertyName): self
{
$this->dto->setProperty($propertyName);
return $this;
}
/**
* @param TranslatableInterface|string|false|null $label
*/
public function setLabel($label): self
{
$this->dto->setLabel($label);
return $this;
}
public function setValue($value): self
{
$this->dto->setValue($value);
return $this;
}
public function setFormattedValue($value): self
{
$this->dto->setFormattedValue($value);
return $this;
}
public function formatValue(?callable $callable): self
{
$this->dto->setFormatValueCallable($callable);
return $this;
}
public function setVirtual(bool $isVirtual): self
{
$this->dto->setVirtual($isVirtual);
return $this;
}
public function setDisabled(bool $disabled = true): self
{
$this->dto->setFormTypeOption('disabled', $disabled);
return $this;
}
public function setRequired(bool $isRequired): self
{
$this->dto->setFormTypeOption('required', $isRequired);
return $this;
}
public function setEmptyData($emptyData = null): self
{
$this->dto->setFormTypeOption('empty_data', $emptyData);
return $this;
}
public function setFormType(string $formTypeFqcn): self
{
$this->dto->setFormType($formTypeFqcn);
return $this;
}
public function setFormTypeOptions(array $options): self
{
$this->dto->setFormTypeOptions($options);
return $this;
}
/**
* @param string $optionName You can use "dot" notation to set nested options (e.g. 'attr.class')
*/
public function setFormTypeOption(string $optionName, $optionValue): self
{
$this->dto->setFormTypeOption($optionName, $optionValue);
return $this;
}
/**
* @param string $optionName You can use "dot" notation to set nested options (e.g. 'attr.class')
*/
public function setFormTypeOptionIfNotSet(string $optionName, $optionValue): self
{
$this->dto->setFormTypeOptionIfNotSet($optionName, $optionValue);
return $this;
}
/**
* Sets the value of a custom HTML attribute that will be added when rendering the field.
* E.g. setAttribute('data-foo', 'bar') will render a 'data-foo="bar"' attribute in HTML.
* It's a shortcut for the equivalent setFormTypeOption('attr.data-foo', 'bar).
*/
public function setHtmlAttribute(string $attributeName, $attributeValue): self
{
if (str_contains($attributeName, '.')) {
throw new \InvalidArgumentException(sprintf('Field attributes must be defined as pairs of "(string) key => (scalar) value". That\'s why the attribute name ("%s") cannot use the "dot notation" with the "." character to create nested attributes.', $attributeName));
}
if (!\is_scalar($attributeValue)) {
throw new \InvalidArgumentException(sprintf('The value of the "%s" attribute must be a scalar value (string, integer, float, boolean); "%s" given.', $attributeName, \gettype($attributeValue)));
}
$this->dto->setFormTypeOption('attr.'.$attributeName, $attributeValue);
$this->dto->setHtmlAttribute($attributeName, $attributeValue);
return $this;
}
public function setHtmlAttributes(array $attributes): self
{
foreach ($attributes as $attributeName => $attributeValue) {
$this->setHtmlAttribute($attributeName, $attributeValue);
}
return $this;
}
public function setSortable(bool $isSortable): self
{
$this->dto->setSortable($isSortable);
return $this;
}
public function setPermission(string|Expression $permission): self
{
$this->dto->setPermission($permission);
return $this;
}
/**
* @param string $textAlign It can be 'left', 'center' or 'right'
*/
public function setTextAlign(string $textAlign): self
{
$validOptions = [TextAlign::LEFT, TextAlign::CENTER, TextAlign::RIGHT];
if (!\in_array($textAlign, $validOptions, true)) {
throw new \InvalidArgumentException(sprintf('The value of the "textAlign" option can only be one of these: "%s" ("%s" was given).', implode(',', $validOptions), $textAlign));
}
$this->dto->setTextAlign($textAlign);
return $this;
}
public function setHelp(TranslatableInterface|string $help): self
{
$this->dto->setHelp($help);
return $this;
}
public function addCssClass(string $cssClass): self
{
$this->dto->setCssClass($this->dto->getCssClass().' '.$cssClass);
return $this;
}
public function setCssClass(string $cssClass): self
{
$this->dto->setCssClass($cssClass);
return $this;
}
public function setTranslationParameters(array $parameters): self
{
$this->dto->setTranslationParameters($parameters);
return $this;
}
public function setTemplateName(string $name): self
{
$this->dto->setTemplateName($name);
$this->dto->setTemplatePath(null);
return $this;
}
public function setTemplatePath(string $path): self
{
$this->dto->setTemplateName(null);
$this->dto->setTemplatePath($path);
return $this;
}
public function addFormTheme(string ...$formThemePaths): self
{
foreach ($formThemePaths as $formThemePath) {
$this->dto->addFormTheme($formThemePath);
}
return $this;
}
public function addWebpackEncoreEntries(Asset|string ...$entryNamesOrAssets): self
{
if (!class_exists('Symfony\\WebpackEncoreBundle\\Twig\\EntryFilesTwigExtension')) {
throw new \RuntimeException('You are trying to add Webpack Encore entries in a field but Webpack Encore is not installed in your project. Try running "composer require symfony/webpack-encore-bundle"');
}
foreach ($entryNamesOrAssets as $entryNameOrAsset) {
if (\is_string($entryNameOrAsset)) {
$this->dto->addWebpackEncoreAsset(new AssetDto($entryNameOrAsset));
} else {
$this->dto->addWebpackEncoreAsset($entryNameOrAsset->getAsDto());
}
}
return $this;
}
public function addAssetMapperEntries(Asset|string ...$entryNamesOrAssets): self
{
if (!class_exists('Symfony\\Component\\AssetMapper\\AssetMapper')) {
throw new \RuntimeException('You are trying to add AssetMapper entries in a field but AssetMapper is not installed in your project. Try running "composer require symfony/asset-mapper"');
}
foreach ($entryNamesOrAssets as $entryNameOrAsset) {
if (\is_string($entryNameOrAsset)) {
$this->dto->addAssetMapperEncoreAsset(new AssetDto($entryNameOrAsset));
} else {
$this->dto->addAssetMapperEncoreAsset($entryNameOrAsset->getAsDto());
}
}
return $this;
}
public function addCssFiles(Asset|string ...$pathsOrAssets): self
{
foreach ($pathsOrAssets as $pathOrAsset) {
if (\is_string($pathOrAsset)) {
$this->dto->addCssAsset(new AssetDto($pathOrAsset));
} else {
$this->dto->addCssAsset($pathOrAsset->getAsDto());
}
}
return $this;
}
public function addJsFiles(Asset|string ...$pathsOrAssets): self
{
foreach ($pathsOrAssets as $pathOrAsset) {
if (\is_string($pathOrAsset)) {
$this->dto->addJsAsset(new AssetDto($pathOrAsset));
} else {
$this->dto->addJsAsset($pathOrAsset->getAsDto());
}
}
return $this;
}
public function addHtmlContentsToHead(string ...$contents): self
{
foreach ($contents as $content) {
$this->dto->addHtmlContentToHead($content);
}
return $this;
}
public function addHtmlContentsToBody(string ...$contents): self
{
foreach ($contents as $content) {
$this->dto->addHtmlContentToBody($content);
}
return $this;
}
public function setCustomOption(string $optionName, $optionValue): self
{
$this->dto->setCustomOption($optionName, $optionValue);
return $this;
}
public function setCustomOptions(array $options): self
{
$this->dto->setCustomOptions($options);
return $this;
}
public function hideOnDetail(): self
{
$displayedOn = $this->dto->getDisplayedOn();
$displayedOn->delete(Crud::PAGE_DETAIL);
$this->dto->setDisplayedOn($displayedOn);
return $this;
}
public function hideOnForm(): self
{
$displayedOn = $this->dto->getDisplayedOn();
$displayedOn->delete(Crud::PAGE_NEW);
$displayedOn->delete(Crud::PAGE_EDIT);
$this->dto->setDisplayedOn($displayedOn);
return $this;
}
public function hideWhenCreating(): self
{
$displayedOn = $this->dto->getDisplayedOn();
$displayedOn->delete(Crud::PAGE_NEW);
$this->dto->setDisplayedOn($displayedOn);
return $this;
}
public function hideWhenUpdating(): self
{
$displayedOn = $this->dto->getDisplayedOn();
$displayedOn->delete(Crud::PAGE_EDIT);
$this->dto->setDisplayedOn($displayedOn);
return $this;
}
public function hideOnIndex(): self
{
$displayedOn = $this->dto->getDisplayedOn();
$displayedOn->delete(Crud::PAGE_INDEX);
$this->dto->setDisplayedOn($displayedOn);
return $this;
}
public function onlyOnDetail(): self
{
$this->dto->setDisplayedOn(KeyValueStore::new([Crud::PAGE_DETAIL => Crud::PAGE_DETAIL]));
return $this;
}
public function onlyOnForms(): self
{
$this->dto->setDisplayedOn(KeyValueStore::new([
Crud::PAGE_NEW => Crud::PAGE_NEW,
Crud::PAGE_EDIT => Crud::PAGE_EDIT,
]));
return $this;
}
public function onlyOnIndex(): self
{
$this->dto->setDisplayedOn(KeyValueStore::new([Crud::PAGE_INDEX => Crud::PAGE_INDEX]));
return $this;
}
public function onlyWhenCreating(): self
{
$this->dto->setDisplayedOn(KeyValueStore::new([Crud::PAGE_NEW => Crud::PAGE_NEW]));
return $this;
}
public function onlyWhenUpdating(): self
{
$this->dto->setDisplayedOn(KeyValueStore::new([Crud::PAGE_EDIT => Crud::PAGE_EDIT]));
return $this;
}
/**
* @param int|string $cols An integer with the number of columns that this field takes (e.g. 6),
* or a string with responsive col CSS classes (e.g. 'col-6 col-sm-4 col-lg-3')
*/
public function setColumns(int|string $cols): self
{
$this->dto->setColumns(\is_int($cols) ? 'col-md-'.$cols : $cols);
return $this;
}
/**
* Used to define the columns of fields when users don't define the
* columns explicitly using the setColumns() method.
* This should only be used if you create a custom EasyAdmin field,
* not when configuring fields in your backend.
*
* @internal
*/
public function setDefaultColumns(int|string $cols): self
{
$this->dto->setDefaultColumns(\is_int($cols) ? 'col-md-'.$cols : $cols);
return $this;
}
public function getAsDto(): FieldDto
{
return $this->dto;
}
}