0

我想Shopware\Core\Content\Category\Event\CategoryIndexerEvent.php在我的插件中使用。

有谁知道如何使用这个活动?

4

1 回答 1

1

您需要创建一个EventSubscriber。对于您提到的事件,这看起来像这样。

首先你需要一个事件订阅者:

<?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;

class MySubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            CategoryEvents::CATEGORY_INDEXER_EVENT => 'doMyStuff'
        ];
    }

    public function doMyStuff(CategoryIndexerEvent $event): void
    {
        $indexedCategories = $event->getIds();
        // Do stuff
    }
}

其次,您需要在 DIC (plugin/src/Resources/config/services.xml) 中注册此订阅者:

<!-- ... -->
<service id="MyPlugin\MySubscriber">
    <tag name="kernel.event_subscriber"/>
</service>
<!-- ... -->
于 2020-09-25T05:54:29.097 回答