custom/plugins/GrimmTheme/src/Storefront/Subscriber/DefaultSalutationSubscriber.php line 51

Open in your IDE?
  1. <?php
  2. declare(strict_types 1);
  3. namespace GrimmTheme\Storefront\Subscriber;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  6. use Shopware\Core\Content\Category\CategoryCollection;
  7. use Shopware\Core\Content\Category\Service\NavigationLoaderInterface;
  8. use Shopware\Core\Content\Category\Tree\Tree;
  9. use Shopware\Core\Content\Category\Tree\TreeItem;
  10. use Shopware\Core\System\SystemConfig\SystemConfigService;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  14. use Shopware\Storefront\Event\RouteRequest\LanguageRouteRequestEvent;
  15. use Shopware\Core\System\Language\LanguageCollection;
  16. use Shopware\Storefront\Page\GenericPageLoadedEvent;
  17. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  18. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  19. use Shopware\Core\Framework\Struct\ArrayStruct;
  20. class DefaultSalutationSubscriber implements EventSubscriberInterface {
  21.     /**
  22.      * @var SystemConfigService
  23.      */
  24.     private $systemConfigService;
  25.     /**
  26.      * @var EntityRepositoryInterface
  27.      */
  28.     private $salutationRepository;
  29.     public function __construct(
  30.         SystemConfigService $systemConfigService,
  31.         EntityRepositoryInterface $salutationRepository
  32.     ) {
  33.         $this->systemConfigService $systemConfigService;
  34.         $this->salutationRepository $salutationRepository;
  35.     }
  36.     public static function getSubscribedEvents()
  37.     {
  38.         return [
  39.             GenericPageLoadedEvent::class => 'onGenericPageLoadedEvent'
  40.         ];
  41.     }
  42.     public function onGenericPageLoadedEvent(GenericPageLoadedEvent $event) {
  43.         $salutationId null;
  44.         if (!empty($this->systemConfigService->get('GrimmTheme.config.defaultSalutation'))) {
  45.             $salutationId $this->systemConfigService->get('GrimmTheme.config.defaultSalutation');
  46.         }
  47.         $salutationCollection $this->getSalutation($salutationId$event->getSalesChannelContext());
  48.         // we can go through first here, because:
  49.         //
  50.         // either we access the only one we found with the set id
  51.         // or we just take a random one as first salutation and 
  52.         // set it as default
  53.         if(count($salutationCollection) > ) {
  54.             $event->getPage()->addExtension(
  55.                 'Grimm_defaultSalutation',
  56.                 new ArrayStruct([$salutationCollection->first()])
  57.             );
  58.         }
  59.     }
  60.     private function getSalutation(String $salutationId nullSalesChannelContext $context):EntitySearchResult
  61.     {
  62.         $criteria = new Criteria();
  63.         if($salutationId) {
  64.             $criteria->addFilter(new EqualsFilter('id'$salutationId));
  65.         }
  66.         // EntitySearchResult
  67.         return $this->salutationRepository->search($criteria$context->getContext());
  68.     }
  69. }