vendor/crehler/mojebambino-variants-advanced/src/Subscriber/PropertyGroupResultSubscriber.php line 40

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  * @copyright 2020 Crehler Sp. z o. o. <https://crehler.com>
  4.  *
  5.  * @author    Jakub MedyƄski <jme@crehler.com>
  6.  * @support   Crehler <support@crehler.com>
  7.  * @created   31 mar 2020
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Crehler\VariantsAdvanced\Subscriber;
  13. use Crehler\MojeBambinoEngine\Service\CrehlerContextService;
  14. use Crehler\VariantsAdvanced\Util\Lifecycle\ActivateDeactivate;
  15. use Shopware\Core\Content\Property\PropertyEvents;
  16. use Shopware\Core\Content\Property\PropertyGroupEntity;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  18. use Shopware\Core\System\CustomField\CustomFieldTypes;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. class PropertyGroupResultSubscriber implements EventSubscriberInterface
  21. {
  22.     private CrehlerContextService $crehlerContextService;
  23.     
  24.     public function __construct(CrehlerContextService $crehlerContextService)
  25.     {
  26.         $this->crehlerContextService $crehlerContextService;
  27.     }
  28.     
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             PropertyEvents::PROPERTY_GROUP_LOADED_EVENT => 'onPropertyGroupLoaded'
  33.         ];
  34.     }
  35.     public function onPropertyGroupLoaded(EntityLoadedEvent $event): void
  36.     {
  37.         if($this->crehlerContextService->get(CrehlerContextService::PRODUCT_EXPORT_GENERATEfalse)) return;
  38.         /** @var PropertyGroupEntity $propertyGroup */
  39.         foreach ($event->getEntities() as $propertyGroup) {
  40.             $this->initializationCustomFields(ActivateDeactivate::PROPERTY_GROUP_CUSTOM_FIELDS$propertyGroup);
  41.         }
  42.     }
  43.     private function initializationCustomFields(array $customFieldsPropertyGroupEntity $propertyGroup): void
  44.     {
  45.         $prepareCustomFields = [];
  46.         foreach ($customFields as $customField) {
  47.             if ($propertyGroup->getCustomFields() !== null || isset($propertyGroup->getCustomFields()[$customField['name']])) {
  48.                 return;
  49.             }
  50.             if ($customField['type'] === CustomFieldTypes::BOOL) {
  51.                 $prepareCustomFields[$customField['name']] = false;
  52.             } else {
  53.                 $prepareCustomFields[$customField['name']] = null;
  54.             }
  55.         }
  56.         $propertyGroup->setCustomFields($prepareCustomFields);
  57.     }
  58. }