app/Customize/Form/Extension/Front/AddCartTypeExtension.php line 57

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 Customize\Form\Extension\Front;
  13. use Eccube\Form\Type\AddCartType;
  14. use Customize\Entity\ProductTrait;
  15. use Eccube\Entity\CartItem;
  16. use Eccube\Entity\ProductClass;
  17. use Eccube\Form\DataTransformer\EntityToIdTransformer;
  18. use Eccube\Repository\ProductClassRepository;
  19. use Symfony\Component\Form\AbstractTypeExtension;
  20. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  21. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  22. use Symfony\Component\Form\Extension\Core\Type\IntegerType;
  23. use Symfony\Component\Form\FormBuilderInterface;
  24. use Symfony\Component\Form\FormEvent;
  25. use Symfony\Component\Form\FormEvents;
  26. use Symfony\Component\Form\FormInterface;
  27. use Symfony\Component\Form\FormView;
  28. use Symfony\Component\OptionsResolver\OptionsResolver;
  29. use Symfony\Component\Validator\Constraints as Assert;
  30. use Symfony\Component\Validator\Context\ExecutionContext;
  31. class AddCartTypeExtension extends AbstractTypeExtension
  32. {
  33.     /**
  34.      * {@inheritdoc}
  35.      */
  36.     public function getExtendedType()
  37.     {
  38.         return AddCartType::class;
  39.     }
  40.     
  41.     /**
  42.      * {@inheritdoc}
  43.      */
  44.     public static function getExtendedTypes(): iterable
  45.     {
  46.         yield AddCartType::class;
  47.     }
  48.     /**
  49.      * {@inheritdoc}
  50.      */
  51.     public function buildForm(FormBuilderInterface $builder, array $options)
  52.     {
  53.         /* @var $Product \Eccube\Entity\Product */
  54.         $Product $options['product'];
  55.         $this->Product $Product;
  56.         $ProductClasses $Product->getProductClasses();
  57.         if ($Product->getStockFind()) {
  58.             if ($Product && $Product->getProductClasses()) {
  59.                 if (!is_null($Product->getClassName1())) {
  60.                     if (!is_null($Product->getClassName2())) {
  61.                         $builder->add('classcategory_id1'ChoiceType::class, [
  62.                             'label' => $Product->getClassName1(),
  63.                             'choices' => ['common.select' => '__unselected'] + $Product->getClassCategories1AsFlip(),
  64.                             'mapped' => false,
  65.                         ]);
  66.                     } else {
  67.                         $builder->add('classcategory_id1'ChoiceType::class, [
  68.                             'label' => $Product->getClassName1(),
  69.                             'choices' => ['common.select' => '__unselected'] + $Product->getCombinedProductClass1(),
  70.                             'mapped' => false,
  71.                         ]);
  72.                     }
  73.                 }
  74.                 if (!is_null($Product->getClassName2())) {
  75.                     $builder->add('classcategory_id2'ChoiceType::class, [
  76.                         'label' => $Product->getClassName2(),
  77.                         'choices' => ['common.select' => '__unselected'],
  78.                         'mapped' => false,
  79.                     ]);
  80.                 }
  81.             }
  82.         }
  83.         $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($Product) {
  84.             $data $event->getData();
  85.             $form $event->getForm();
  86.             if (isset($data['classcategory_id1']) && !is_null($Product->getClassName2())) {
  87.                 if ($data['classcategory_id1']) {
  88.                     $form->add('classcategory_id2'ChoiceType::class, [
  89.                         'label' => $Product->getClassName2(),
  90.                         'choices' => ['common.select' => '__unselected'] + $Product->getClassCategories2AsFlip($data['classcategory_id1']),
  91.                         'mapped' => false,
  92.                     ]);
  93.                 }
  94.             }
  95.         });
  96.     }
  97. }