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.     /**
  31.      * @var bool
  32.      */
  33.     protected $csrfProtection;
  34.     public function __construct(
  35.         CookieChecker $cookieChecker,
  36.         array $cookieCategories,
  37.         bool $cookieConsentSimplified false,
  38.         bool $csrfProtection true
  39.     ) {
  40.         $this->cookieChecker           $cookieChecker;
  41.         $this->cookieCategories        $cookieCategories;
  42.         $this->cookieConsentSimplified $cookieConsentSimplified;
  43.         $this->csrfProtection          $csrfProtection;
  44.     }
  45.     /**
  46.      * Build the cookie consent form.
  47.      */
  48.     public function buildForm(FormBuilderInterface $builder, array $options): void
  49.     {
  50.         foreach ($this->cookieCategories as $category) {
  51.             $builder->add($categoryChoiceType::class, [
  52.                 'expanded' => true,
  53.                 'multiple' => false,
  54.                 'data'     => $this->cookieChecker->isCategoryAllowedByUser($category) ? 'true' 'false',
  55.                 'choices'  => [
  56.                     ['ch_cookie_consent.yes' => 'true'],
  57.                     ['ch_cookie_consent.no' => 'false'],
  58.                 ],
  59.             ]);
  60.         }
  61.         if ($this->cookieConsentSimplified === false) {
  62.             $builder->add('save'SubmitType::class, ['label' => 'ch_cookie_consent.save''attr' => ['class' => 'btn ch-cookie-consent__btn']]);
  63.         } else {
  64.             $builder->add('use_only_functional_cookies'SubmitType::class, ['label' => 'ch_cookie_consent.use_only_functional_cookies''attr' => ['class' => 'btn ch-cookie-consent__btn']]);
  65.             $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']]);
  66.             $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
  67.                 $data $event->getData();
  68.                 foreach ($this->cookieCategories as $category) {
  69.                     $data[$category] = isset($data['use_all_cookies']) ? 'true' 'false';
  70.                 }
  71.                 $event->setData($data);
  72.             });
  73.         }
  74.     }
  75.     /**
  76.      * Default options.
  77.      */
  78.     public function configureOptions(OptionsResolver $resolver): void
  79.     {
  80.         $resolver->setDefaults([
  81.             'translation_domain' => 'CHCookieConsentBundle',
  82.             'csrf_protection' => $this->csrfProtection,
  83.         ]);
  84.     }
  85. }