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

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(!str_contains($this->current_route,"api.cms_page.search")){
  74.                 if (in_array($this->current_route, ['frontend.navigation.page''frontend.home.page'])){
  75.                     // muss nicht ausgeführt werden. onPageLoaded-Event enthält die Übersetzungen und
  76.                     // wird später ausgeführt
  77.                     continue;
  78.                 }
  79.                 $this->updateAkuCmsSlot($slot$context$sales_channel_context);
  80.                 }
  81.             }
  82.         }
  83.     }
  84.     /**
  85.      * Event benötigt für die Anzeige von Category-Seiten
  86.      */
  87.     public function onPageLoaded($event) {
  88.         $entities $event->getResult();
  89.         $sales_channel_context $event->getSalesChannelContext();
  90.         $context $event->getContext();
  91.         foreach($entities as $entity) {
  92.             // Shopware\Core\Content\Cms\CmsPageEntity;
  93.             $slots $entity->getElementsOfType('aku-cms-factory');
  94.             foreach ($slots as $slot){
  95.                 $this->updateAkuCmsSlot($slot$context$sales_channel_context);
  96.             }
  97.         }
  98.     }
  99.     public function updateAkuCmsSlot(CmsSlotEntity $entity$context$sales_channel_context){
  100.         $slot_config $entity->getConfig();
  101.         $template '';
  102.         $fields = [];
  103.         $id = isset($slot_config['cms_factory_element_id']) 
  104.             ? $slot_config['cms_factory_element_id']['value']
  105.             : null;
  106.         $field_values = [];
  107.         //var_dump($slot_config['field_values']);die();
  108.         if (isset($slot_config['field_values'])) {
  109.             if (is_string($slot_config['field_values']['value'])){
  110.                 // neuerdings als string gespeichert
  111.                 $field_values json_decode($slot_config['field_values']['value'], true);
  112.             } else {
  113.                 // Legacy
  114.                 $field_values $slot_config['field_values']['value'];
  115.             }
  116.         }
  117.         if ($id && Uuid::isValid($id)) {
  118.             $criteria = new Criteria([$id]);
  119.             $criteria->setLimit(1);
  120.             $akuCmsFactory $this->cmsFactoryElementRepository
  121.                 ->search($criteria$context)->getEntities()->first();
  122.             $template null === $akuCmsFactory
  123.                 ''
  124.                 $akuCmsFactory->getTwig();
  125.             $fields =  null === $akuCmsFactory || null === $akuCmsFactory->getFields()
  126.                 ? []
  127.                 : json_decode($akuCmsFactory->getFields(), true);
  128.         }
  129.         $data $this->CmsElementService->getData($fields$field_values$context$sales_channel_context);
  130.         $data['__template'] = $template;
  131.         $entity->setData(new ArrayStruct($data)); 
  132.     }
  133.     
  134. }