vendor/store.shopware.com/rhiemextendedregistration/src/Subscriber/Frontend/GroupsToPage.php line 75

Open in your IDE?
  1. <?php
  2. namespace RhiemExtendedRegistration\Subscriber\Frontend;
  3. use RhiemExtendedRegistration\Components\Services\RegistrationGroupService;
  4. use Shopware\Core\Content\Cms\Events\CmsPageLoadedEvent;
  5. use Shopware\Core\Framework\Struct\StructCollection;
  6. use Shopware\Storefront\Event\StorefrontRenderEvent;
  7. use Shopware\Storefront\Page\Account\CustomerGroupRegistration\CustomerGroupRegistrationPageLoadedEvent;
  8. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
  9. use Shopware\Storefront\Page\Account\Order\AccountOrderPageLoadedEvent;
  10. use Shopware\Storefront\Page\Account\Overview\AccountOverviewPageLoadedEvent;
  11. use Shopware\Storefront\Page\Account\PaymentMethod\AccountPaymentMethodPageLoadedEvent;
  12. use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
  13. use Shopware\Storefront\Page\PageLoadedEvent;
  14. use Shopware\Storefront\Page\Account\Login\AccountLoginPageLoadedEvent;
  15. use Shopware\Storefront\Page\Account\Profile\AccountProfilePageLoadedEvent;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. class GroupsToPage implements EventSubscriberInterface
  18. {
  19.     /**
  20.      * @var RegistrationGroupService
  21.      */
  22.     private RegistrationGroupService $registrationGroupService;
  23.     public function __construct(RegistrationGroupService $registrationGroupService)
  24.     {
  25.         $this->registrationGroupService $registrationGroupService;
  26.     }
  27.     /**
  28.      * @return string[]
  29.      */
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             StorefrontRenderEvent::class => "onStorefrontRenderEvent",
  34.             AccountLoginPageLoadedEvent::class => "addGroupsToPage",
  35.             AccountOverviewPageLoadedEvent::class => "addGroupsToPage",
  36.             AccountProfilePageLoadedEvent::class => "addGroupsToPage",
  37.             AccountOrderPageLoadedEvent::class => "addGroupsToPage",
  38.             AccountPaymentMethodPageLoadedEvent::class => "addGroupsToPage",
  39.             CheckoutRegisterPageLoadedEvent::class => "addGroupsToPage",
  40.             CustomerGroupRegistrationPageLoadedEvent::class => "addGroupsToPage",
  41.         ];
  42.     }
  43.     /**
  44.      * @param StorefrontRenderEvent $event
  45.      */
  46.     public function onStorefrontRenderEvent(StorefrontRenderEvent $event)
  47.     {
  48.         try {
  49.             $registrationGroups $this->registrationGroupService->getRegistrationGroups($event->getSalesChannelContext());
  50.         } catch (\Exception $e) {
  51.             $registrationGroups = [];
  52.         }
  53.         if (isset($event->getParameters()['page'])) {
  54.             $event->getParameters()['page']->addExtension(
  55.                 "additionalRegistrationGroups",
  56.                 new StructCollection($registrationGroups)
  57.             );
  58.         }
  59.         if (isset($event->getParameters()['cmsPage'])) {
  60.             $event->getParameters()['cmsPage']->addExtension(
  61.                 "additionalRegistrationGroups",
  62.                 new StructCollection($registrationGroups)
  63.             );
  64.         }
  65.     }
  66.     public function addGroupsToPage(PageLoadedEvent $event)
  67.     {
  68.         $page $event->getPage();
  69.         try {
  70.             $registrationGroups $this->registrationGroupService->getRegistrationGroups($event->getSalesChannelContext());
  71.         } catch (\Exception $e) {
  72.             $registrationGroups = [];
  73.         }
  74.         if (!$page->getExtension("additionalRegistrationGroups")) {
  75.             $page->addExtension("additionalRegistrationGroups",
  76.                 new StructCollection($registrationGroups));
  77.         }
  78.     }
  79.     public function onCmsPageLoadedEvent(CmsPageLoadedEvent $event)
  80.     {
  81.         try {
  82.             $registrationGroups $this->registrationGroupService->getRegistrationGroups($event->getSalesChannelContext());
  83.         } catch (\Exception $e) {
  84.             $registrationGroups = [];
  85.         }
  86.         $pages $event->getResult();
  87.         foreach ($pages as $page) {
  88.             if (!$page->getExtension("additionalRegistrationGroups")) {
  89.                 $page->addExtension("additionalRegistrationGroups",
  90.                     new StructCollection($registrationGroups));
  91.             }
  92.         }
  93.     }
  94. }