custom/plugins/OttCmsAdvanced/src/Subscriber/NavigationPageSubscriber.php line 34

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Ott\CmsAdvanced\Subscriber;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Content\Cms\CmsPageEntity;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\Struct\ArrayStruct;
  8. use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class NavigationPageSubscriber implements EventSubscriberInterface
  11. {
  12.     private Connection $connection;
  13.     private EntityRepositoryInterface $mediaRepository;
  14.     public function __construct(
  15.         Connection $connection,
  16.         EntityRepositoryInterface $mediaRepository
  17.     )
  18.     {
  19.         $this->connection $connection;
  20.         $this->mediaRepository $mediaRepository;
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             NavigationPageLoadedEvent::class => 'onNavigationPageLoaded',
  26.         ];
  27.     }
  28.     public function onNavigationPageLoaded(NavigationPageLoadedEvent $event): void
  29.     {
  30.         $cmsPage $event->getPage()->getCmsPage();
  31.         if (null !== $cmsPage && $this->shouldExtendPage($cmsPage)) {
  32.             $navigationPage $event->getPage();
  33.             $navigationId $navigationPage->getNavigationId();
  34.             $statement = <<<'SQL'
  35. SELECT
  36.     *
  37. FROM
  38.     (SELECT LOWER(HEX(c.id)) AS categoryId, LOWER(HEX(m.id)) AS media
  39.     FROM category AS c
  40.     JOIN product_category AS pc ON c.id = pc.category_id
  41.     JOIN product_media AS pm ON pm.product_id = pc.product_id
  42.     JOIN media AS m ON m.id = pm.media_id
  43.     WHERE parent_id = UNHEX(:navigationId)
  44.     ORDER BY c.id ASC) AS result
  45. GROUP BY categoryId
  46. SQL;
  47.             $preparedStatement $this->connection->prepare($statement);
  48.             $preparedStatement->bindValue('navigationId'$navigationId\PDO::PARAM_STR);
  49.             $preparedStatement->execute();
  50.             $result $preparedStatement->fetchAll();
  51.             if (!empty($result)) {
  52.                 $mediaIds = [];
  53.                 foreach ($result as $category) {
  54.                     $mediaIds[] = $category['media'];
  55.                 }
  56.                 if (!empty($mediaIds)) {
  57.                     $mediaCollection $this->mediaRepository->search(
  58.                         new Criteria($mediaIds),
  59.                         $event->getContext()
  60.                     )->getEntities();
  61.                     if (!empty($mediaCollection)) {
  62.                         $mediaUrls = [];
  63.                         foreach ($mediaCollection->getElements() as $media) {
  64.                             $mediaUrl $media->getUrl();
  65.                             if (!empty($media->getThumbnails())) {
  66.                                 foreach ($media->getThumbnails() as $thumbnail) {
  67.                                     if (400 === $thumbnail->getHeight() && 400 === $thumbnail->getWidth()) {
  68.                                         $mediaUrl $thumbnail->getUrl();
  69.                                         break;
  70.                                     }
  71.                                 }
  72.                             }
  73.                             $mediaUrls[] = $mediaUrl;
  74.                         }
  75.                         foreach ($mediaUrls as $key => $mediaUrl) {
  76.                             $result[$key]['media'] = $mediaUrl;
  77.                         }
  78.                         $navigationPage->addExtension('subcategoriesFirstProductImg', new ArrayStruct([
  79.                             'data' => $result,
  80.                         ]));
  81.                     }
  82.                 }
  83.             }
  84.         }
  85.     }
  86.     private function shouldExtendPage(CmsPageEntity $cmsPage): bool
  87.     {
  88.         if (null !== $cmsPage->getSections()) {
  89.             foreach ($cmsPage->getSections() as $section) {
  90.                 foreach ($section->getBlocks() as $block) {
  91.                     foreach ($block->getSlots()->getElements() as $slot) {
  92.                         if (
  93.                             'categoryteaser' === $slot->getType()
  94.                             && null !== $slot->getConfig()
  95.                             && !empty($slot->getConfig()['useFirstProductImg'])
  96.                             && $slot->getConfig()['useFirstProductImg']['value']
  97.                         ) {
  98.                             return true;
  99.                         }
  100.                     }
  101.                 }
  102.             }
  103.         }
  104.         return false;
  105.     }
  106. }