vendor/connectholland/cookie-consent-bundle/Form/CookieConsentType.php line 21

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of the ConnectHolland CookieConsentBundle package.
  5.  * (c) Connect Holland.
  6.  */
  7. namespace ConnectHolland\CookieConsentBundle\Form;
  8. use ConnectHolland\CookieConsentBundle\Cookie\CookieChecker;
  9. use Symfony\Component\Form\AbstractType;
  10. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  11. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  12. use Symfony\Component\Form\FormBuilderInterface;
  13. use Symfony\Component\Form\FormEvent;
  14. use Symfony\Component\Form\FormEvents;
  15. use Symfony\Component\OptionsResolver\OptionsResolver;
  16. class CookieConsentType extends AbstractType
  17. {
  18.     /**
  19.      * @var CookieChecker
  20.      */
  21.     protected $cookieChecker;
  22.     /**
  23.      * @var array
  24.      */
  25.     protected $cookieCategories;
  26.     /**
  27.      * @var bool
  28.      */
  29.     protected $cookieConsentSimplified;
  30.     public function __construct(CookieChecker $cookieChecker, array $cookieCategoriesbool $cookieConsentSimplified false)
  31.     {
  32.         $this->cookieChecker           $cookieChecker;
  33.         $this->cookieCategories        $cookieCategories;
  34.         $this->cookieConsentSimplified $cookieConsentSimplified;
  35.     }
  36.     /**
  37.      * Build the cookie consent form.
  38.      */
  39.     public function buildForm(FormBuilderInterface $builder, array $options): void
  40.     {
  41.         foreach ($this->cookieCategories as $category) {
  42.             $builder->add($categoryChoiceType::class, [
  43.                 'expanded' => true,
  44.                 'multiple' => false,
  45.                 'data'     => $this->cookieChecker->isCategoryAllowedByUser($category) ? 'true' 'false',
  46.                 'choices'  => [
  47.                     ['ch_cookie_consent.yes' => 'true'],
  48.                     ['ch_cookie_consent.no' => 'false'],
  49.                 ],
  50.                 'disabled' => $category == 'analytics' true false,
  51.                 'row_attr' => [
  52.                     //'class' => $category == 'analytics' ? 'disabled' : 'enabled',
  53.                     'disabled' => $category == 'analytics' true false,
  54.                 ],
  55.             ]);
  56.         }
  57.         if ($this->cookieConsentSimplified === false) {
  58.             //$builder->add('save', SubmitType::class, ['label' => 'ch_cookie_consent.save', 'attr' => ['class' => 'btn ch-cookie-consent__btn']]);
  59.             $builder->add('save'SubmitType::class, ['label' => 'ch_cookie_consent.save''attr' => ['class' => 'btn ch-cookie-consent__btn btn-success']]);
  60.         } else {
  61.             $builder->add('use_only_functional_cookies'SubmitType::class, ['label' => 'ch_cookie_consent.use_only_functional_cookies''attr' => ['class' => 'btn ch-cookie-consent__btn']]);
  62.             $builder->add('use_all_cookies'SubmitType::class, ['label' => 'ch_cookie_consent.use_all_cookies''attr' => ['class' => 'btn ch-cookie-consent__btn ch-cookie-consent__btn--secondary']]);
  63.             $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
  64.                 $data $event->getData();
  65.                 foreach ($this->cookieCategories as $category) {
  66.                     $data[$category] = isset($data['use_all_cookies']) ? 'true' 'false';
  67.                 }
  68.                 $event->setData($data);
  69.             });
  70.             $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
  71.             });
  72.         }
  73.     }
  74.     /**
  75.      * Default options.
  76.      */
  77.     public function configureOptions(OptionsResolver $resolver): void
  78.     {
  79.         $resolver->setDefaults([
  80.             'translation_domain' => 'CHCookieConsentBundle',
  81.         ]);
  82.     }
  83. }