custom/plugins/MoorlFormsClassic/src/Core/Subscriber/SubmitFormSubscriber.php line 29

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace MoorlFormsClassic\Core\Subscriber;
  3. use MoorlForms\Core\Content\Form\FormCollection;
  4. use MoorlForms\Core\Event\SubmitFormSuccessEvent;
  5. use MoorlForms\Core\Service\FbService;
  6. use MoorlForms\MoorlForms;
  7. use MoorlFormsClassic\Core\Content\History\HistoryDefinition;
  8. use Shopware\Core\Framework\Uuid\Uuid;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class SubmitFormSubscriber implements EventSubscriberInterface
  11. {
  12.     private FbService $fbService;
  13.     public function __construct(FbService $fbService)
  14.     {
  15.         $this->fbService $fbService;
  16.     }
  17.     public static function getSubscribedEvents(): array
  18.     {
  19.         return [
  20.             SubmitFormSuccessEvent::class => 'onSubmitFormSuccessEvent',
  21.         ];
  22.     }
  23.     public function onSubmitFormSuccessEvent(SubmitFormSuccessEvent $event): void
  24.     {
  25.         $salesChannelContext $event->getSalesChannelContext();
  26.         $form $event->getForm();
  27.         if (!in_array('write-history'$form->getActions())) {
  28.             return;
  29.         }
  30.         $request $event->getRequest();
  31.         $entity $this->fbService->getEntityFromRequest($request);
  32.         $historyId Uuid::randomHex();
  33.         $data = [
  34.             'id' => $historyId,
  35.             'formId' => $form->getId(),
  36.             'salesChannelId' => $salesChannelContext->getSalesChannelId(),
  37.             'languageId' => $salesChannelContext->getLanguageId(),
  38.             'name' => $entity && method_exists($entity'getName') ? $entity->getName() : null,
  39.             'request' => $request->request->all(),
  40.             'entityName' => $request->request->get(MoorlForms::ENTITY_NAME_KEY),
  41.             'entityId' => $request->request->get(MoorlForms::ENTITY_ID_KEY),
  42.             'payload' => null,
  43.             'media' => $form->getDataStruct()->getAttachmentMediaIds()
  44.         ];
  45.         $form->setHistoryId($historyId);
  46.         $this->fbService->upsertEntity(
  47.             new FormCollection([$form]),
  48.             $salesChannelContext,
  49.             null,
  50.             HistoryDefinition::ENTITY_NAME,
  51.             $data
  52.         );
  53.         if (!in_array('print-view'$form->getActions())) {
  54.             $form->setHistoryId(null);
  55.         }
  56.     }
  57. }