|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
| 4 | + * |
| 5 | + * Copyright (C) 2019 - 2025 Jan Böhmer (https://github.com/jbtronics) |
| 6 | + * |
| 7 | + * This program is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU Affero General Public License as published |
| 9 | + * by the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU Affero General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Affero General Public License |
| 18 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +declare(strict_types=1); |
| 22 | + |
| 23 | +namespace App\Form\Extension; |
| 24 | + |
| 25 | +use Symfony\Component\Form\AbstractTypeExtension; |
| 26 | +use Symfony\Component\Form\Extension\Core\Type\PasswordType; |
| 27 | +use Symfony\Component\Form\FormInterface; |
| 28 | +use Symfony\Component\Form\FormView; |
| 29 | +use Symfony\Component\OptionsResolver\Options; |
| 30 | +use Symfony\Component\OptionsResolver\OptionsResolver; |
| 31 | +use Symfony\Component\Translation\TranslatableMessage; |
| 32 | +use Symfony\Contracts\Translation\TranslatorInterface; |
| 33 | + |
| 34 | +final class TogglePasswordTypeExtension extends AbstractTypeExtension |
| 35 | +{ |
| 36 | + public function __construct(private readonly ?TranslatorInterface $translator) |
| 37 | + { |
| 38 | + } |
| 39 | + |
| 40 | + public static function getExtendedTypes(): iterable |
| 41 | + { |
| 42 | + return [PasswordType::class]; |
| 43 | + } |
| 44 | + |
| 45 | + public function configureOptions(OptionsResolver $resolver): void |
| 46 | + { |
| 47 | + $resolver->setDefaults([ |
| 48 | + 'toggle' => false, |
| 49 | + 'hidden_label' => 'Hide', |
| 50 | + 'visible_label' => 'Show', |
| 51 | + 'hidden_icon' => 'Default', |
| 52 | + 'visible_icon' => 'Default', |
| 53 | + 'button_classes' => ['toggle-password-button'], |
| 54 | + 'toggle_container_classes' => ['toggle-password-container'], |
| 55 | + 'toggle_translation_domain' => null, |
| 56 | + 'use_toggle_form_theme' => true, |
| 57 | + ]); |
| 58 | + |
| 59 | + $resolver->setNormalizer( |
| 60 | + 'toggle_translation_domain', |
| 61 | + static fn (Options $options, $labelTranslationDomain) => $labelTranslationDomain ?? $options['translation_domain'], |
| 62 | + ); |
| 63 | + |
| 64 | + $resolver->setAllowedTypes('toggle', ['bool']); |
| 65 | + $resolver->setAllowedTypes('hidden_label', ['string', TranslatableMessage::class, 'null']); |
| 66 | + $resolver->setAllowedTypes('visible_label', ['string', TranslatableMessage::class, 'null']); |
| 67 | + $resolver->setAllowedTypes('hidden_icon', ['string', 'null']); |
| 68 | + $resolver->setAllowedTypes('visible_icon', ['string', 'null']); |
| 69 | + $resolver->setAllowedTypes('button_classes', ['string[]']); |
| 70 | + $resolver->setAllowedTypes('toggle_container_classes', ['string[]']); |
| 71 | + $resolver->setAllowedTypes('toggle_translation_domain', ['string', 'bool', 'null']); |
| 72 | + $resolver->setAllowedTypes('use_toggle_form_theme', ['bool']); |
| 73 | + } |
| 74 | + |
| 75 | + public function buildView(FormView $view, FormInterface $form, array $options): void |
| 76 | + { |
| 77 | + $view->vars['toggle'] = $options['toggle']; |
| 78 | + |
| 79 | + if (!$options['toggle']) { |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + if ($options['use_toggle_form_theme']) { |
| 84 | + array_splice($view->vars['block_prefixes'], -1, 0, 'toggle_password'); |
| 85 | + } |
| 86 | + |
| 87 | + $controllerName = 'toggle-password'; |
| 88 | + $view->vars['attr']['data-controller'] = trim(\sprintf('%s %s', $view->vars['attr']['data-controller'] ?? '', $controllerName)); |
| 89 | + |
| 90 | + if (false !== $options['toggle_translation_domain']) { |
| 91 | + $controllerValues['hidden-label'] = $this->translateLabel($options['hidden_label'], $options['toggle_translation_domain']); |
| 92 | + $controllerValues['visible-label'] = $this->translateLabel($options['visible_label'], $options['toggle_translation_domain']); |
| 93 | + } else { |
| 94 | + $controllerValues['hidden-label'] = $options['hidden_label']; |
| 95 | + $controllerValues['visible-label'] = $options['visible_label']; |
| 96 | + } |
| 97 | + |
| 98 | + $controllerValues['hidden-icon'] = $options['hidden_icon']; |
| 99 | + $controllerValues['visible-icon'] = $options['visible_icon']; |
| 100 | + $controllerValues['button-classes'] = json_encode($options['button_classes'], \JSON_THROW_ON_ERROR); |
| 101 | + |
| 102 | + foreach ($controllerValues as $name => $value) { |
| 103 | + $view->vars['attr'][\sprintf('data-%s-%s-value', $controllerName, $name)] = $value; |
| 104 | + } |
| 105 | + |
| 106 | + $view->vars['toggle_container_classes'] = $options['toggle_container_classes']; |
| 107 | + } |
| 108 | + |
| 109 | + private function translateLabel(string|TranslatableMessage|null $label, ?string $translationDomain): ?string |
| 110 | + { |
| 111 | + if (null === $this->translator || null === $label) { |
| 112 | + return $label; |
| 113 | + } |
| 114 | + |
| 115 | + if ($label instanceof TranslatableMessage) { |
| 116 | + return $label->trans($this->translator); |
| 117 | + } |
| 118 | + |
| 119 | + return $this->translator->trans($label, domain: $translationDomain); |
| 120 | + } |
| 121 | +} |
0 commit comments