1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Lug package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Lug\Bundle\ResourceBundle\Form\Extension; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Form\AbstractTypeExtension; |
15
|
|
|
use Symfony\Component\Form\FormInterface; |
16
|
|
|
use Symfony\Component\Form\FormView; |
17
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author GeLo <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
abstract class AbstractLabelExtension extends AbstractTypeExtension |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
10 |
|
public function buildView(FormView $view, FormInterface $form, array $options) |
28
|
|
|
{ |
29
|
10 |
|
$view->vars['label_translation_arguments'] = $options['label_translation_arguments']; |
30
|
10 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
10 |
|
public function finishView(FormView $view, FormInterface $form, array $options) |
36
|
|
|
{ |
37
|
10 |
|
if ($view->vars['label'] !== null) { |
38
|
2 |
|
return; |
39
|
|
|
} |
40
|
|
|
|
41
|
10 |
|
$view->vars['label'] = $label = $this->createLabelPrefix($view, $form); |
42
|
|
|
|
43
|
10 |
|
if (array_key_exists('label_add', $view->vars) && $view->vars['label_add'] === null) { |
44
|
1 |
|
$view->vars['label_add'] = $label.'.add'; |
45
|
1 |
|
$view->vars['label'] = $label.'.label'; |
46
|
1 |
|
} |
47
|
|
|
|
48
|
10 |
|
if (array_key_exists('label_delete', $view->vars) && $view->vars['label_delete'] === null) { |
49
|
1 |
|
$view->vars['label_delete'] = $label.'.delete'; |
50
|
1 |
|
$view->vars['label'] = $label.'.label'; |
51
|
1 |
|
} |
52
|
10 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
10 |
|
public function configureOptions(OptionsResolver $resolver) |
58
|
|
|
{ |
59
|
|
|
$resolver |
60
|
10 |
|
->setDefaults([ |
61
|
10 |
|
'label_prefix' => null, |
62
|
10 |
|
'label_translation_arguments' => [], |
63
|
10 |
|
]) |
64
|
10 |
|
->setAllowedTypes('label_prefix', ['string', 'null']) |
65
|
10 |
|
->setAllowedTypes('label_translation_arguments', 'array'); |
66
|
10 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param FormView $view |
70
|
|
|
* @param FormInterface $form |
71
|
|
|
* @param int $depth |
72
|
|
|
* |
73
|
|
|
* @return string|null |
74
|
|
|
*/ |
75
|
10 |
|
private function createLabelPrefix(FormView $view, FormInterface $form, $depth = 1) |
76
|
|
|
{ |
77
|
10 |
|
if (($labelPrefix = $form->getConfig()->getOption('label_prefix')) === null) { |
78
|
9 |
|
return $this->createParentLabelPrefix($view, $form, $depth); |
79
|
|
|
} |
80
|
|
|
|
81
|
6 |
|
if ($depth === 1 && ($rootLabelPrefix = $this->createParentLabelPrefix($view, $form, $depth)) !== null) { |
82
|
1 |
|
return $rootLabelPrefix; |
83
|
|
|
} |
84
|
|
|
|
85
|
6 |
|
return $labelPrefix; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param FormView $view |
90
|
|
|
* @param FormInterface $form |
91
|
|
|
* @param int $depth |
92
|
|
|
* |
93
|
|
|
* @return string|null |
94
|
|
|
*/ |
95
|
10 |
|
private function createParentLabelPrefix(FormView $view, FormInterface $form, $depth) |
96
|
|
|
{ |
97
|
10 |
|
if ($view->parent !== null |
98
|
10 |
|
&& $form->getParent() !== null |
99
|
10 |
|
&& ($label = $this->createLabelPrefix($view->parent, $form->getParent(), ++$depth)) !== null) { |
100
|
5 |
|
return $label.'.'.$view->vars['name']; |
101
|
|
|
} |
102
|
10 |
|
} |
103
|
|
|
} |
104
|
|
|
|