src/Eccube/Form/Type/Master/ProductListOrderByType.php line 23

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Eccube\Form\Type\Master;
  13. use Eccube\Form\Type\MasterType;
  14. use Symfony\Component\Form\AbstractType;
  15. use Symfony\Component\Form\FormBuilderInterface;
  16. use Symfony\Component\Form\FormEvent;
  17. use Symfony\Component\Form\FormEvents;
  18. use Symfony\Component\OptionsResolver\OptionsResolver;
  19. class ProductListOrderByType extends AbstractType
  20. {
  21.     /**
  22.      * {@inheritdoc}
  23.      */
  24.     public function buildForm(FormBuilderInterface $builder, array $options)
  25.     {
  26.         $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
  27.             $options $event->getForm()->getConfig()->getOptions();
  28.             if (!$event->getData()) {
  29.                 $data current(array_keys($options['choice_loader']->loadChoiceList()->getValues()));
  30.                 $event->setData($data);
  31.             }
  32.         });
  33.         $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
  34.             $options $event->getForm()->getConfig()->getOptions();
  35.             $values $options['choice_loader']->loadChoiceList()->getValues();
  36.             if (!in_array($event->getData(), $values)) {
  37.                 $data current($values);
  38.                 $event->setData($data);
  39.             }
  40.         });
  41.     }
  42.     /**
  43.      * {@inheritdoc}
  44.      */
  45.     public function configureOptions(OptionsResolver $resolver)
  46.     {
  47.         $resolver->setDefaults([
  48.             'class' => 'Eccube\Entity\Master\ProductListOrderBy',
  49.         ]);
  50.     }
  51.     /**
  52.      * {@inheritdoc}
  53.      */
  54.     public function getBlockPrefix()
  55.     {
  56.         return 'product_list_order_by';
  57.     }
  58.     /**
  59.      * {@inheritdoc}
  60.      */
  61.     public function getParent()
  62.     {
  63.         return MasterType::class;
  64.     }
  65. }