custom/plugins/CompraOrganizationLogoSW6/src/Storefront/Subscriber/FrontendSubscriber.php line 38

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Compra\OrganizationLogoSW6\Storefront\Subscriber;
  3. use Compra\FoundationSW6\Core\System\PluginConfigService;
  4. use Shopware\Core\Framework\Struct\ArrayEntity;
  5. use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. class FrontendSubscriber implements EventSubscriberInterface
  8. {
  9.     /** @var PluginConfigService */
  10.     protected $pluginConfigService;
  11.     public function __construct(
  12.         PluginConfigService $pluginConfigService
  13.     )
  14.     {
  15.         $this->pluginConfigService $pluginConfigService;
  16.     }
  17.     /**
  18.      * @return string[]
  19.      */
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return [
  23.             HeaderPageletLoadedEvent::class => 'onHeaderPageletLoaded'
  24.         ];
  25.     }
  26.     /**
  27.      * Subscribes the HeaderPageletLoadedEvent to add customer group logos to header.
  28.      *
  29.      * @param HeaderPageletLoadedEvent $event
  30.      */
  31.     public function onHeaderPageletLoaded(HeaderPageletLoadedEvent $event)
  32.     {
  33.         // get customer
  34.         $customer $event->getSalesChannelContext()->getCustomer();
  35.         // get all customer group logos
  36.         $customerGroupLogos $this->getCustomerGroupLogos();
  37.         if (!$customer || !$customerGroupLogos || !array_key_exists($customer->getGroupId(), $customerGroupLogos)) {
  38.             return;
  39.         }
  40.         // only add extension to page, if current customer's group has an organization logo
  41.         $event->getPagelet()->addExtension('compra_organization_logo', new ArrayEntity([
  42.             'mediaId' => $customerGroupLogos[$customer->getGroupId()]
  43.         ]));
  44.     }
  45.     /**
  46.      * Helper function to get all customer group logos.
  47.      * - key = customerGroupId
  48.      * - value = logo mediaId
  49.      *
  50.      * @return array
  51.      */
  52.     protected function getCustomerGroupLogos()
  53.     {
  54.         $customerGroupLogos = [];
  55.         for ($i 1$i <= 2$i++) {
  56.             $customerGroup $this->pluginConfigService->getPluginConfig("customerGroup$i");
  57.             $logo $this->pluginConfigService->getPluginConfig("logo$i");
  58.             if ($customerGroup && $logo) {
  59.                 $customerGroupLogos[$customerGroup] = $logo;
  60.             }
  61.         }
  62.         return $customerGroupLogos;
  63.     }
  64. }