1: <?php
2: namespace TIC\CoreBundle\Base;
3:
4: use Symfony\Component\Form\AbstractType as BaseType;
5: use Symfony\Component\OptionsResolver\OptionsResolver;
6:
7: use Symfony\Contracts\Translation\TranslatorInterface;
8:
9: /**
10: * FormType de base disposant d'éventuelles méthodes utiles.
11: * @see https://symfony.com/doc/current/forms.html#form-types
12: */
13: abstract class TICFormType extends BaseType
14: {
15: // Initialisation automatique des propriétés à partir du nom de la classe courante
16: use \TIC\CoreBundle\Traits\ContextProperties; // ctxRoot, ctxName, ctxRoute
17:
18: protected $data_class; // détermination automatique selon le contexte (false pour désactiver)
19: protected $form_alias; // détermination automatique selon le contexte (false pour désactiver)
20:
21: protected $translator;
22:
23: /**
24: * Construction du FormType en renseignant automatiquement l'objet associé et le nom du formulaire.
25: */
26: public function __construct(TranslatorInterface $translator)
27: {
28: $this->translator = $translator;
29:
30: // appel manuel pour l'initialisation des propriétés de contexte (l'appel auto n'étant fait qu'après)
31: $this->setContextProperties('Form|Type');
32:
33: if (null === $this->data_class)
34: $this->data_class = $this->ctxRoot . "\\Entity\\" . $this->ctxName;
35:
36: if (null === $this->form_alias)
37: $this->form_alias = $this->ctxRoute . "form";
38: }
39:
40: /**
41: * {@inheritdoc}
42: */
43: public function configureOptions(OptionsResolver $resolver)
44: {
45: $defaults = array();
46: if ($this->data_class) $defaults['data_class'] = $this->data_class;
47: $resolver->setDefaults($defaults);
48: }
49:
50: /**
51: * {@inheritdoc}
52: */
53: public function getBlockPrefix(): string
54: {
55: if ($this->form_alias) return $this->form_alias;
56: return parent::getBlockPrefix();
57: }
58:
59: /**
60: *
61: */
62: protected function trans(string $token, array $parameters = []): string
63: {
64: return $this->translator->trans($token, $parameters);
65: }
66:
67: }
68: