1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* (c) Christian Gripp <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Core23\Form\Extension; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Form\AbstractTypeExtension; |
15
|
|
|
use Symfony\Component\Form\Extension\Core\Type\FormType; |
16
|
|
|
use Symfony\Component\Form\FormInterface; |
17
|
|
|
use Symfony\Component\Form\FormView; |
18
|
|
|
use Symfony\Component\OptionsResolver\Options; |
19
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
20
|
|
|
|
21
|
|
|
final class HelpTypeExtension extends AbstractTypeExtension |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
*/ |
26
|
|
|
public function getExtendedType() |
27
|
|
|
{ |
28
|
|
|
return FormType::class; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
|
public function buildView(FormView $view, FormInterface $form, array $options): void |
35
|
|
|
{ |
36
|
|
|
$helpTranslationDomain = $options['help_translation_domain']; |
37
|
|
|
if ($view->parent && null === $helpTranslationDomain) { |
38
|
|
|
$helpTranslationDomain = $view->vars['translation_domain']; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$view->vars = array_replace($view->vars, [ |
42
|
|
|
'help' => $options['help'], |
43
|
|
|
'help_translation_domain' => $helpTranslationDomain, |
44
|
|
|
]); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public function configureOptions(OptionsResolver $resolver): void |
51
|
|
|
{ |
52
|
|
|
$helpTranslationDomainNormalizer = function (Options $options, $helpTranslationDomain) { |
53
|
|
|
if (true === $helpTranslationDomain) { |
54
|
|
|
return $options['translation_domain']; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $helpTranslationDomain; |
58
|
|
|
}; |
59
|
|
|
|
60
|
|
|
$resolver->setDefaults([ |
61
|
|
|
'help' => null, |
62
|
|
|
'help_translation_domain' => true, |
63
|
|
|
]); |
64
|
|
|
|
65
|
|
|
$resolver->setNormalizer('help_translation_domain', $helpTranslationDomainNormalizer); |
66
|
|
|
|
67
|
|
|
$resolver->setAllowedTypes('help', ['null', 'string']); |
68
|
|
|
$resolver->setAllowedTypes('help_translation_domain', ['null', 'bool', 'string']); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|