Shopware 6:如何通过 SalesChannel 级别的 EventSubscriberInterface 检测任何类别 add/update?
Shopware 6 : how to detect any category add/update through EventSubscriberInterface on SalesChannel level?
有谁知道如何检查特定类别是否在 Shopware 6 中added/updated/deleted?
我想通过 Subscriber 使用 Symfony\Component\EventDispatcher\EventSubscriberInterface
还是必须实现任何其他东西?
UPDATE:能够找到几个与实体相关的事件,但仍然无法区分(检测)类别是否正在添加或修改
plugin/src/Resources/config/services.xml
<!-- ... -->
<service id="MyPlugin\MySubscriber">
<tag name="kernel.event_subscriber"/>
</service>
<!-- ... -->
MySubscriber.php
<?php declare(strict_types=1);
namespace MyPlugin;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Content\Category\CategoryEvents;
use Shopware\Core\Content\Category\Event\CategoryIndexerEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityDeletedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
class MySubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
CategoryEvents::CATEGORY_INDEXER_EVENT => 'onCategoryIndex',
CategoryEvents::CATEGORY_DELETED_EVENT => 'onCategoryDelete',
CategoryEvents::CATEGORY_WRITTEN_EVENT => 'onCategoryWritten'
];
}
public function onCategoryWritten(EntityWrittenEvent $event): void
{
$ids = $event->getIds();
//how to check here whether category is adding or modified here or any other event.
//EntityWrittenEvent in both actions(add/modify) this listener is triggering
file_put_contents('/var/onCategoryWritten.text', print_r($ids, true), FILE_APPEND | LOCK_EX);
}
public function onCategoryDelete(EntityDeletedEvent $event): void
{
$ids = $event->getIds();
file_put_contents('/var/onCategoryDelete.text', print_r($ids, true), FILE_APPEND | LOCK_EX);
}
public function onCategoryIndex(CategoryIndexerEvent $event): void
{
$ids = $event->getIds();
file_put_contents('/var/onCategoryIndex.text', print_r($ids, true), FILE_APPEND | LOCK_EX);
}
}
您可以在写入结果中检查完成的操作,例如
foreach($event->getWriteResults() as $writeResult) {
if ($writeResult->getOperation() === EntityWriteResult::OPERATION_INSERT)
{
//entity created
}
if ($writeResult->getOperation() === EntityWriteResult::OPERATION_UPDATE)
{
//entity updated/modified
}
}
有谁知道如何检查特定类别是否在 Shopware 6 中added/updated/deleted?
我想通过 Subscriber 使用 Symfony\Component\EventDispatcher\EventSubscriberInterface
还是必须实现任何其他东西?
UPDATE:能够找到几个与实体相关的事件,但仍然无法区分(检测)类别是否正在添加或修改
plugin/src/Resources/config/services.xml
<!-- ... -->
<service id="MyPlugin\MySubscriber">
<tag name="kernel.event_subscriber"/>
</service>
<!-- ... -->
MySubscriber.php
<?php declare(strict_types=1);
namespace MyPlugin;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Content\Category\CategoryEvents;
use Shopware\Core\Content\Category\Event\CategoryIndexerEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityDeletedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
class MySubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
CategoryEvents::CATEGORY_INDEXER_EVENT => 'onCategoryIndex',
CategoryEvents::CATEGORY_DELETED_EVENT => 'onCategoryDelete',
CategoryEvents::CATEGORY_WRITTEN_EVENT => 'onCategoryWritten'
];
}
public function onCategoryWritten(EntityWrittenEvent $event): void
{
$ids = $event->getIds();
//how to check here whether category is adding or modified here or any other event.
//EntityWrittenEvent in both actions(add/modify) this listener is triggering
file_put_contents('/var/onCategoryWritten.text', print_r($ids, true), FILE_APPEND | LOCK_EX);
}
public function onCategoryDelete(EntityDeletedEvent $event): void
{
$ids = $event->getIds();
file_put_contents('/var/onCategoryDelete.text', print_r($ids, true), FILE_APPEND | LOCK_EX);
}
public function onCategoryIndex(CategoryIndexerEvent $event): void
{
$ids = $event->getIds();
file_put_contents('/var/onCategoryIndex.text', print_r($ids, true), FILE_APPEND | LOCK_EX);
}
}
您可以在写入结果中检查完成的操作,例如
foreach($event->getWriteResults() as $writeResult) {
if ($writeResult->getOperation() === EntityWriteResult::OPERATION_INSERT)
{
//entity created
}
if ($writeResult->getOperation() === EntityWriteResult::OPERATION_UPDATE)
{
//entity updated/modified
}
}