|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Field\Field; |
|
4
|
|
|
|
|
5
|
|
|
use LAG\AdminBundle\Field\EntityFieldInterface; |
|
6
|
|
|
use LAG\AdminBundle\Field\Field; |
|
7
|
|
|
use LAG\AdminBundle\Field\TwigFieldInterface; |
|
8
|
|
|
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; |
|
9
|
|
|
use Symfony\Component\OptionsResolver\Options; |
|
10
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
11
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
|
12
|
|
|
|
|
13
|
|
|
class Link extends StringField implements EntityFieldInterface, TwigFieldInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var Object |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $entity; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Render link template filled with configured options. |
|
22
|
|
|
* |
|
23
|
|
|
* @param mixed $value |
|
24
|
|
|
* @return string |
|
25
|
|
|
*/ |
|
26
|
2 |
|
public function render($value) |
|
27
|
|
|
{ |
|
28
|
2 |
|
$text = $this->options->get('text') ?: parent::render($value); |
|
29
|
2 |
|
$parameters = []; |
|
30
|
2 |
|
$accessor = PropertyAccess::createPropertyAccessor(); |
|
31
|
|
|
|
|
32
|
2 |
|
foreach ($this->options->get('parameters') as $parameterName => $fieldName) { |
|
33
|
1 |
|
if (!$fieldName) { |
|
34
|
1 |
|
$fieldName = $parameterName; |
|
35
|
1 |
|
} |
|
36
|
1 |
|
$parameters[$parameterName] = $accessor->getValue($this->entity, $fieldName); |
|
37
|
2 |
|
} |
|
38
|
|
|
|
|
39
|
2 |
|
$render = $this->twig->render($this->options->get('template'), [ |
|
40
|
2 |
|
'text' => $text, |
|
41
|
2 |
|
'route' => $this->options->get('route'), |
|
42
|
2 |
|
'parameters' => $parameters, |
|
43
|
2 |
|
'target' => $this->options->get('target'), |
|
44
|
2 |
|
'url' => $this->options->get('url'), |
|
45
|
2 |
|
'title' => $this->options->get('title'), |
|
46
|
2 |
|
'icon' => $this->options->get('icon'), |
|
47
|
2 |
|
]); |
|
48
|
|
|
|
|
49
|
2 |
|
return $render; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Configure options resolver. |
|
54
|
|
|
* |
|
55
|
|
|
* @param OptionsResolver $resolver |
|
56
|
|
|
* @return void |
|
57
|
|
|
*/ |
|
58
|
2 |
|
public function configureOptions(OptionsResolver $resolver) |
|
59
|
|
|
{ |
|
60
|
|
|
// inherit parent's option |
|
61
|
2 |
|
parent::configureOptions($resolver); |
|
62
|
|
|
|
|
63
|
2 |
|
$resolver->setDefaults([ |
|
64
|
2 |
|
'length' => $this->applicationConfiguration->getParameter('string_length'), |
|
65
|
2 |
|
'replace' => $this->applicationConfiguration->getParameter('string_length_truncate'), |
|
66
|
2 |
|
'template' => $this->applicationConfiguration->getParameter('fields_template_mapping')[Field::TYPE_LINK], |
|
67
|
2 |
|
'title' => '', |
|
68
|
2 |
|
'icon' => '', |
|
69
|
2 |
|
'target' => '_self', |
|
70
|
2 |
|
'route' => '', |
|
71
|
2 |
|
'parameters' => [], |
|
72
|
2 |
|
'url' => '', |
|
73
|
2 |
|
'text' => '', |
|
74
|
2 |
|
]); |
|
75
|
2 |
|
$resolver->setAllowedTypes('route', 'string'); |
|
76
|
2 |
|
$resolver->setAllowedTypes('parameters', 'array'); |
|
77
|
2 |
|
$resolver->setAllowedTypes('length', 'integer'); |
|
78
|
2 |
|
$resolver->setAllowedTypes('url', 'string'); |
|
79
|
2 |
|
$resolver->setAllowedValues('target', [ |
|
80
|
2 |
|
'_self', |
|
81
|
2 |
|
'_blank', |
|
82
|
2 |
|
]); |
|
83
|
2 |
|
$resolver->setNormalizer('route', function(Options $options, $value) { |
|
84
|
|
|
|
|
85
|
|
|
// route or url should be defined |
|
86
|
2 |
|
if (!$value && !$options->offsetGet('url')) { |
|
87
|
1 |
|
throw new InvalidOptionsException('You must set either an url or a route'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
2 |
|
return $value; |
|
91
|
2 |
|
}); |
|
92
|
2 |
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Define field type. |
|
96
|
|
|
* |
|
97
|
|
|
* @return string |
|
98
|
|
|
*/ |
|
99
|
|
|
public function getType() |
|
100
|
|
|
{ |
|
101
|
|
|
return 'link'; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Define entity. It will be use to fill parameters with properties values. |
|
106
|
|
|
* |
|
107
|
|
|
* @param $entity |
|
108
|
|
|
*/ |
|
109
|
1 |
|
public function setEntity($entity) |
|
110
|
|
|
{ |
|
111
|
1 |
|
$this->entity = $entity; |
|
112
|
1 |
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|