src/Form/EventSubscriber/Quote/AddOptionValueSubscriber.php line 29

  1. <?php
  2. namespace App\Form\EventSubscriber\Quote;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  5. use Symfony\Component\Form\Extension\Core\Type\TextType;
  6. use Symfony\Component\Form\FormEvent;
  7. use Symfony\Component\Form\FormEvents;
  8. class AddOptionValueSubscriber implements EventSubscriberInterface
  9. {
  10.     // private $locale; 
  11.     // public function __construct(String $locale)
  12.     // { 
  13.     //     $this->locale = $locale;  
  14.     // }
  15.     public static function getSubscribedEvents()
  16.     {
  17.         // Tells the dispatcher that you want to listen on the form.pre_set_data
  18.         // event and that the preSetData method should be called.
  19.         return [FormEvents::PRE_SET_DATA => 'preSetData'];
  20.     }
  21.     public function preSetData(FormEvent $event)
  22.     {
  23.        
  24.         $data $event->getData();
  25.         $opts $data->getProductOption();
  26.         $choices = [];
  27.    
  28.        // dd($data->getProductOption()->getValues());
  29.         foreach ($opts->getValues()->getIterator() as   $value) {
  30.             //$choices[$value->getName()] = $value->getCode();
  31.            // dd($value->getFallbackLocale());
  32.            $choices$value->getTranslations()->getIterator()["en"]->getValue()] = $value->getCode();
  33.            
  34.              
  35.         } 
  36.         $form $event->getForm();
  37.         $form
  38.             ->add('value',ChoiceType::class, array(
  39.                 'choices' =>  $choices,
  40.                 'multiple' =>  false,
  41.                 'expanded' => false,
  42.                 'label'=>$opts->getName(),
  43.                 'required' => true));
  44.     } 
  45. }