|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Field\Configuration; |
|
4
|
|
|
|
|
5
|
|
|
use LAG\AdminBundle\Field\AbstractField; |
|
6
|
|
|
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; |
|
7
|
|
|
use Symfony\Component\OptionsResolver\Options; |
|
8
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
9
|
|
|
|
|
10
|
|
|
class LinkConfiguration extends StringFieldConfiguration |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @param OptionsResolver $resolver |
|
14
|
|
|
*/ |
|
15
|
|
|
public function configureOptions(OptionsResolver $resolver) |
|
16
|
|
|
{ |
|
17
|
|
|
parent::configureOptions($resolver); |
|
18
|
|
|
|
|
19
|
|
|
$resolver->setDefaults([ |
|
20
|
|
|
'template' => $this |
|
21
|
|
|
->applicationConfiguration |
|
22
|
|
|
->getParameter('fields_template_mapping')[AbstractField::TYPE_LINK], |
|
23
|
|
|
'title' => '', |
|
24
|
|
|
'icon' => '', |
|
25
|
|
|
'target' => '_self', |
|
26
|
|
|
'route' => '', |
|
27
|
|
|
'parameters' => [], |
|
28
|
|
|
'url' => '', |
|
29
|
|
|
'text' => '', |
|
30
|
|
|
'admin' => null, |
|
31
|
|
|
'action' => null, |
|
32
|
|
|
'class' => '', |
|
33
|
|
|
]); |
|
34
|
|
|
$resolver->setAllowedTypes('route', 'string'); |
|
35
|
|
|
$resolver->setAllowedTypes('parameters', 'array'); |
|
36
|
|
|
$resolver->setAllowedTypes('length', 'integer'); |
|
37
|
|
|
$resolver->setAllowedTypes('url', 'string'); |
|
38
|
|
|
$resolver->setAllowedValues('target', [ |
|
39
|
|
|
'_self', |
|
40
|
|
|
'_blank', |
|
41
|
|
|
]); |
|
42
|
|
|
$resolver->setNormalizer('route', function(Options $options, $value) { |
|
43
|
|
|
// route or url should be defined |
|
44
|
|
|
if (!$value && !$options->offsetGet('url') && !$options->offsetGet('admin')) { |
|
45
|
|
|
throw new InvalidOptionsException( |
|
46
|
|
|
'You must set either an url or a route for the property' |
|
47
|
|
|
); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return $value; |
|
51
|
|
|
}); |
|
52
|
|
|
$resolver->setNormalizer('admin', function(Options $options, $value) { |
|
53
|
|
|
// if a Admin is defined, an Action should be defined too |
|
54
|
|
|
if ($value && !$options->offsetGet('action')) { |
|
55
|
|
|
throw new InvalidOptionsException( |
|
56
|
|
|
'An Action should be provided if an Admin is provided' |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $value; |
|
61
|
|
|
}); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|