custom/plugins/FlinkCmsGridBuilder/src/Subscriber/CmsPageSubscriber.php line 69

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Flink\CmsGridBuilder\Subscriber;
  3. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  5. use Shopware\Storefront\Event\StorefrontRenderEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Shopware\Core\Content\Cms\Events\CmsPageLoadedEvent;
  8. class CmsPageSubscriber implements EventSubscriberInterface
  9. {
  10.     protected $cmsGridRepository null;
  11.     public function __construct(
  12.         EntityRepositoryInterface $cmsGridRepository
  13.     )
  14.     {
  15.         $this->cmsGridRepository $cmsGridRepository;
  16.     }
  17.     public static function getSubscribedEvents()
  18.     {
  19.         return [
  20.             // CmsPageLoadedEvent::class => 'onCmsPageLoaded',
  21.             StorefrontRenderEvent::class => 'onStorefrontRender'
  22.         ];
  23.     }
  24.     public function onCmsPageLoaded(CmsPageLoadedEvent $event)
  25.     {
  26.         $entities $event->getResult();
  27.         $gridBlocks = [];
  28.         foreach ($entities as $entity) {
  29.             foreach ($entity->sections as $section) {
  30.                 foreach ($section->blocks as $block) {
  31.                     if (strpos($block->type'flink-cms-grid') === 0) {
  32.                         $cmsGridId str_replace('flink-cms-grid-'''$block->type);
  33.                         if (!isset($gridBlocks[$cmsGridId])) {
  34.                             $gridBlocks[$cmsGridId] = [];
  35.                         }
  36.                         $gridBlocks[$cmsGridId][] = $block;
  37.                     }
  38.                 }
  39.             }
  40.         }
  41.         $criteria = new Criteria(array_keys($gridBlocks));
  42.         $result $this->cmsGridRepository->search($criteria$event->getContext());
  43.         foreach ($result as $cmsGrid) {
  44.             if (isset($gridBlocks[$cmsGrid->getId()])) {
  45.                 foreach ($gridBlocks[$cmsGrid->getId()] as $gridBlock) {
  46.                     $customFields $gridBlock->getCustomFields();
  47.                     $customFields['flinkCmsGrid'] = $cmsGrid;
  48.                     $gridBlock->setCustomFields($customFields);
  49.                 }
  50.             }
  51.         }
  52.     }
  53.     public function onStorefrontRender(StorefrontRenderEvent $event)
  54.     {
  55.         $parameters $event->getParameters();
  56.         if (@is_object($parameters['page']) && method_exists($parameters['page'], 'getCmsPage')) {
  57.             $cmsPage $parameters['page']->getCmsPage();
  58.             if ($cmsPage) {
  59.                 $gridBlocks = [];
  60.                 foreach ($cmsPage->getSections() as $section) {
  61.                     foreach ($section->blocks as $block) {
  62.                         if (strpos($block->type'flink-cms-grid') === 0) {
  63.                             $cmsGridId str_replace('flink-cms-grid-'''$block->type);
  64.                             if (!isset($gridBlocks[$cmsGridId])) {
  65.                                 $gridBlocks[$cmsGridId] = [];
  66.                             }
  67.                             $gridBlocks[$cmsGridId][] = $block;
  68.                         }
  69.                     }
  70.                 }
  71.                 if ($gridBlocks) {
  72.                     $criteria = new Criteria(array_keys($gridBlocks));
  73.                     $result $this->cmsGridRepository->search($criteria$event->getContext());
  74.                     foreach ($result as $cmsGrid) {
  75.                         if (isset($gridBlocks[$cmsGrid->getId()])) {
  76.                             foreach ($gridBlocks[$cmsGrid->getId()] as $gridBlock) {
  77.                                 $customFields $gridBlock->getCustomFields();
  78.                                 $customFields['flinkCmsGrid'] = $cmsGrid;
  79.                                 $gridBlock->setCustomFields($customFields);
  80.                             }
  81.                         }
  82.                     }
  83.                 }
  84.             }
  85.         }
  86.     }
  87. }