<?php
namespace RhiemExtendedRegistration\Subscriber\Frontend;
use RhiemExtendedRegistration\Components\Services\RegistrationGroupService;
use Shopware\Core\Content\Cms\Events\CmsPageLoadedEvent;
use Shopware\Core\Framework\Struct\StructCollection;
use Shopware\Storefront\Event\StorefrontRenderEvent;
use Shopware\Storefront\Page\Account\CustomerGroupRegistration\CustomerGroupRegistrationPageLoadedEvent;
use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
use Shopware\Storefront\Page\Account\Order\AccountOrderPageLoadedEvent;
use Shopware\Storefront\Page\Account\Overview\AccountOverviewPageLoadedEvent;
use Shopware\Storefront\Page\Account\PaymentMethod\AccountPaymentMethodPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
use Shopware\Storefront\Page\PageLoadedEvent;
use Shopware\Storefront\Page\Account\Login\AccountLoginPageLoadedEvent;
use Shopware\Storefront\Page\Account\Profile\AccountProfilePageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class GroupsToPage implements EventSubscriberInterface
{
/**
* @var RegistrationGroupService
*/
private RegistrationGroupService $registrationGroupService;
public function __construct(RegistrationGroupService $registrationGroupService)
{
$this->registrationGroupService = $registrationGroupService;
}
/**
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [
StorefrontRenderEvent::class => "onStorefrontRenderEvent",
AccountLoginPageLoadedEvent::class => "addGroupsToPage",
AccountOverviewPageLoadedEvent::class => "addGroupsToPage",
AccountProfilePageLoadedEvent::class => "addGroupsToPage",
AccountOrderPageLoadedEvent::class => "addGroupsToPage",
AccountPaymentMethodPageLoadedEvent::class => "addGroupsToPage",
CheckoutRegisterPageLoadedEvent::class => "addGroupsToPage",
CustomerGroupRegistrationPageLoadedEvent::class => "addGroupsToPage",
];
}
/**
* @param StorefrontRenderEvent $event
*/
public function onStorefrontRenderEvent(StorefrontRenderEvent $event)
{
try {
$registrationGroups = $this->registrationGroupService->getRegistrationGroups($event->getSalesChannelContext());
} catch (\Exception $e) {
$registrationGroups = [];
}
if (isset($event->getParameters()['page'])) {
$event->getParameters()['page']->addExtension(
"additionalRegistrationGroups",
new StructCollection($registrationGroups)
);
}
if (isset($event->getParameters()['cmsPage'])) {
$event->getParameters()['cmsPage']->addExtension(
"additionalRegistrationGroups",
new StructCollection($registrationGroups)
);
}
}
public function addGroupsToPage(PageLoadedEvent $event)
{
$page = $event->getPage();
try {
$registrationGroups = $this->registrationGroupService->getRegistrationGroups($event->getSalesChannelContext());
} catch (\Exception $e) {
$registrationGroups = [];
}
if (!$page->getExtension("additionalRegistrationGroups")) {
$page->addExtension("additionalRegistrationGroups",
new StructCollection($registrationGroups));
}
}
public function onCmsPageLoadedEvent(CmsPageLoadedEvent $event)
{
try {
$registrationGroups = $this->registrationGroupService->getRegistrationGroups($event->getSalesChannelContext());
} catch (\Exception $e) {
$registrationGroups = [];
}
$pages = $event->getResult();
foreach ($pages as $page) {
if (!$page->getExtension("additionalRegistrationGroups")) {
$page->addExtension("additionalRegistrationGroups",
new StructCollection($registrationGroups));
}
}
}
}