vendor/pimcore/portal-engine/src/Form/LoginForm.php line 28

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under following license:
  6.  * - Pimcore Commercial License (PCL)
  7.  *
  8.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  9.  *  @license    http://www.pimcore.org/license     PCL
  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\CheckboxType;
  18. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  19. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  20. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  21. use Symfony\Component\Form\Extension\Core\Type\TextType;
  22. use Symfony\Component\Form\FormBuilderInterface;
  23. use Symfony\Contracts\Translation\TranslatorInterface;
  24. class LoginForm extends AbstractType
  25. {
  26.     /**
  27.      * @var TranslatorInterface
  28.      */
  29.     private $translator;
  30.     /**
  31.      * @var array
  32.      */
  33.     private $fields;
  34.     /**
  35.      * @var EventDispatcherInterface
  36.      */
  37.     private $eventDispatcher;
  38.     public function __construct(EventDispatcherInterface $eventDispatcherTranslatorInterface $translator, array $fields)
  39.     {
  40.         $this->eventDispatcher $eventDispatcher;
  41.         $this->fields $fields;
  42.         $this->translator $translator;
  43.     }
  44.     public function buildForm(FormBuilderInterface $builder, array $options)
  45.     {
  46.         $event = new LoginFieldTypeEvent();
  47.         $this->eventDispatcher->dispatch($event);
  48.         if ($event->getUseEmailField() && $this->fields === ['email']) {
  49.             $builder
  50.                 ->add(
  51.                     'username',
  52.                     EmailType::class,
  53.                     [
  54.                         'required' => true,
  55.                         'label' => 'portal-engine.auth.form_label_email',
  56.                         'attr' => [
  57.                             'autofocus' => true
  58.                         ]
  59.                     ]
  60.                 );
  61.         } else {
  62.             $classDefinition ClassDefinition::getById((string) PortalUser::classId());
  63.             $fieldLabels = [];
  64.             foreach ($this->fields as $fieldName) {
  65.                 $fieldDefinition $classDefinition->getFieldDefinition($fieldName);
  66.                 $fieldTitle $fieldDefinition->getTitle();
  67.                 $fieldLabels[] = $this->translator->trans($fieldTitle, [], 'admin');
  68.             }
  69.             $builder
  70.                 ->add(
  71.                     'username',
  72.                     TextType::class,
  73.                     [
  74.                         'required' => true,
  75.                         'label' => implode(' / '$fieldLabels),
  76.                         'attr' => [
  77.                             'autofocus' => true
  78.                         ]
  79.                     ]
  80.                 );
  81.         }
  82.         $builder
  83.             ->add(
  84.                 'password',
  85.                 PasswordType::class,
  86.                 [
  87.                     'required' => true,
  88.                     'label' => 'portal-engine.auth.form_label_password',
  89.                 ]
  90.             )->add(
  91.                 '_remember_me',
  92.                 CheckboxType::class,
  93.                 [
  94.                     'label' => 'portal-engine.auth.form_remember_me',
  95.                     'required' => false
  96.                 ]
  97.             )
  98.             ->add(
  99.                 'submit',
  100.                 SubmitType::class,
  101.                 [
  102.                     'label' => 'portal-engine.auth.login_button',
  103.                 ]
  104.             );
  105.     }
  106.     /**
  107.      * @inheritDoc
  108.      */
  109.     public function getBlockPrefix()
  110.     {
  111.         // we need to set this to an empty string as we want _username as input name
  112.         // instead of login_form[_username] to work with the form authenticator out
  113.         // of the box
  114.         return '';
  115.     }
  116. }