custom/plugins/MoorlFormBuilderRegistration/src/Subscriber/StorefrontSubscriber.php line 80

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace MoorlFormBuilderRegistration\Subscriber;
  3. use MoorlFormBuilder\Core\Service\FormService;
  4. use Shopware\Core\Checkout\Customer\CustomerEvents;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\Framework\Event\DataMappingEvent;
  9. use Shopware\Core\System\SystemConfig\SystemConfigService;
  10. use Shopware\Storefront\Page\Account\Login\AccountLoginPageLoadedEvent;
  11. use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
  12. use Shopware\Storefront\Page\PageLoadedEvent;
  13. use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. class StorefrontSubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var SystemConfigService
  19.      */
  20.     private $systemConfigService;
  21.     /**
  22.      * @var EntityRepositoryInterface
  23.      */
  24.     private $customerUploadRepository;
  25.     /**
  26.      * @var FormService
  27.      */
  28.     private $formService;
  29.     public function __construct(
  30.         FormService $formService,
  31.         SystemConfigService $systemConfigService,
  32.         EntityRepositoryInterface $customerUploadRepository
  33.     )
  34.     {
  35.         $this->formService $formService;
  36.         $this->systemConfigService $systemConfigService;
  37.         $this->customerUploadRepository $customerUploadRepository;
  38.     }
  39.     public static function getSubscribedEvents(): array
  40.     {
  41.         return [
  42.             CustomerEvents::MAPPING_REGISTER_CUSTOMER => 'onSignUpMapping',
  43.             AccountLoginPageLoadedEvent::class => 'onPageLoaded',
  44.             CheckoutRegisterPageLoadedEvent ::class => 'onPageLoaded',
  45.             HeaderPageletLoadedEvent::class => 'onHeaderPageletLoaded'
  46.         ];
  47.     }
  48.     public function onHeaderPageletLoaded(HeaderPageletLoadedEvent $event): void
  49.     {
  50.         $salesChannelContext $event->getSalesChannelContext();
  51.         $customer $salesChannelContext->getCustomer();
  52.         if (!$customer) {
  53.             return;
  54.         }
  55.         $customerGroupIds $this->systemConfigService->get('MoorlFormBuilderRegistration.config.customerGroupIds') ?: [];
  56.         if (!in_array($customer->getGroupId(), $customerGroupIds)) {
  57.             return;
  58.         }
  59.         $criteria = new Criteria();
  60.         $criteria->addAssociation('media');
  61.         $criteria->addFilter(new EqualsFilter('customerId'$customer->getId()));
  62.         $uploads $this->customerUploadRepository->search($criteria$salesChannelContext->getContext())->getEntities();
  63.         $customer->addExtension('DocumentUploads'$uploads);
  64.     }
  65.     public function onPageLoaded(PageLoadedEvent $event): void
  66.     {
  67.         $this->formService->setSalesChannelContext($event->getSalesChannelContext());
  68.         $this->formService->initFormsByType($event->getContext(), 'customerRegister');
  69.         $this->formService->initCurrentForm();
  70.         if (!$this->formService->getCurrentForm()) {
  71.             return;
  72.         }
  73.         $event->getPage()->addExtension('MoorlFormBuilderRegistration'$this->formService->getCurrentForm());
  74.     }
  75.     public function onSignUpMapping(DataMappingEvent $event): void
  76.     {
  77.         $violations $this->formService->fire($event->getContext());
  78.         if ($violations) {
  79.             return;
  80.         }
  81.         //$data = $event->getInput();
  82.         $customer $event->getOutput();
  83.         $customer array_merge($customer$this->formService->getCurrentForm()->getPayload());
  84.         $event->setOutput($customer);
  85.     }
  86. }