<?php
declare(strict_types=1);
namespace Juki\Bundle\AppBundle\EventListener;
use Doctrine\Common\Util\Debug;
use Doctrine\ORM\EntityManagerInterface;
use Hitso\Bundle\CommonBundle\Doctrine\Repository\EntityRepository;
use Hitso\Bundle\FormBundle\Event\FormEvent;
use Juki\Bundle\AppBundle\Entity\Dictionary\Shop;
use Juki\Bundle\AppBundle\Validator\Constraints\ArticleShopValidator;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Constraints\Count;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
/**
* Class ContentSubscriber
*
* @package Juki\Bundle\AppBundle\EventListener
*/
final class ContentSubscriber implements EventSubscriberInterface
{
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* ContentSubscriber constructor.
*
* @param EntityManagerInterface $entityManager
*/
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
public static function getSubscribedEvents()
{
return [
'content_form_type.form_init' => 'onFormInit',
];
}
public function onFormInit(FormEvent $event)
{
$builder = $event->getBuilder();
$content = $builder->getFormConfig()->getData();
$builder->add(
'shops',
ChoiceType::class,
[
'label' => 'Sklep',
'required' => true,
'translation_domain' => 'admin',
'multiple' => false,
'expanded' => false,
'choices' => $this->entityManager->getRepository(Shop::class)->getParentChoices(),
'position' => ['after' => 'slug'],
// 'constraints' => [
// new Count([
// 'min' => 1,
// 'minMessage' => 'Musisz wybrać przynajmniej 1 sklep',
// ]),
// new Callback([
// 'callback' => function ($object, ExecutionContextInterface $context) use ($content) {
//
// if ($object instanceof Category && $currentCategory instanceof Category) {
// if ($object->getId() === $currentCategory->getId()) {
// $context->buildViolation('Kategoria nie może być nadrzędną dla samej siebie.')->addViolation();
// }
// }
// },
// ]),
// ],
]
);
$builder->add(
'label',
ChoiceType::class,
[
'label' => 'Plakietka (HSM only)',
'required' => false,
'empty_data' => '',
'choices' => [
'---' => '',
'best for start (zielona)' => 'best_for_start',
'advanced (żółta)' => 'advanced',
'best deal (niebieska)' => 'best_deal',
],
'position' => ['after' => 'slug'],
'dependencies' => [],
]
);
}
}