vendor/crehler/mojebambino-engine/src/Subscriber/PropertyGroupOptionResultSubscriber.php line 28

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Crehler\MojeBambinoEngine\Subscriber;
  3. use Crehler\Arrangements\CrehlerArrangements;
  4. use Crehler\MojeBambinoEngine\CrehlerMojeBambinoEngine;
  5. use Crehler\MojeBambinoEngine\Util\Lifecycle\ActivateDeactivate;
  6. use Shopware\Core\Content\Category\CategoryEntity;
  7. use Shopware\Core\Content\Category\CategoryEvents;
  8. use Shopware\Core\Content\Property\Aggregate\PropertyGroupOption\PropertyGroupOptionEntity;
  9. use Shopware\Core\Content\Property\PropertyEvents;
  10. use Shopware\Core\Framework\Context;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  12. use Shopware\Core\System\CustomField\CustomFieldTypes;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class PropertyGroupOptionResultSubscriber implements EventSubscriberInterface
  15. {
  16.     public static function getSubscribedEvents(): array
  17.     {
  18.         return [
  19.             PropertyEvents::PROPERTY_GROUP_OPTION_LOADED_EVENT => 'onPropertyGroupOptionLoaded'
  20.         ];
  21.     }
  22.     public function onPropertyGroupOptionLoaded(EntityLoadedEvent $event)
  23.     {
  24.         try {
  25.             if ($event->getContext()->getScope() !== Context::CRUD_API_SCOPE) {
  26.                 return;
  27.             }
  28.             /** @var PropertyGroupOptionEntity $category */
  29.             foreach ($event->getEntities() as $propertyGroupOption) {
  30.                 $this->initializationCustomFields(ActivateDeactivate::PROPERTY_GROUP_OPTION_FIELDS$propertyGroupOption);
  31.             }
  32.         } catch (\Throwable $exception) {
  33.             dd($exception);
  34.         }
  35.     }
  36.     private function initializationCustomFields(array $customFieldsPropertyGroupOptionEntity $propertyGroupOption): void
  37.     {
  38.         $prepareCustomFields = [];
  39.         if ($propertyGroupOption->getCustomFields() !== null
  40.             || ($propertyGroupOption->getCustomFields() !== null && isset($propertyGroupOption->getCustomFields()[$customField['name']]))) {
  41.             return;
  42.         }
  43.         foreach ($customFields as $customField) {
  44.             if ($customField['type'] === CustomFieldTypes::BOOL) {
  45.                 $prepareCustomFields[$customField['name']] = false;
  46.             } else {
  47.                 $prepareCustomFields[$customField['name']] = null;
  48.             }
  49.         }
  50.         $propertyGroupOption->setCustomFields($prepareCustomFields);
  51.     }
  52. }