custom/plugins/426-fourtwosixregistrationfields/src/Subscriber/ItalyCountryIdSubscriber.php line 33

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace FourtwosixRegistrationFields\Subscriber;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\System\Country\CountryEntity;
  9. use Shopware\Storefront\Page\GenericPageLoadedEvent;
  10. use Shopware\Storefront\Page\PageLoadedEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class ItalyCountryIdSubscriber implements EventSubscriberInterface
  13. {
  14.     private EntityRepository $countryRepository;
  15.     public function __construct(
  16.         EntityRepository $countryRepository
  17.     ) {
  18.         $this->countryRepository $countryRepository;
  19.     }
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return [
  23.             GenericPageLoadedEvent::class => 'onItalyCountryId',
  24.         ];
  25.     }
  26.     public function onItalyCountryId(PageLoadedEvent $event): void
  27.     {
  28.         $context $event->getContext();
  29.         if (empty($italy $this->getCountryFromISO($context'IT'))) {
  30.             return;
  31.         }
  32.         $countryId $italy->getId();
  33.         $extensions $event->getPage()->getExtensions();
  34.         $extensions['italyCountryId'] = $countryId;
  35.         $event->getPage()->setExtensions($extensions);
  36.     }
  37.     private function getCountryFromISO(Context &$contextstring $iso): CountryEntity
  38.     {
  39.         $criteria = new Criteria();
  40.         $criteria->addFilter(new EqualsFilter('iso'$iso));
  41.         return $this->countryRepository->search($criteria$context)->first();
  42.     }
  43. }