我正在开发一个Shopware 6插件来处理分页。在我的插件中,我有一个StorefrontRenderSubscriber
,它将一些数据传递给TWIG模板:
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Storefront\Event\StorefrontRenderEvent;
use Swag\SwagPagination\TComponents\SwagPaginationData;
use Swag\SwagPagination\Components\Helper\LoggerHelper;
use Swag\SwagPagination\Service\ConfigService;
class StorefrontRenderSubscriber implements EventSubscriberInterface {
protected const EXTENSION_NAME = "SwagPagination";
protected ConfigService $configService;
protected LoggerHelper $loggerHelper;
public function __construct(ConfigService $configService, LoggerHelper $loggerHelper) {
$this->configService = $configService;
$this->loggerHelper = $loggerHelper;
}
/**
* Get subscribed events for this subscriber.
*
* @return array The list of subscribed events and their associated methods
*/
public static function getSubscribedEvents(): array {
return [
StorefrontRenderEvent::class => 'onStorefrontRender'
];
}
/**
* Callback method for the storefront render event.
*
* @param StorefrontRenderEvent $event The event object
* @return void
*/
public function onStorefrontRender(StorefrontRenderEvent $event): void {
try {
// Retrieve the current request
$request = $event->getRequest();
// Retrieve all query parameters
$queryParameters = $request->query->all();
// Create an instance of SwagPaginationData
$pluginData = new SwagPaginationData();
// Get the configuration and check if it's active, add queryParameters
$config = $this->configService->getConfig();
if ($config && $config->isActive()) {
$pluginData->assign([
'active' => $config->isActive(),
'queryParameters' => $queryParameters,
/*
* add more fields here
*/
]);
}
// Add the extension to the context
$event->getContext()->addExtension(self::EXTENSION_NAME, $pluginData);
} catch (\Throwable $e) {
// Log any exceptions
$this->loggerHelper->logThrowable($e);
}
}
}
这个订阅者目前在每个页面上运行。然而,我从Shopware审查小组得到反馈,它应该被限制在只运行在需要分页的列表和搜索页面上。
我如何更新我的订阅者以限制其范围并仅在列表和搜索上运行?
1条答案
按热度按时间t3psigkw1#
插件数据可以通过调用相应的事件(如
ProductListingResultEvent
和ProductSearchResultEvent
)传递到产品列表和搜索页面。