custom/plugins/ThemeOkeOnline/src/Core/Content/Category/Subscriber/NewCategorySubscriber.php line 36

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace ThemeOkeOnline\Core\Content\Category\Subscriber;
  3. use Nette\Utils\DateTime;
  4. use Shopware\Core\Content\Category\CategoryEntity;
  5. use Shopware\Core\Content\Category\CategoryEvents;
  6. use Shopware\Core\Defaults;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Shopware\Core\Content\Product\ProductEvents;
  14. class NewCategorySubscriber implements EventSubscriberInterface
  15. {
  16.     private $categoryRepository;
  17.     public function __construct(
  18.         EntityRepository $categoryRepository
  19.     )
  20.     {
  21.         $this->categoryRepository $categoryRepository;
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             CategoryEvents::CATEGORY_WRITTEN_EVENT => 'onCategoryWritten'
  27.         ];
  28.     }
  29.     public function onCategoryWritten(EntityWrittenEvent $event)
  30.     {
  31.         $results $event->getWriteResults();
  32.         /** @var EntityWriteResult $result */
  33.         foreach ($results as $result)
  34.         {
  35.             if($result->getOperation() == EntityWriteResult::OPERATION_INSERT) {
  36.                 $categoryId $result->getPrimaryKey();
  37.                 /** @var CategoryEntity $currentCategory */
  38.                 $currentCategory $this->categoryRepository->search(new Criteria([$categoryId]), $event->getContext());
  39.                 if (!$currentCategory->hasExtension('info')) {
  40.                     $this->categoryRepository->update(
  41.                         [
  42.                             [
  43.                                 'id' => $categoryId,
  44.                                 'info' => ['intro' => '']
  45.                             ]
  46.                         ], $event->getContext());
  47.                 }
  48.             }
  49.         }
  50.     }
  51. }