src/Form/EventListener/Global/AddCodeFieldSubscriber.php line 27

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Form\EventListener\Global;
  4. use App\Form\FormHelper;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\Form\Extension\Core\Type\TextType;
  7. use Symfony\Component\Form\FormEvent;
  8. use Symfony\Component\Form\FormEvents;
  9. class AddCodeFieldSubscriber extends FormHelper implements EventSubscriberInterface
  10. {
  11.     public function __construct(
  12.         private string $label 'app.global.fields.code',
  13.         private string $placeholder self::PLACEHOLDER_SAME_AS_LABEL,
  14.         private bool $readOnly true
  15.     ) {
  16.     }
  17.     public static function getSubscribedEvents(): array
  18.     {
  19.         return [FormEvents::PRE_SET_DATA => 'preSetData'];
  20.     }
  21.     public function preSetData(FormEvent $event): void
  22.     {
  23.         $entity $event->getData();
  24.         $mapped true;
  25.         $attr = [];
  26.         if (null !== $entity && null !== $entity->getId()) {
  27.             $mapped false;
  28.             $attr['value'] = $entity->getCode();
  29.         } else {
  30.             $attr['class'] = 'slug-destination solid';
  31.         }
  32.         if (true === $this->readOnly) {
  33.             $attr['readonly'] = 'readonly';
  34.         }
  35.         $event->getForm()->add('code'TextType::class, $this->getTextParameters($this->label$this->placeholdertruecompact('attr''mapped')));
  36.     }
  37. }