custom/plugins/MSTManufacturerSlider6/src/Subscriber/DataSubscriber.php line 39

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace MST\MSTManufacturerSlider6\Subscriber;
  4. use MST\MSTManufacturerSlider6\Repository\SliderRepository;
  5. use MST\MSTManufacturerSlider6\Struct\ConfigCollection;
  6. use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotEntity;
  7. use Shopware\Core\Content\Cms\CmsPageEvents;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\Struct\ArrayEntity;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. class DataSubscriber implements EventSubscriberInterface
  15. {
  16.     private RequestStack $requestStack;
  17.     private SliderRepository $sliderRepository;
  18.     public function __construct(
  19.         RequestStack $requestStack,
  20.         SliderRepository $sliderRepository
  21.     ) {
  22.         $this->requestStack $requestStack;
  23.         $this->sliderRepository $sliderRepository;
  24.     }
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             CmsPageEvents::SLOT_LOADED_EVENT => 'onSlotLoad',
  29.         ];
  30.     }
  31.     public function onSlotLoad(EntityLoadedEvent $event): void
  32.     {
  33.         $navigationId $this->getNavigationId();
  34.         // iterate through all slots on page
  35.         // check for mst-manufacturer-sliders
  36.         /** @var CmsSlotEntity $entity */
  37.         foreach ($event->getEntities() as $entity) {
  38.             if ($entity->getType() == 'mst-manufacturer-slider') {
  39.                 // get element config
  40.                 $entityConfig $this->getEntityConfig($entity);
  41.                 $config = new ConfigCollection($entityConfig);
  42.                 // if we do not have any config, stop here
  43.                 if ($config->count() === 0) {
  44.                     return;
  45.                 }
  46.                 $criteria = new Criteria();
  47.                 $manufacturerIds $this->sliderRepository->getManufacturerIds($criteria$config$navigationId);
  48.                 // stop if no manufacturers were found - can happen if no logos are defined or no items have been sold in the shop yet
  49.                 if (count($manufacturerIds) == 0) {
  50.                     $entity->addExtension('slider-data', new ArrayEntity());
  51.                     return;
  52.                 }
  53.                 $elements $this->sliderRepository->getManufacturerMedia($manufacturerIds$criteria$config);
  54.                 $entity->addExtension('slider-data'$elements);
  55.             }
  56.         }
  57.     }
  58.     /**
  59.      * @return array<string, mixed>
  60.      */
  61.     private function getEntityConfig(CmsSlotEntity $entity): array
  62.     {
  63.         $config $entity->getConfig();
  64.         if ($config !== null) {
  65.             return $config;
  66.         }
  67.         $translated $entity->getTranslated();
  68.         if ($config === null && isset($translated['config'])) {
  69.             return $translated['config'];
  70.         }
  71.         return [];
  72.     }
  73.     private function getNavigationId(): ?string
  74.     {
  75.         /** @var Request $request */
  76.         $request $this->requestStack->getCurrentRequest();
  77.         $navigationId $request->attributes->get('navigationId');
  78.         if ($navigationId !== null) {
  79.             return strval($navigationId);
  80.         }
  81.         return null;
  82.     }
  83. }