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

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\Events\CmsPageLoadedEvent;
  5. use Shopware\Core\Content\Media\Event\MediaFileExtensionWhitelistEvent;
  6. use Shopware\Core\Framework\Api\Context\SalesChannelApiSource;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class StorefrontSubscriber implements EventSubscriberInterface
  9. {
  10.     private FormService $formService;
  11.     public function __construct(
  12.         FormService $formService
  13.     )
  14.     {
  15.         $this->formService = $formService;
  16.     }
  17.     public static function getSubscribedEvents(): array
  18.     {
  19.         return [
  20.             // CIO-AI-Driven: Use CmsPageLoadedEvent, because slot config overrides (e.g. category_translation.slot_config)
  21.             // are applied in SalesChannelCmsPageLoader::overwriteSlotConfig() after the DAL slot loaded event.
  22.             // Listening on cms_slot.loaded would only see the base cms_slot_translation.config.
  23.             CmsPageLoadedEvent::class => 'onCmsPageLoaded',
  24.             MediaFileExtensionWhitelistEvent::class => 'onMediaFileExtensionWhitelist'
  25.         ];
  26.     }
  27.     public function onMediaFileExtensionWhitelist(MediaFileExtensionWhitelistEvent $event)
  28.     {
  29.         $whitelist = $event->getWhitelist();
  30.         $whitelist = $this->formService->extendFileTypeWhitelist($whitelist);
  31.         $event->setWhitelist($whitelist);
  32.     }
  33.     public function onCmsPageLoaded(CmsPageLoadedEvent $event): void
  34.     {
  35.         $source = $event->getContext()->getSource();
  36.         if (!$source instanceof SalesChannelApiSource) {
  37.             return;
  38.         }
  39.         foreach ($event->getResult() as $page) {
  40.             $sections = $page->getSections();
  41.             if ($sections === null) {
  42.                 continue;
  43.             }
  44.             foreach ($sections->getBlocks()->getSlots() as $slot) {
  45.                 if ($slot->getType() !== 'moorl-form-builder') {
  46.                     continue;
  47.                 }
  48.                 $config = $slot->getConfig();
  49.                 if ($config === null) {
  50.                     $config = $slot->getTranslation('config');
  51.                 }
  52.                 $formId = $config['form']['value'] ?? null;
  53.                 if (!$formId) {
  54.                     continue;
  55.                 }
  56.                 $this->formService->setContext($event->getContext());
  57.                 $this->formService->setCheckCache(true);
  58.                 $this->formService->initCurrentForm($formId);
  59.                 if (!$this->formService->getCurrentForm()) {
  60.                     continue;
  61.                 }
  62.                 $this->formService->setCheckCache(false);
  63.                 $slot->setData($this->formService->getCurrentForm());
  64.             }
  65.         }
  66.     }
  67. }