custom/plugins/ZweiPunktSidebarBannerSW6/src/Subscriber/AddConfigToView.php line 60

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace ZweiPunktSidebarBannerSW6\Subscriber;
  4. use Shopware\Core\Content\Cms\Events\CmsPageLoadedEvent;
  5. use Shopware\Core\Framework\Context;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Shopware\Core\System\SystemConfig\SystemConfigService;
  13. use ZweiPunktSidebarBannerSW6\ZweiPunktSidebarBannerSW6;
  14. /**
  15.  * Class AddConfigToView
  16.  *
  17.  * Used to format the config data and pass it to the view
  18.  */
  19. class AddConfigToView implements EventSubscriberInterface
  20. {
  21.     private SystemConfigService $systemConfigService;
  22.     private EntityRepository $mediaRepository;
  23.     /**
  24.      * AddConfigToView constructor
  25.      *
  26.      * @param SystemConfigService $systemConfigService
  27.      * @param EntityRepository $mediaRepository
  28.      */
  29.     public function __construct(
  30.         SystemConfigService $systemConfigService,
  31.         EntityRepository $mediaRepository
  32.     ) {
  33.         // Get system config
  34.         $this->systemConfigService $systemConfigService;
  35.         // Get media repository
  36.         $this->mediaRepository $mediaRepository;
  37.     }
  38.     /**
  39.      * @return array<string, string>
  40.      */
  41.     public static function getSubscribedEvents(): array
  42.     {
  43.         return [
  44.             CmsPageLoadedEvent::class => 'onPageLoaded'
  45.         ];
  46.     }
  47.     /**
  48.      * Prepares config data by determining the links for the images and merging the data of the banner
  49.      *
  50.      * @param CmsPageLoadedEvent $event
  51.      */
  52.     public function onPageLoaded(CmsPageLoadedEvent $event): void
  53.     {
  54.         // Get sales channel id
  55.         $salesChannelId $event->getSaleschannelContext()->getSalesChannelId();
  56.         // Get plugin configuration
  57.         $config $this
  58.             ->systemConfigService->get(ZweiPunktSidebarBannerSW6::PLUGIN_NAME '.config'$salesChannelId);
  59.         // Cancel if plugin is not activated
  60.         if ($config['sbActivePlugin'] == false) {
  61.             return;
  62.         }
  63.         // Number of possible banners
  64.         $config['sbNumberOfEntries'] = 5;
  65.         // Collect banner settings
  66.         $config['banner'] = [];
  67.         for ($i 1$i <= $config['sbNumberOfEntries']; $i++) {
  68.             // skip if banner is not activated
  69.             if ($config['sbBanner' $i 'Active'] == false) {
  70.                 continue;
  71.             }
  72.             // Get media id
  73.             if (empty($config['sbBanner' $i])) {
  74.                 continue;
  75.             }
  76.             $mediaId $config['sbBanner' $i];
  77.             // Get media
  78.             $criteria = new Criteria();
  79.             $criteria->addFilter(new EqualsAnyFilter('id', [$mediaId]));
  80.             $media $this
  81.                 ->mediaRepository->search($criteria$event->getContext())->getElements();
  82.             $altText $config['sbAltText' $i] ?? false;
  83.             $url $config['sbLink' $i] ?? false;
  84.             $target = ($config['sbTargetNewWindow' $i]) ? '_blank' '_self';
  85.             $css $config['sbCss' $i] ?? false;
  86.             // Get data of active banner
  87.             $config['banner'][] = [
  88.                 'media'     => $media[$mediaId],
  89.                 'altText'   => $altText,
  90.                 'url'       => $url,
  91.                 'target'    => $target,
  92.                 'css'       => $css];
  93.         }
  94.         $event->getSalesChannelContext()->assign(
  95.             ['zweiPunktSidebarBannerConfig' => $config]
  96.         );
  97.     }
  98. }