<?php declare(strict_types=1);
namespace Spno\SalutationOrder\Subscriber;
use Shopware\Core\System\Salutation\SalutationCollection;
use Shopware\Storefront\Page\Account\Login\AccountLoginPageLoadedEvent;
use Shopware\Storefront\Page\Account\Profile\AccountProfilePageLoadedEvent;
use Shopware\Storefront\Page\Address\Detail\AddressDetailPageLoadedEvent;
use Shopware\Storefront\Page\Address\Listing\AddressListingPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
use Shopware\Storefront\Page\PageLoadedEvent;
use Spno\SalutationOrder\Service\SalutationOrderService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class SalutationPageSubscriber implements EventSubscriberInterface
{
private SalutationOrderService $salutationOrderService;
public function __construct(
SalutationOrderService $salutationOrderService
) {
$this->salutationOrderService = $salutationOrderService;
}
public static function getSubscribedEvents()
{
return [
CheckoutRegisterPageLoadedEvent::class => 'onPageLoaded',
AccountLoginPageLoadedEvent::class => 'onPageLoaded',
AddressDetailPageLoadedEvent::class => 'onPageLoaded',
AddressListingPageLoadedEvent::class => 'onPageLoaded',
AccountProfilePageLoadedEvent::class => 'onPageLoaded',
];
}
public function onPageLoaded(PageLoadedEvent $page): void
{
/** @var SalutationCollection $salutations */
$salutations = $page->getPage()->getSalutations();
$salutations = $this->salutationOrderService->sort($salutations);
$page->getPage()->setSalutations($salutations);
}
}