src/Juki/Bundle/AppBundle/EventListener/ContentSubscriber.php line 49

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Juki\Bundle\AppBundle\EventListener;
  4. use Doctrine\Common\Util\Debug;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Hitso\Bundle\CommonBundle\Doctrine\Repository\EntityRepository;
  7. use Hitso\Bundle\FormBundle\Event\FormEvent;
  8. use Juki\Bundle\AppBundle\Entity\Dictionary\Shop;
  9. use Juki\Bundle\AppBundle\Validator\Constraints\ArticleShopValidator;
  10. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  13. use Symfony\Component\Validator\Constraints\Callback;
  14. use Symfony\Component\Validator\Constraints\Count;
  15. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  16. /**
  17.  * Class ContentSubscriber
  18.  *
  19.  * @package Juki\Bundle\AppBundle\EventListener
  20.  */
  21. final class ContentSubscriber implements EventSubscriberInterface
  22. {
  23.     /**
  24.      * @var EntityManagerInterface
  25.      */
  26.     private $entityManager;
  27.     /**
  28.      * ContentSubscriber constructor.
  29.      *
  30.      * @param EntityManagerInterface $entityManager
  31.      */
  32.     public function __construct(EntityManagerInterface $entityManager)
  33.     {
  34.         $this->entityManager $entityManager;
  35.     }
  36.     public static function getSubscribedEvents()
  37.     {
  38.         return [
  39.             'content_form_type.form_init' => 'onFormInit',
  40.         ];
  41.     }
  42.     public function onFormInit(FormEvent $event)
  43.     {
  44.         $builder $event->getBuilder();
  45.         $content $builder->getFormConfig()->getData();
  46.         $builder->add(
  47.             'shops',
  48.             ChoiceType::class,
  49.             [
  50.                 'label'              => 'Sklep',
  51.                 'required'           => true,
  52.                 'translation_domain' => 'admin',
  53.                 'multiple'           => false,
  54.                 'expanded'           => false,
  55.                 'choices'            => $this->entityManager->getRepository(Shop::class)->getParentChoices(),
  56.                 'position'           => ['after' => 'slug'],
  57. //                'constraints'        => [
  58. //                    new Count([
  59. //                        'min'        => 1,
  60. //                        'minMessage' => 'Musisz wybrać przynajmniej 1 sklep',
  61. //                    ]),
  62. //                    new Callback([
  63. //                        'callback' => function ($object, ExecutionContextInterface $context) use ($content) {
  64. //
  65. //                            if ($object instanceof Category && $currentCategory instanceof Category) {
  66. //                                if ($object->getId() === $currentCategory->getId()) {
  67. //                                    $context->buildViolation('Kategoria nie może być nadrzędną dla samej siebie.')->addViolation();
  68. //                                }
  69. //                            }
  70. //                        },
  71. //                    ]),
  72. //                ],
  73.             ]
  74.         );
  75.         $builder->add(
  76.             'label',
  77.             ChoiceType::class,
  78.             [
  79.                 'label'        => 'Plakietka (HSM only)',
  80.                 'required'     => false,
  81.                 'empty_data'   => '',
  82.                 'choices'      => [
  83.                     '---'                      => '',
  84.                     'best for start (zielona)' => 'best_for_start',
  85.                     'advanced (żółta)'         => 'advanced',
  86.                     'best deal (niebieska)'    => 'best_deal',
  87.                 ],
  88.                 'position'     => ['after' => 'slug'],
  89.                 'dependencies' => [],
  90.             ]
  91.         );
  92.     }
  93. }