vendor/crehler/mojebambino-engine/src/Subscriber/CategoryResultSubscriber.php line 25

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