custom/plugins/ThemeOkeOnline/src/Core/Framework/DatabaseAbstractionLayer/EntityLoadedEventSubscriber.php line 33

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace ThemeOkeOnline\Core\Framework\DatabaseAbstractionLayer;
  3. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityIdSearchResultLoadedEvent;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntitySearchResultLoadedEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. class EntityLoadedEventSubscriber implements EventSubscriberInterface
  8. {
  9.     const DEFAULT_HIGH_PRIORITY 1000;
  10.     public static function getSubscribedEvents(): array
  11.     {
  12.         $return = [];
  13.         /**** OkeonlineProjects ****/
  14.         if(class_exists('Okeonline\Projects\Content\Project\ProjectDefinition'))
  15.             $return array_merge($return, ['oo_project_category.loaded' => ['addCustomFieldsStructOnEntityLoaded'self::DEFAULT_HIGH_PRIORITY]]);
  16.         if(class_exists('Okeonline\Projects\Content\ProjectCategory\ProjectCategoryDefinition'))
  17.             $return array_merge($return, ['oo_project_category.loaded' => ['addCustomFieldsStructOnEntityLoaded'self::DEFAULT_HIGH_PRIORITY]]);
  18.         /**** Shopware Defaults ****/
  19.         return array_merge($return, [
  20.             'category.loaded' => ['addCustomFieldsStructOnEntityLoaded'self::DEFAULT_HIGH_PRIORITY],
  21.             'product.loaded' => ['addCustomFieldsStructOnEntityLoaded'self::DEFAULT_HIGH_PRIORITY],
  22.         ]);
  23.     }
  24.     public function addCustomFieldsStructOnEntityLoaded(EntityLoadedEvent $event)
  25.     {
  26.         // if no array, return directly
  27.         if( ! is_array($event->getEntities()))
  28.             return;
  29.         // loop trought array
  30.         foreach($event->getEntities() as $entity)
  31.         {
  32.             // if there are no custom fields, continue;
  33.             if( ! $this->customFieldsExists($entity))
  34.                 continue;
  35.             // if there is already a cf-struct set, continue;
  36.             if($entity->hasExtension('cf'))
  37.                 continue;
  38.             $customFields $entity->getCustomFields();
  39.             // add cf-struct, even if customFields is null so it is always available (see struct for details)
  40.             $entity->cf = new CustomFieldsStruct($customFields);
  41.         }
  42.     }
  43.     private function customFieldsExists($entity): bool
  44.     {
  45.         return method_exists($entity'getCustomFields');
  46.     }
  47. }