custom/plugins/AkuCmsFactory/src/Subscriber/CmsElementSubscriber.php line 60

Open in your IDE?
  1. <?php
  2. namespace AkuCmsFactory\Subscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
  5. use Shopware\Core\Content\Cms\CmsPageEvents;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  10. use Shopware\Core\Framework\Struct\StructCollection;
  11. use Shopware\Core\Framework\Struct\ArrayStruct;
  12. use Shopware\Core\Framework\Uuid\Uuid;
  13. use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotEntity;
  14. use Shopware\Core\Content\Cms\Events\CmsPageLoadedEvent;
  15. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  16. use AkuCmsFactory\Services\CmsElementService;
  17. class CmsElementSubscriber implements EventSubscriberInterface
  18. {
  19.     protected $cmsFactoryElementRepository;
  20.     protected $CmsElementService;
  21.     protected $SalesChannelContext;
  22.     protected $current_route '';
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         // Return the events to listen to as array like this:  <event to listen to> => <method to execute>
  26.         return [
  27.             // Der folgende Event enthält nicht die Übersetzungen
  28.             // Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent
  29.             //CmsPageEvents::PAGE_LOADED_EVENT => 'onCmsPageLoaded', 
  30.             
  31.             // Dieser Event enthält die Kategorie-Übersetzungen, wird getriggert in 
  32.             // Shopware\Core\Content\Cms\SalesChannel\SalesChannelCmsPage::load()
  33.             // Shopware\Core\Content\Cms\Events\CmsPageLoadedEvent
  34.             CmsPageLoadedEvent::class  => 'onPageLoaded',
  35.             
  36.             // Bei Produktseiten muss dieser Event ausgeführt werden
  37.             CmsPageEvents::SLOT_LOADED_EVENT => 'onSlotLoaded',
  38.             // Sales Channel Context merken für Event onSlotLoaded
  39.             'kernel.controller_arguments' => 'onControllerArguments',
  40.         ];
  41.     }
  42.     public function __construct (
  43.         EntityRepositoryInterface $cmsFactoryElementRepository
  44.         CmsElementService $CmsElementService
  45.     ) {
  46.         $this->cmsFactoryElementRepository $cmsFactoryElementRepository;
  47.         $this->CmsElementService $CmsElementService;
  48.     }
  49.     public function onControllerArguments($event){
  50.         $arguments $event->getArguments();
  51.         
  52.         foreach ($arguments as $argument){
  53.             if (is_object($argument) && 'Shopware\Core\System\SalesChannel\SalesChannelContext' == get_class($argument)){
  54.                 $this->SalesChannelContext $argument;
  55.             }
  56.             if (is_object($argument) && 'Symfony\Component\HttpFoundation\Request' == get_class($argument)){
  57.                 $this->current_route $argument->get('_route');
  58.             }
  59.         }
  60.         
  61.     }
  62.     /**
  63.      * Event benötigt für die Anzeige von Erlebniswelten
  64.      * z.B. mit Plugin sc_PlatformDetailEmotion
  65.      */
  66.     public function onSlotLoaded($event){
  67.         $entities $event->getEntities();
  68.         $context $event->getContext();
  69.         $sales_channel_context $this->SalesChannelContext;
  70.         
  71.         foreach ($entities as $slot){
  72.             if ('aku-cms-factory' == $slot->getType()){
  73.                 if (in_array($this->current_route, ['frontend.navigation.page''frontend.home.page'])){
  74.                     // muss nicht ausgeführt werden. onPageLoaded-Event enthält die Übersetzungen und
  75.                     // wird später ausgeführt
  76.                     continue;
  77.                 }
  78.                 $this->updateAkuCmsSlot($slot$context$sales_channel_context);
  79.             }
  80.         }
  81.     }
  82.     /**
  83.      * Event benötigt für die Anzeige von Category-Seiten
  84.      */
  85.     public function onPageLoaded($event) {
  86.         $entities $event->getResult();
  87.         $sales_channel_context $event->getSalesChannelContext();
  88.         $context $event->getContext();
  89.         foreach($entities as $entity) {
  90.             // Shopware\Core\Content\Cms\CmsPageEntity;
  91.             $slots $entity->getElementsOfType('aku-cms-factory');
  92.             foreach ($slots as $slot){
  93.                 $this->updateAkuCmsSlot($slot$context$sales_channel_context);
  94.             }
  95.         }
  96.     }
  97.     public function updateAkuCmsSlot(CmsSlotEntity $entity$context$sales_channel_context){
  98.         $slot_config $entity->getConfig();
  99.         $template '';
  100.         $fields = [];
  101.         $id = isset($slot_config['cms_factory_element_id']) 
  102.             ? $slot_config['cms_factory_element_id']['value']
  103.             : null;
  104.         $field_values = isset($slot_config['field_values']) 
  105.             ? $slot_config['field_values']['value']
  106.             : [];
  107.         if ($id && Uuid::isValid($id)) {
  108.             $criteria = new Criteria([$id]);
  109.             $criteria->setLimit(1);
  110.             $akuCmsFactory $this->cmsFactoryElementRepository
  111.                 ->search($criteria$context)->getEntities()->first();
  112.             $template null === $akuCmsFactory
  113.                 ''
  114.                 $akuCmsFactory->getTwig();
  115.             $fields =  null === $akuCmsFactory || null === $akuCmsFactory->getFields()
  116.                 ? []
  117.                 : json_decode($akuCmsFactory->getFields(), true);
  118.         }
  119.         $data $this->CmsElementService->getData($fields$field_values$context$sales_channel_context);
  120.         $data['__template'] = $template;
  121.         $entity->setData(new ArrayStruct($data)); 
  122.     }
  123.     
  124. }