|
17 | 17 | use Symfony\Component\Form\Extension\Core\Type\HiddenType; |
18 | 18 | use Symfony\Component\Form\Extension\Core\Type\TextType; |
19 | 19 | use Symfony\Component\Form\FormBuilderInterface; |
| 20 | +use Symfony\Component\Form\FormEvent; |
| 21 | +use Symfony\Component\Form\FormEvents; |
20 | 22 |
|
21 | 23 | final class PayPalConfigurationType extends AbstractType |
22 | 24 | { |
| 25 | + private const HIDDEN_FIELDS = [ |
| 26 | + 'merchant_id', |
| 27 | + 'sylius_merchant_id', |
| 28 | + 'partner_attribution_id', |
| 29 | + 'use_authorize', |
| 30 | + ]; |
| 31 | + |
23 | 32 | public function buildForm(FormBuilderInterface $builder, array $options): void |
24 | 33 | { |
| 34 | + $originalData = []; |
| 35 | + |
25 | 36 | $builder |
26 | | - ->add('client_id', TextType::class, ['label' => 'sylius.pay_pal.client_id', 'attr' => ['readonly' => true]]) |
27 | | - ->add('client_secret', TextType::class, ['label' => 'sylius.pay_pal.client_secret', 'attr' => ['readonly' => true]]) |
28 | | - ->add('merchant_id', HiddenType::class, ['label' => 'sylius.pay_pal.client_secret', 'attr' => ['readonly' => true]]) |
29 | | - ->add('sylius_merchant_id', HiddenType::class, ['label' => 'sylius.pay_pal.client_secret', 'attr' => ['readonly' => true]]) |
30 | | - ->add('partner_attribution_id', HiddenType::class, ['label' => 'sylius.pay_pal.partner_attribution_id', 'attr' => ['readonly' => true]]) |
| 37 | + ->add('client_id', TextType::class, ['label' => 'sylius_paypal.client_id', 'attr' => ['readonly' => true]]) |
| 38 | + ->add('client_secret', TextType::class, ['label' => 'sylius_paypal.client_secret', 'attr' => ['readonly' => true]]) |
| 39 | + ->add('merchant_id', HiddenType::class, ['label' => 'sylius_paypal.client_secret', 'attr' => ['readonly' => true]]) |
| 40 | + ->add('sylius_merchant_id', HiddenType::class, ['label' => 'sylius_paypal.client_secret', 'attr' => ['readonly' => true]]) |
| 41 | + ->add('partner_attribution_id', HiddenType::class, ['label' => 'sylius_paypal.partner_attribution_id', 'attr' => ['readonly' => true]]) |
31 | 42 | // we need to force Sylius Payum integration to postpone creating an order, it's the easiest way |
32 | 43 | ->add('use_authorize', HiddenType::class, ['data' => true, 'attr' => ['readonly' => true]]) |
33 | | - ->add('reports_sftp_username', TextType::class, ['label' => 'sylius.pay_pal.sftp_username', 'required' => false]) |
34 | | - ->add('reports_sftp_password', TextType::class, ['label' => 'sylius.pay_pal.sftp_password', 'required' => false]) |
| 44 | + ->add('reports_sftp_username', TextType::class, ['label' => 'sylius_paypal.sftp_username', 'required' => false]) |
| 45 | + ->add('reports_sftp_password', TextType::class, ['label' => 'sylius_paypal.sftp_password', 'required' => false]) |
35 | 46 | ; |
| 47 | + |
| 48 | + $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use (&$originalData): void { |
| 49 | + $data = $event->getData(); |
| 50 | + if (is_array($data)) { |
| 51 | + $originalData = $data; |
| 52 | + } |
| 53 | + }); |
| 54 | + |
| 55 | + $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use (&$originalData): void { |
| 56 | + $submitted = $event->getData() ?? []; |
| 57 | + |
| 58 | + foreach (self::HIDDEN_FIELDS as $field) { |
| 59 | + if ( |
| 60 | + !array_key_exists($field, $submitted) && |
| 61 | + array_key_exists($field, $originalData) |
| 62 | + ) { |
| 63 | + $submitted[$field] = $originalData[$field]; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + $event->setData($submitted); |
| 68 | + }); |
36 | 69 | } |
37 | 70 | } |
0 commit comments