<?php declare(strict_types=1);
namespace Crehler\MojeBambinoEngine\Subscriber;
use Crehler\Arrangements\CrehlerArrangements;
use Crehler\MojeBambinoEngine\CrehlerMojeBambinoEngine;
use Crehler\MojeBambinoEngine\Util\Lifecycle\ActivateDeactivate;
use Shopware\Core\Content\Category\CategoryEntity;
use Shopware\Core\Content\Category\CategoryEvents;
use Shopware\Core\Content\Property\Aggregate\PropertyGroupOption\PropertyGroupOptionEntity;
use Shopware\Core\Content\Property\PropertyEvents;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Shopware\Core\System\CustomField\CustomFieldTypes;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class PropertyGroupOptionResultSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
PropertyEvents::PROPERTY_GROUP_OPTION_LOADED_EVENT => 'onPropertyGroupOptionLoaded'
];
}
public function onPropertyGroupOptionLoaded(EntityLoadedEvent $event)
{
try {
if ($event->getContext()->getScope() !== Context::CRUD_API_SCOPE) {
return;
}
/** @var PropertyGroupOptionEntity $category */
foreach ($event->getEntities() as $propertyGroupOption) {
$this->initializationCustomFields(ActivateDeactivate::PROPERTY_GROUP_OPTION_FIELDS, $propertyGroupOption);
}
} catch (\Throwable $exception) {
dd($exception);
}
}
private function initializationCustomFields(array $customFields, PropertyGroupOptionEntity $propertyGroupOption): void
{
$prepareCustomFields = [];
if ($propertyGroupOption->getCustomFields() !== null
|| ($propertyGroupOption->getCustomFields() !== null && isset($propertyGroupOption->getCustomFields()[$customField['name']]))) {
return;
}
foreach ($customFields as $customField) {
if ($customField['type'] === CustomFieldTypes::BOOL) {
$prepareCustomFields[$customField['name']] = false;
} else {
$prepareCustomFields[$customField['name']] = null;
}
}
$propertyGroupOption->setCustomFields($prepareCustomFields);
}
}