custom/plugins/MoorlFormBuilder/src/Subscriber/StorefrontSubscriber.php line 38

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace MoorlFormBuilder\Subscriber;
  3. use MoorlFormBuilder\Core\Service\FormService;
  4. use Shopware\Core\Content\Cms\CmsPageEvents;
  5. use Shopware\Core\Content\Media\Event\MediaFileExtensionWhitelistEvent;
  6. use Shopware\Core\Framework\Api\Context\SalesChannelApiSource;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class StorefrontSubscriber implements EventSubscriberInterface
  10. {
  11.     private FormService $formService;
  12.     public function __construct(
  13.         FormService $formService
  14.     )
  15.     {
  16.         $this->formService $formService;
  17.     }
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [
  21.             CmsPageEvents::SLOT_LOADED_EVENT => 'onEntityLoadedEvent',
  22.             MediaFileExtensionWhitelistEvent::class => 'onMediaFileExtensionWhitelist'
  23.         ];
  24.     }
  25.     public function onMediaFileExtensionWhitelist(MediaFileExtensionWhitelistEvent $event)
  26.     {
  27.         $whitelist $event->getWhitelist();
  28.         $whitelist $this->formService->extendFileTypeWhitelist($whitelist);
  29.         $event->setWhitelist($whitelist);
  30.     }
  31.     public function onEntityLoadedEvent(EntityLoadedEvent $event): void
  32.     {
  33.         $source $event->getContext()->getSource();
  34.         if (!$source instanceof SalesChannelApiSource) {
  35.             return;
  36.         }
  37.         foreach ($event->getEntities() as $entity) {
  38.             if ($entity->getType() === 'moorl-form-builder') {
  39.                 $config $entity->getConfig();
  40.                 $formId $config $config['form']['value'] : null;
  41.                 if (!$formId) {
  42.                     $config $entity->getTranslated();
  43.                     $formId $config['config']['form']['value'];
  44.                     if (!$formId) {
  45.                         throw new \Exception("No form set for the current storefront language");
  46.                     }
  47.                 }
  48.                 $this->formService->setContext($event->getContext());
  49.                 $this->formService->setCheckCache(true);
  50.                 $this->formService->initCurrentForm($formId);
  51.                 if (!$this->formService->getCurrentForm()) {
  52.                     continue;
  53.                 }
  54.                 $this->formService->setCheckCache(false);
  55.                 $entity->setData($this->formService->getCurrentForm());
  56.             }
  57.         }
  58.     }
  59. }