vendor/pimcore/portal-engine/src/Form/RecoverPasswordForm.php line 31

  1. <?php
  2. /**
  3. * This source file is available under the terms of the
  4. * Pimcore Open Core License (POCL)
  5. * Full copyright and license information is available in
  6. * LICENSE.md which is distributed with this source code.
  7. *
  8. * @copyright Copyright (c) Pimcore GmbH (https://www.pimcore.com)
  9. * @license Pimcore Open Core License (POCL)
  10. */
  11. namespace Pimcore\Bundle\PortalEngineBundle\Form;
  12. use Pimcore\Bundle\PortalEngineBundle\Event\Auth\LoginFieldTypeEvent;
  13. use Pimcore\Model\DataObject\ClassDefinition;
  14. use Pimcore\Model\DataObject\PortalUser;
  15. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  16. use Symfony\Component\Form\AbstractType;
  17. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  18. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  19. use Symfony\Component\Form\Extension\Core\Type\TextType;
  20. use Symfony\Component\Form\FormBuilderInterface;
  21. use Symfony\Contracts\Translation\TranslatorInterface;
  22. /**
  23. * @internal
  24. */
  25. class RecoverPasswordForm extends AbstractType
  26. {
  27. public function __construct(
  28. private readonly EventDispatcherInterface $eventDispatcher,
  29. private readonly TranslatorInterface $translator,
  30. private readonly array $fields
  31. ) {
  32. }
  33. public function buildForm(FormBuilderInterface $builder, array $options): void
  34. {
  35. $event = new LoginFieldTypeEvent();
  36. $this->eventDispatcher->dispatch($event);
  37. if ($event->getUseEmailField() && $this->fields === ['email']) {
  38. $builder
  39. ->add(
  40. 'userIdentifier',
  41. EmailType::class,
  42. [
  43. 'required' => true,
  44. 'label' => $this->translator->trans('portal-engine.auth.form_label_email', [], 'portal_engine_frontend'),
  45. 'attr' => [
  46. 'autofocus' => true
  47. ]
  48. ]
  49. );
  50. } else {
  51. $classDefinition = ClassDefinition::getById((string) PortalUser::classId());
  52. $fieldLabels = [];
  53. foreach ($this->fields as $fieldName) {
  54. $fieldDefinition = $classDefinition->getFieldDefinition($fieldName);
  55. $fieldTitle = $fieldDefinition->getTitle();
  56. $fieldLabels[] = $this->translator->trans($fieldTitle, [], 'admin');
  57. }
  58. $builder
  59. ->add(
  60. 'userIdentifier',
  61. TextType::class,
  62. [
  63. 'required' => true,
  64. 'label' => implode(' / ', $fieldLabels),
  65. 'attr' => [
  66. 'autofocus' => true
  67. ]
  68. ]
  69. );
  70. }
  71. $builder->add(
  72. 'submit',
  73. SubmitType::class,
  74. [
  75. 'label' => $this->translator->trans('portal-engine.auth.recover_password_button', [], 'portal_engine_frontend'),
  76. ]
  77. );
  78. }
  79. }