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

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.     /**
  12.      * @var FormService
  13.      */
  14.     private $formService;
  15.     public function __construct(
  16.         FormService $formService
  17.     )
  18.     {
  19.         $this->formService $formService;
  20.     }
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             CmsPageEvents::SLOT_LOADED_EVENT => 'onEntityLoadedEvent',
  25.             MediaFileExtensionWhitelistEvent::class => 'onMediaFileExtensionWhitelist'
  26.         ];
  27.     }
  28.     public function onMediaFileExtensionWhitelist(MediaFileExtensionWhitelistEvent $event)
  29.     {
  30.         $whitelist $event->getWhitelist();
  31.         $whitelist $this->formService->extendFileTypeWhitelist($whitelist);
  32.         $event->setWhitelist($whitelist);
  33.     }
  34.     public function onEntityLoadedEvent(EntityLoadedEvent $event): void
  35.     {
  36.         $source $event->getContext()->getSource();
  37.         if (!$source instanceof SalesChannelApiSource) {
  38.             return;
  39.         }
  40.         foreach ($event->getEntities() as $entity) {
  41.             if ($entity->getType() == 'moorl-form-builder') {
  42.                 $config $entity->getConfig();
  43.                 $formId $config['form']['value'];
  44.                 if (!$formId) {
  45.                     $config $entity->getTranslated();
  46.                     $formId $config['config']['form']['value'];
  47.                     if (!$formId) {
  48.                         throw new \Exception("No form set for the current storefront language");
  49.                     }
  50.                 }
  51.                 $this->formService->setContext($event->getContext());
  52.                 $this->formService->setCheckCache(true);
  53.                 $this->formService->initCurrentForm($formId);
  54.                 if (!$this->formService->getCurrentForm()) {
  55.                     continue;
  56.                 }
  57.                 $this->formService->setCheckCache(false);
  58.                 $entity->setData($this->formService->getCurrentForm());
  59.             }
  60.         }
  61.     }
  62. }