<?php declare(strict_types=1);
namespace DreiscSeoPro\Subscriber\CategoryEvents;
use DreiscSeoPro\Core\Seo\LiveTemplate\LiveTemplateConverter;
use Shopware\Core\Content\Category\CategoryEntity;
use Shopware\Core\Content\Category\CategoryEvents;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CategoryLoadedSubscriber implements EventSubscriberInterface
{
/**
* @var LiveTemplateConverter
*/
private $liveTemplateConverter;
/**
* @param LiveTemplateConverter $liveTemplateConverter
*/
public function __construct(LiveTemplateConverter $liveTemplateConverter)
{
$this->liveTemplateConverter = $liveTemplateConverter;
}
/**
* @return array|void
*/
public static function getSubscribedEvents()
{
return [
CategoryEvents::CATEGORY_LOADED_EVENT => 'onCategoryLoaded'
];
}
/**
* @param EntityLoadedEvent $entityLoadedEvent
*/
public function onCategoryLoaded(EntityLoadedEvent $entityLoadedEvent): void
{
/** @var CategoryEntity $categoryEntity */
$categoryEntity = current($entityLoadedEvent->getEntities());
/** Abort, if empty */
if (false === $categoryEntity) {
return;
}
/** At this point the seo live template will be converted */
$this->liveTemplateConverter->translateCategoryEntity($categoryEntity, $entityLoadedEvent->getContext());
}
}