symfony Shopware 6插件-如何将StorefrontRenderSubscriber范围限制为列表和搜索页面

knpiaxh1  于 7个月前  发布在  PWA
关注(0)|答案(1)|浏览(88)

我正在开发一个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审查小组得到反馈,它应该被限制在只运行在需要分页的列表和搜索页面上。
我如何更新我的订阅者以限制其范围并仅在列表和搜索上运行?

t3psigkw

t3psigkw1#

插件数据可以通过调用相应的事件(如ProductListingResultEventProductSearchResultEvent)传递到产品列表和搜索页面。

use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
use Shopware\Core\Content\Product\Events\ProductSearchResultEvent;

class ProductListingSearchSubscriber 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 [
             ProductListingResultEvent::class => 'onProductListingResult',
             ProductSearchResultEvent::class => 'onProductSearchResult',
        ];
    }
    
     /**
     * Callback method for the product listing result event.
     *
     * @param ProductListingResultEvent $event The event object
     * @return void
     */
    public function onProductListingResult(ProductListingResultEvent $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);            
        }
    }

    /**
     * Callback method for the product search result event.
     *
     * @param ProductSearchResultEvent $event The event object
     * @return void
     */
    public function onProductSearchResult(ProductSearchResultEvent $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);            
        }
    }
}

相关问题