src/Cms/Modules/AlertBundle/Form/Type/Alert/AlertDataType.php line 33

Open in your IDE?
  1. <?php
  2. namespace Cms\Modules\AlertBundle\Form\Type\Alert;
  3. use Cms\ContainerBundle\Entity\Container;
  4. use Cms\ContainerBundle\Service\ContainerService;
  5. use Cms\CoreBundle\Form\Type\DateTimeType;
  6. use Cms\CoreBundle\Form\Type\HtmlType;
  7. use Cms\CoreBundle\Form\Type\SlugType;
  8. use Cms\CoreBundle\Util\DateTimeUtils;
  9. use Cms\FileBundle\Form\Type\FileModalType;
  10. use Cms\FileBundle\Form\Type\SpecialFileModalType;
  11. use Cms\ModuleBundle\Form\StandardMetadataType;
  12. use Cms\ModuleBundle\Form\Type\AbstractModuleDataType;
  13. use Cms\ModuleBundle\Validator\Constraints\ModuleReservedSlugs;
  14. use Cms\ModuleBundle\Validator\Constraints\ModuleSlug;
  15. use Cms\Modules\AlertBundle\Form\Type\AlertBehaviorType;
  16. use Cms\Modules\AlertBundle\Form\Type\AlertLevelType;
  17. use Cms\Modules\AlertBundle\Model\Alert\AlertData;
  18. use Cms\TagBundle\Form\Type\TaggableType;
  19. use Symfony\Component\Form\FormBuilderInterface;
  20. use Symfony\Component\Form\Extension\Core\Type\TextType;
  21. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  22. use Symfony\Component\Form\FormEvent;
  23. use Symfony\Component\Form\FormEvents;
  24. use Symfony\Component\Validator\Constraints\NotBlank;
  25. /**
  26.  * Class AlertDataType
  27.  *
  28.  * @package Cms\Modules\AlertBundle\Form\Type\Alert
  29.  */
  30. final class AlertDataType extends AbstractModuleDataType
  31. {
  32.     /**
  33.      * @var ContainerService
  34.      */
  35.     private ContainerService $containerService;
  36.     /**
  37.      * @param ContainerService $containerService
  38.      */
  39.     public function __construct(
  40.         ContainerService $containerService
  41.     )
  42.     {
  43.         $this->containerService $containerService;
  44.     }
  45.     /**
  46.      * {@inheritdoc}
  47.      */
  48.     public function buildForm(FormBuilderInterface $builder, array $options): void
  49.     {
  50.         /** @var Container $container */
  51.         $container $options['container'];
  52.         $builder
  53.             ->add('title'TextType::class, array(
  54.                 'constraints' => array(
  55.                     new NotBlank(),
  56.                 ),
  57.             ))
  58.             ->add('slug'SlugType::class, array(
  59.                 'dependency' => array('title'),
  60.                 'helpText' => 'Attention! Changing the URL can cause broken links across your site. Proceed with caution.',
  61.                 'constraints' => array(
  62.                     new ModuleSlug(),
  63.                     new ModuleReservedSlugs(),
  64.                 ),
  65.                 'slugPrefix' => sprintf(
  66.                     '%s/alert/{id}/',
  67.                     rtrim($this->containerService->getFrontPath($container), '/')
  68.                 ),
  69.             ))
  70.             ->add('startDate'DateTimeType::class, array(
  71.                 'constraints' => array(
  72.                     new NotBlank(),
  73.                 ),
  74.             ))
  75.             ->add('expiryDate'DateTimeType::class, array(
  76.                 'constraints' => array(
  77.                     new NotBlank(),
  78.                 ),
  79.             ))
  80.             ->add('externalLink'SpecialFileModalType::class, array(
  81.                 'required' => false,
  82.                 'container' => $container->getId(),
  83.             ))
  84.             ->add('abstract'TextareaType::class, array(
  85.                 'required' => false,
  86.             ))
  87.             ->add('content'HtmlType::class, array(
  88.                 'baseHref' => $this->containerService->getFrontLink($container),
  89.             ))
  90.             /*
  91.             ->add('attachments', AttachableType::class, array(
  92.                 'container' => $container->getId(),
  93.             ))
  94.             */
  95.             ->add('standardMetadata'StandardMetadataType::class, [])
  96.             ->add('socialImage'FileModalType::class, array(
  97.                 'container' => $container->getId(),
  98.             ))
  99.             ->add('tags'TaggableType::class, [])
  100.             ->add('level'AlertLevelType::class, [])
  101.             ->add('behavior'AlertBehaviorType::class, [])
  102.         ;
  103.         $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($options) {
  104.             /** @var AlertData $data */
  105.             $data $event->getData();
  106.             // Add interval only for new items
  107.             if ($options['create'] && ( ! $options['clone']) && $data->getExpiryDate() instanceof \DateTime) {
  108.                 // +1 hour gap between start and end dates
  109.                 $data->setExpiryDate(
  110.                     $data->getExpiryDate()->add(DateTimeUtils::duration('PT1H'))
  111.                 );
  112.             }
  113.         });
  114.     }
  115. }