How to programmatically get language info and salesChannel info for a category entity written event(subscriber) in Shopware 6?

Viewed 997

I have written Administration subscriber event on category which triggers every category written events and i would like to get translations data language(english/german) and sales channel information of current category.

Let's say system having two languages(English and German) and three sales Channels(us.shopware.com, ae.shopware.com and eu.shopware.com), and admin is making some changes in category data then, so onWrittenEvent I want to get two names(English and German) plus all other properties which are translatable such as description etc.. and also if current category (which is being written) is belongs to all three sales channels or single sales channel?

File: services.xml

<service id="Custom\SW6\Subscriber\CatSubscriber">
            <argument id="category.repository" type="service"/>
            <tag name="kernel.event_subscriber"/>
</service>

File: CatSubscriber.php

<?php

namespace Custom\SW6\Subscriber;

use Psr\Log\LoggerInterface;
use Shopware\Core\Content\Category\CategoryEvents;
use Shopware\Core\Content\Media\MediaEntity;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityDeletedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class CatSubscriber implements EventSubscriberInterface
{

    /**
     * @var EntityRepositoryInterface
     */
    private $categoryRepo;

    public function __construct(
        EntityRepositoryInterface $categoryRepo
     
    )
    {
        $this->categoryRepo = $categoryRepo;

    }

    public static function getSubscribedEvents(): array
    {
        return [
            CategoryEvents::CATEGORY_WRITTEN_EVENT => 'onCategoryWrite'
        ];
    }



    public function onCategoryWrite(EntityWrittenEvent $event): void
    {
            $writeResults = $event->getWriteResults();

            $catIds = array();
            foreach ($writeResults as $result) {
                $catIds = $result->getPrimaryKey();
            }
            $categoryCriteria = new Criteria([$catIds]);
            //$categoryCriteria->addAssociation('salesChannel.domains');
            $categories = $this->categoryRepo->search($categoryCriteria, $event->getContext())->getEntities();
            
            $data = array();
            foreach ($categories as $row) {
                $data[]['id'] = $row->getId();
                //$data[]['salesChannelId'] = $row->getSalesChannelId();
                //$data[]['langId'] = $row->getLangId();
            }
    }
    
}

I would like to fetch current category entity belongs to which language and which sales channel something like in $data object ?

0 Answers
Related