custom/plugins/AcrisCmsPreConfigCS/src/Subscriber/CmsSlotWrittenSubscriber.php line 40

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\CmsPreConfig\Subscriber;
  3. use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotEntity;
  4. use Shopware\Core\Content\Cms\CmsPageEvents;
  5. use Shopware\Core\Content\Cms\DataResolver\FieldConfig;
  6. use Shopware\Core\Defaults;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class CmsSlotWrittenSubscriber implements EventSubscriberInterface
  12. {
  13.     const ACRIS_SLOT_TRANSLATION_KEY 'acrisTranslation';
  14.     // custom fields which should be merged for default languages
  15.     const DEFAULT_LANGUAGE_CUSTOM_FIELDS = [
  16.         'position',
  17.         'acrisCmsSlot'
  18.     ];
  19.     private EntityRepository $slotRepository;
  20.     public function __construct(
  21.         EntityRepository $slotRepository
  22.     )
  23.     {
  24.         $this->slotRepository $slotRepository;
  25.     }
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             CmsPageEvents::SLOT_WRITTEN_EVENT => 'onSlotWrittenEvent'
  30.         ];
  31.     }
  32.     public function onSlotWrittenEvent(EntityWrittenEvent $event): void
  33.     {
  34.         $results $event->getWriteResults();
  35.         foreach ($results as $result) {
  36.             $payload $result->getPayload();
  37.             if (!empty($payload) && array_key_exists('id'$payload) && !empty($payload['id'])) {
  38.                 $slotId $payload['id'];
  39.                 /** @var CmsSlotEntity $cmsSlot */
  40.                 $cmsSlot $this->slotRepository->search(new Criteria([$slotId]), $event->getContext())->first();
  41.                 if (empty($cmsSlot)) continue;
  42.                 $configArray = [];
  43.                 $translations = [];
  44.                 foreach ($cmsSlot->getFieldConfig()->getElements() as $configKey => $element) {
  45.                     if ($element instanceof FieldConfig) {
  46.                         if ($configKey === self::ACRIS_SLOT_TRANSLATION_KEY) {
  47.                             $translations $element->getArrayValue();
  48.                             foreach ($translations as $key => $translation) {
  49.                                 if (is_array($translation) && array_key_exists('languageId'$translation) && $translation['languageId'] === $event->getContext()->getLanguageId()) {
  50.                                     unset($translations[$key]);
  51.                                 } else {
  52.                                     // remove acrisTranslations to prevent problems on further usage
  53.                                     if(array_key_exists('config'$translation) && is_array($translation['config'])
  54.                                         && array_key_exists(self::ACRIS_SLOT_TRANSLATION_KEY$translation['config'])) {
  55.                                         unset($translations[$key]['config'][self::ACRIS_SLOT_TRANSLATION_KEY]);
  56.                                     }
  57.                                     // compatibility with AcrisCms slot position
  58.                                     if (is_array($translation) && array_key_exists('languageId'$translation) && $translation['languageId'] === Defaults::LANGUAGE_SYSTEM) {
  59.                                         $this->mergeDefaultCustomFields($cmsSlot$translations[$key]);
  60.                                     }
  61.                                 }
  62.                             }
  63.                         } else {
  64.                             $configArray[$configKey] = $element->getVars();
  65.                             if (array_key_exists('name'$configArray[$configKey])) {
  66.                                 unset($configArray[$configKey]['name']);
  67.                             }
  68.                             if (array_key_exists('extensions'$configArray[$configKey])) {
  69.                                 unset($configArray[$configKey]['extensions']);
  70.                             }
  71.                         }
  72.                     }
  73.                 }
  74.                 if (!empty($translations)) {
  75.                     $this->slotRepository->upsert([
  76.                         [
  77.                             'id' => $slotId,
  78.                             'config' => $configArray,
  79.                             'translations' => $translations
  80.                         ]
  81.                     ], $event->getContext());
  82.                 }
  83.             }
  84.         }
  85.     }
  86.     private function mergeDefaultCustomFields(CmsSlotEntity $cmsSlot, array &$translation): void
  87.     {
  88.         foreach (self::DEFAULT_LANGUAGE_CUSTOM_FIELDS as $customField) {
  89.             if(is_array($cmsSlot->getTranslation('customFields')) && array_key_exists($customField$cmsSlot->getTranslation('customFields'))) {
  90.                 if(!array_key_exists('customFields'$translation) || !is_array($translation['customFields'])) {
  91.                     $translation['customFields'] = [];
  92.                 }
  93.                 $translation['customFields'][$customField] = $cmsSlot->getTranslation('customFields')[$customField];
  94.             }
  95.         }
  96.     }
  97. }