vendor/crehler/arrangements/src/Subscriber/CategoryResultSubscriber.php line 34

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   14 sie 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\Arrangements\Subscriber;
  13. use Crehler\Arrangements\CrehlerArrangements;
  14. use Shopware\Core\Content\Category\CategoryEntity;
  15. use Shopware\Core\Content\Category\CategoryEvents;
  16. use Shopware\Core\Framework\Context;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  18. use Shopware\Core\System\CustomField\CustomFieldTypes;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. class CategoryResultSubscriber implements EventSubscriberInterface
  21. {
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             CategoryEvents::CATEGORY_LOADED_EVENT => 'onCategoryResult'
  26.         ];
  27.     }
  28.     public function onCategoryResult(EntityLoadedEvent $event)
  29.     {
  30.         if ($event->getContext()->getScope() !== Context::CRUD_API_SCOPE) {
  31.             return;
  32.         }
  33.         /** @var CategoryEntity $category */
  34.         foreach ($event->getEntities() as $category) {
  35.             $this->initializationCustomFields(CrehlerArrangements::CATEGORY_CUSTOM_FIELDS$category);
  36.         }
  37.     }
  38.     private function initializationCustomFields(array $customFieldCategoryEntity $category): void
  39.     {
  40.         $prepareCustomFields = [];
  41.         if ($category->getCustomFields() !== null || isset($category->getCustomFields()[$customField['name']])) {
  42.             return;
  43.         }
  44.         if ($customField['type'] === CustomFieldTypes::BOOL) {
  45.             $prepareCustomFields[$customField['name']] = false;
  46.         } else {
  47.             $prepareCustomFields[$customField['name']] = null;
  48.         }
  49.         $category->setCustomFields($prepareCustomFields);
  50.     }
  51. }