custom/static-plugins/SamsonCustomer/src/Subscriber/CmsPageLoadedSubscriber.php line 48

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Samson\Subscriber;
  3. /***
  4.  *
  5.  * This file is part of the "SAMSON Shop" project.
  6.  *
  7.  * For the full copyright and license information, please read the
  8.  * LICENSE.txt file that was distributed with this source code.
  9.  *
  10.  *  (c) 2022
  11.  *
  12.  ***/
  13. use Samson\CustomFieldSet\Constants\CmsSectionCustomFieldConstants;
  14. use Shopware\Core\Content\Cms\Aggregate\CmsSection\CmsSectionEntity;
  15. use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotEntity;
  16. use Shopware\Core\Content\Cms\CmsPageEntity;
  17. use Shopware\Core\Content\Cms\Events\CmsPageLoadedEvent;
  18. use Shopware\Core\Framework\Context;
  19. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  20. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  21. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  22. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  23. /**
  24.  * class CmsPageLoadedSubscriber
  25.  * @package Samson\Subscriber
  26.  * @author Christian Schmitz <christian.schmitz@dkd.de>
  27.  */
  28. class CmsPageLoadedSubscriber implements EventSubscriberInterface
  29. {
  30.     private EntityRepositoryInterface $mediaRepository;
  31.     public function __construct(EntityRepositoryInterface $mediaRepository)
  32.     {
  33.         $this->mediaRepository $mediaRepository;
  34.     }
  35.     public static function getSubscribedEvents(): array
  36.     {
  37.         return [
  38.             CmsPageLoadedEvent::class => 'onCmsPageLoaded',
  39.         ];
  40.     }
  41.     public function onCmsPageLoaded(CmsPageLoadedEvent $event): void
  42.     {
  43.         foreach ($event->getResult() as $cmsPage) {
  44.             $this->sliderDefaults($cmsPage);
  45.             $this->processBlockMedia($cmsPage$event->getSalesChannelContext());
  46.         }
  47.     }
  48.     private function sliderDefaults(CmsPageEntity $cmsPage) {
  49.         $sliderSections $cmsPage->getSections()->filterByProperty'type'"slider-section" );
  50.         $sliderSections->map( function ( CmsSectionEntity $section ) {
  51.             $customFields $section->getCustomFields() ?? [];
  52.             $sliderConfig = [
  53.                 'verticalAlign'          => [
  54.                     'value' => 'center'
  55.                 ],
  56.                 'displayMode'            => [
  57.                     'value' => 'standard'
  58.                 ],
  59.                 'navigationArrows'       => [
  60.                     'value' => ( $customFieldsCmsSectionCustomFieldConstants::CUSTOM_FIELD_SLIDER_SECTION_ARROW_VISIBILITY ] ?? false ) ? 'outer' false
  61.                 ],
  62.                 'navigationArrowsColor'  => [
  63.                     'value' => $customFieldsCmsSectionCustomFieldConstants::CUSTOM_FIELD_SLIDER_SECTION_ARROW_COLOR ] ?? false
  64.                 ],
  65.                 'navigationArrowsBackgroundColor' => [
  66.                     'value' => $customFieldsCmsSectionCustomFieldConstants::CUSTOM_FIELD_SLIDER_SECTION_ARROW_BACKGROUND_COLOR ] ?? false
  67.                 ],
  68.                 'navigationArrowsBorderColor' => [
  69.                     'value' => $customFieldsCmsSectionCustomFieldConstants::CUSTOM_FIELD_SLIDER_SECTION_ARROW_BORDER_COLOR ] ?? false
  70.                 ],
  71.                 'navigationDots'         => [
  72.                     'value' => $customFieldsCmsSectionCustomFieldConstants::CUSTOM_FIELD_SLIDER_SECTION_BULLET_VISIBILITY ] ?? false
  73.                 ],
  74.                 'navigationDotsColor'    => [
  75.                     'value' => $customFieldsCmsSectionCustomFieldConstants::CUSTOM_FIELD_SLIDER_SECTION_BULLET_COLOR ] ?? false
  76.                 ],
  77.                 'navigationDotsPosition' => [
  78.                     'value' => $customFieldsCmsSectionCustomFieldConstants::CUSTOM_FIELD_SLIDER_SECTION_BULLET_POSITION ] ?? 'outer'
  79.                 ],
  80.                 'slideMaxWidth' => [
  81.                     'value' => $customFieldsCmsSectionCustomFieldConstants::CUSTOM_FIELD_SLIDER_SECTION_SLIDE_MAX_WIDTH ] ?? 'false'
  82.                 ],
  83.             ];
  84.             $section->assign( [ 'sliderConfig' => $sliderConfig ] );
  85.         } );
  86.     }
  87.     private function processBlockMedia(CmsPageEntity $cmsPageSalesChannelContext $context): void
  88.     {
  89.         $contentTypes = [
  90.             'image-carousel-content' => 'carouselContents',
  91.         ];
  92.         foreach ($cmsPage->getSections()->getBlocks() as $value) {
  93.             if (!in_array($value->getType(), [
  94.                 'hero-tiles-column',
  95.                 'image-carousel',
  96.             ])) {
  97.                 continue;
  98.             }
  99.             foreach ($value->getSlots()->getElements() as $element) {
  100.                 if ($element->getType() === 'hero-tiles-media') {
  101.                     $this->processElementMedia($element$context->getContext());
  102.                 }
  103.                 if (array_key_exists($element->getType(), $contentTypes)) {
  104.                     $content $contentTypes[$element->getType()];
  105.                     $this->processContentMedia($element$content$context->getContext());
  106.                 }
  107.             }
  108.         }
  109.     }
  110.     private function processElementMedia(CmsSlotEntity $cmsSlotEntityContext $context)
  111.     {
  112.         $config $cmsSlotEntity->getConfig();
  113.         $ids = [];
  114.         foreach (['media''mobileMedia''mediaVideo''mediaImage'] as $mediaKey) {
  115.             $mediaId $this->getMediaIdFromConfig($config$mediaKey);
  116.             if (!is_null($mediaId)) {
  117.                 $ids[] = $mediaId;
  118.             }
  119.         }
  120.         $criteria = new Criteria($ids);
  121.         $media $this->mediaRepository
  122.             ->search($criteria$context)
  123.             ->getEntities();
  124.         if ($media->count() > 0) {
  125.             $cmsSlotEntity->setData($media);
  126.         }
  127.     }
  128.     private function processContentMedia(CmsSlotEntity $cmsSlotEntitystring $contentContext $context)
  129.     {
  130.         $config $cmsSlotEntity->getConfig();
  131.         $mediaIds array_column($config[$content]['value'], 'mediaId');
  132.         $criteria = new Criteria($mediaIds);
  133.         $media $this->mediaRepository
  134.             ->search($criteria$context)
  135.             ->getEntities();
  136.         if ($media->count() > 0) {
  137.             $cmsSlotEntity->setData($media);
  138.         }
  139.     }
  140.     private function getMediaIdFromConfig(array $configstring $key): ?string
  141.     {
  142.         return isset($config[$key]) && isset($config[$key]['value']) ? $config[$key]['value'] : null;
  143.     }
  144. }