src/Form/EventListener/Global/AddWeekDaysFieldSubscriber.php line 21

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Form\EventListener\Global;
  4. use App\Entity\Shop\ShopInterface;
  5. use App\Form\FormHelper;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  8. use Symfony\Component\Form\FormEvent;
  9. use Symfony\Component\Form\FormEvents;
  10. class AddWeekDaysFieldSubscriber extends FormHelper implements EventSubscriberInterface
  11. {
  12.     public static function getSubscribedEvents(): array
  13.     {
  14.         return [FormEvents::PRE_SET_DATA => 'preSetData'];
  15.     }
  16.     public function preSetData(FormEvent $event): void
  17.     {
  18.         $choices ShopInterface::WEEKDAYS;
  19.         if (($key array_search($event->getData()->getDay(), $choicestrue)) !== false) {
  20.             unset($choices[$key]);
  21.         }
  22.         $event->getForm()->add(
  23.             'days',
  24.             ChoiceType::class,
  25.             $this->getChoiceParameters('app.shop_owner.pages.schedule.list.days'self::PLACEHOLDER_SAME_AS_LABELtrue, [
  26.                 'choices' => $choices,
  27.                 'multiple' => true,
  28.             ])
  29.         );
  30.     }
  31. }