|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Field\Field; |
|
4
|
|
|
|
|
5
|
|
|
use LAG\AdminBundle\Field\EntityFieldInterface; |
|
6
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
7
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
|
8
|
|
|
use Twig_Environment; |
|
9
|
|
|
|
|
10
|
|
|
class Link extends StringField implements EntityFieldInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $route; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $parameters; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var Twig_Environment |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $twig; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $template; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var Object |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $entity; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Link target. |
|
39
|
|
|
* |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $target; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* If an url is provided we use it instead of route. |
|
46
|
|
|
* |
|
47
|
|
|
* @var string |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $url; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var string |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $title; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Font awesome icon name |
|
58
|
|
|
* |
|
59
|
|
|
* @var string |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $icon; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Link text |
|
65
|
|
|
* |
|
66
|
|
|
* @var |
|
67
|
|
|
*/ |
|
68
|
|
|
protected $text; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Render link template filled with configured options |
|
72
|
|
|
* |
|
73
|
|
|
* @param mixed $value |
|
74
|
|
|
* @return string |
|
75
|
|
|
*/ |
|
76
|
|
|
public function render($value) |
|
77
|
|
|
{ |
|
78
|
|
|
$text = $this->text ?: parent::render($value); |
|
79
|
|
|
$parameters = []; |
|
80
|
|
|
$accessor = PropertyAccess::createPropertyAccessor(); |
|
81
|
|
|
|
|
82
|
|
|
foreach ($this->parameters as $parameterName => $fieldName) { |
|
83
|
|
|
if (!$fieldName) { |
|
84
|
|
|
$fieldName = $parameterName; |
|
85
|
|
|
} |
|
86
|
|
|
$parameters[$parameterName] = $accessor->getValue($this->entity, $fieldName); |
|
87
|
|
|
} |
|
88
|
|
|
$this->twig->getFunction('path'); |
|
89
|
|
|
$render = $this->twig->render($this->template, [ |
|
90
|
|
|
'text' => $text, |
|
91
|
|
|
'route' => $this->route, |
|
92
|
|
|
'parameters' => $parameters, |
|
93
|
|
|
'target' => $this->target, |
|
94
|
|
|
'url' => $this->url, |
|
95
|
|
|
'title' => $this->title, |
|
96
|
|
|
'icon' => $this->icon, |
|
97
|
|
|
]); |
|
98
|
|
|
|
|
99
|
|
|
return $render; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Configure options resolver. |
|
104
|
|
|
* |
|
105
|
|
|
* @param OptionsResolver $resolver |
|
106
|
|
|
* @return void |
|
107
|
|
|
*/ |
|
108
|
|
|
public function configureOptions(OptionsResolver $resolver) |
|
109
|
|
|
{ |
|
110
|
|
|
$resolver->setDefaults([ |
|
111
|
|
|
'length' => $this->configuration->getStringLength(), |
|
112
|
|
|
'replace' => $this->configuration->getStringLengthTruncate(), |
|
113
|
|
|
'template' => 'LAGAdminBundle:Render:link.html.twig', |
|
114
|
|
|
'title' => '', |
|
115
|
|
|
'icon' => '', |
|
116
|
|
|
'target' => '_self', |
|
117
|
|
|
'route' => '', |
|
118
|
|
|
'parameters' => [], |
|
119
|
|
|
'url' => '', |
|
120
|
|
|
'text' => '', |
|
121
|
|
|
]); |
|
122
|
|
|
$resolver->setAllowedTypes('route', 'string'); |
|
123
|
|
|
$resolver->setAllowedTypes('parameters', 'array'); |
|
124
|
|
|
$resolver->setAllowedTypes('length', 'integer'); |
|
125
|
|
|
$resolver->setAllowedTypes('url', 'string'); |
|
126
|
|
|
$resolver->setAllowedValues('target', [ |
|
127
|
|
|
'_self', |
|
128
|
|
|
'_blank', |
|
129
|
|
|
]); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Set resolved options. |
|
134
|
|
|
* |
|
135
|
|
|
* @param array $options |
|
136
|
|
|
* @return void |
|
137
|
|
|
*/ |
|
138
|
|
|
public function setOptions(array $options) |
|
139
|
|
|
{ |
|
140
|
|
|
$this->length = $options['length']; |
|
141
|
|
|
$this->replace = $options['replace']; |
|
142
|
|
|
$this->template = $options['template']; |
|
143
|
|
|
$this->title = $options['title']; |
|
144
|
|
|
$this->icon = $options['icon']; |
|
145
|
|
|
$this->route = $options['route']; |
|
146
|
|
|
$this->parameters = $options['parameters']; |
|
147
|
|
|
$this->target = $options['target']; |
|
148
|
|
|
$this->url = $options['url']; |
|
149
|
|
|
$this->text = $options['text']; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Define field type. |
|
154
|
|
|
* |
|
155
|
|
|
* @return string |
|
156
|
|
|
*/ |
|
157
|
|
|
public function getType() |
|
158
|
|
|
{ |
|
159
|
|
|
return 'link'; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Define Twig engine. |
|
164
|
|
|
* |
|
165
|
|
|
* @param Twig_Environment $twig |
|
166
|
|
|
*/ |
|
167
|
|
|
public function setTwig(Twig_Environment $twig) |
|
168
|
|
|
{ |
|
169
|
|
|
$this->twig = $twig; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Define entity. It will be use to fill parameters with properties values. |
|
174
|
|
|
* |
|
175
|
|
|
* @param $entity |
|
176
|
|
|
*/ |
|
177
|
|
|
public function setEntity($entity) |
|
178
|
|
|
{ |
|
179
|
|
|
$this->entity = $entity; |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|