|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Field\Field; |
|
4
|
|
|
|
|
5
|
|
|
use LAG\AdminBundle\Field\AbstractField; |
|
6
|
|
|
use LAG\AdminBundle\Field\TranslatorAwareInterface; |
|
7
|
|
|
use LAG\AdminBundle\Field\TwigAwareInterface; |
|
8
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
9
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
|
10
|
|
|
use Twig_Environment; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* String field. |
|
14
|
|
|
* |
|
15
|
|
|
* It render a string that can be truncated according to is length |
|
16
|
|
|
* Note : according php7 (nightly), class can not be named String anymore |
|
17
|
|
|
*/ |
|
18
|
|
|
class StringField extends AbstractField implements TranslatorAwareInterface, TwigAwareInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var TranslatorInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $translator; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var Twig_Environment |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $twig; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Render a string that can be truncated according to is length. |
|
32
|
|
|
* |
|
33
|
|
|
* @param $value |
|
34
|
|
|
* @return string |
|
35
|
|
|
*/ |
|
36
|
2 |
|
public function render($value) |
|
37
|
|
|
{ |
|
38
|
2 |
|
if ($this->options->get('translation')) { |
|
39
|
|
|
$value = $this |
|
40
|
2 |
|
->translator |
|
41
|
2 |
|
->trans($value); |
|
42
|
|
|
} |
|
43
|
|
|
$maximumStringLength = $this |
|
44
|
2 |
|
->options |
|
45
|
2 |
|
->get('length'); |
|
46
|
|
|
$replaceString = $this |
|
47
|
2 |
|
->options |
|
48
|
2 |
|
->get('replace'); |
|
49
|
|
|
|
|
50
|
|
|
// truncate string if required |
|
51
|
2 |
|
if ($maximumStringLength && strlen($value) > $maximumStringLength) { |
|
52
|
|
|
$value = substr($value, 0, $maximumStringLength).$replaceString; |
|
53
|
|
|
} |
|
54
|
|
|
// #69 : strip tags to avoid layout destruction when content contains html |
|
55
|
2 |
|
$value = strip_tags($value); |
|
56
|
|
|
|
|
57
|
2 |
|
return $value; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Configure options resolver. |
|
62
|
|
|
* |
|
63
|
|
|
* @param OptionsResolver $resolver |
|
64
|
|
|
*/ |
|
65
|
2 |
|
public function configureOptions(OptionsResolver $resolver) |
|
66
|
|
|
{ |
|
67
|
2 |
|
$resolver->setDefaults([ |
|
68
|
2 |
|
'length' => $this->applicationConfiguration->getParameter('string_length'), |
|
69
|
2 |
|
'replace' => $this->applicationConfiguration->getParameter('string_length_truncate'), |
|
70
|
|
|
'translation' => true, |
|
71
|
|
|
]); |
|
72
|
2 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Return form type. |
|
76
|
|
|
* |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getType() |
|
80
|
|
|
{ |
|
81
|
|
|
return 'string'; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Defines translator. |
|
86
|
|
|
* |
|
87
|
|
|
* @param TranslatorInterface $translator |
|
88
|
|
|
* |
|
89
|
|
|
* @return void |
|
90
|
|
|
*/ |
|
91
|
2 |
|
public function setTranslator(TranslatorInterface $translator) |
|
92
|
|
|
{ |
|
93
|
2 |
|
$this->translator = $translator; |
|
94
|
2 |
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Define twig environment. |
|
98
|
|
|
* |
|
99
|
|
|
* @param Twig_Environment $twig |
|
100
|
|
|
* |
|
101
|
|
|
* @return void |
|
102
|
|
|
*/ |
|
103
|
2 |
|
public function setTwig(Twig_Environment $twig) |
|
104
|
|
|
{ |
|
105
|
2 |
|
$this->twig = $twig; |
|
106
|
2 |
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|