From 0b2c088e0c45baec4dd3c85557f88d580768ed67 Mon Sep 17 00:00:00 2001 From: Mate Sitchinava Date: Mon, 23 Jan 2023 13:50:55 +0400 Subject: [PATCH] added store configs to action --- src/Controller/Pwa.php | 14 +++++++------- src/Controller/Router.php | 33 ++++++++++++++++++++++++++------- src/View/Result/Page.php | 18 +++++++++--------- 3 files changed, 42 insertions(+), 23 deletions(-) diff --git a/src/Controller/Pwa.php b/src/Controller/Pwa.php index 61cef88..d134cbf 100644 --- a/src/Controller/Pwa.php +++ b/src/Controller/Pwa.php @@ -76,9 +76,9 @@ class Pwa extends Action implements HttpGetActionInterface, HttpPostActionInterf protected $catalogDefaultSortBy; /** - * @var string + * @var array|null */ - protected $catalogDefaultSortByConfig; + protected $storeConfig; /** * @param string $type @@ -182,12 +182,12 @@ public function setCatalogDefaultSortBy(string $catalogDefaultSortBy): self } /** - * @param string $catalogDefaultSortByConfig + * @param $storeConfig * @return Pwa */ - public function setCatalogDefaultSortByConfig(string $catalogDefaultSortByConfig): self + public function setStoreConfig($storeConfig): self { - $this->catalogDefaultSortByConfig = $catalogDefaultSortByConfig; + $this->storeConfig = $storeConfig; return $this; } @@ -210,7 +210,7 @@ public function __construct( $this->identifier = ''; $this->description = ''; $this->catalogDefaultSortBy = ''; - $this->catalogDefaultSortByConfig = ''; + $this->storeConfig = null; parent::__construct($context); } @@ -232,7 +232,7 @@ public function execute() $resultLayout->setDisplayMode($this->display_mode); $resultLayout->setDescription($this->description); $resultLayout->setCatalogDefaultSortBy($this->catalogDefaultSortBy); - $resultLayout->setCatalogDefaultSortByConfig($this->catalogDefaultSortByConfig); + $resultLayout->setStoreConfig($this->storeConfig); try{ $templateName = 'pwa-root'; $resultLayout->setRootTemplate($templateName); diff --git a/src/Controller/Router.php b/src/Controller/Router.php index 3c62318..f378ef2 100644 --- a/src/Controller/Router.php +++ b/src/Controller/Router.php @@ -34,6 +34,7 @@ use Magento\Catalog\Model\ProductRepository; use Magento\Catalog\Api\CategoryRepositoryInterface; use Magento\Catalog\Model\Product\Attribute\Source\Status; + class Router extends BaseRouter { const XML_PATH_CMS_HOME_PAGE = 'web/default/cms_home_page'; @@ -44,6 +45,11 @@ class Router extends BaseRouter const PAGE_TYPE_CATEGORY = 'CATEGORY'; const PAGE_TYPE_CMS_PAGE = 'CMS_PAGE'; + const CRUCIAL_STORE_CONFIG_VALUES = [ + 'cms_home_page' => self::XML_PATH_CMS_HOME_PAGE, + 'catalog_default_sort_by' => self::XML_PATH_CATALOG_DEFAULT_SORT_BY + ]; + /** * @var ValidationManagerInterface */ @@ -208,13 +214,6 @@ public function match(RequestInterface $request) $action = $this->actionFactory->create(Pwa::class); $rewrite = $this->getRewrite($request); - $catalogDefaultSortByConfig = $this->scopeConfig->getValue( - self::XML_PATH_CATALOG_DEFAULT_SORT_BY, - ScopeInterface::SCOPE_STORE, - $this->storeId - ); - - $action->setCatalogDefaultSortByConfig($catalogDefaultSortByConfig); if ($rewrite) { // Do not execute any action for external rewrites, @@ -266,6 +265,8 @@ protected function setPageDetails(UrlRewrite $urlRewrite, ActionInterface $actio $this->setResponseCategory($entityId, $action); break; } + + $this->setStoreConfigs($action); } protected function setResponseHomePage(ActionInterface $action) @@ -357,6 +358,24 @@ protected function setResponseCategory($id, ActionInterface $action) } } + protected function setStoreConfigs(ActionInterface $action) + { + $storeConfig = []; + $crucialStoreConfigs = self::CRUCIAL_STORE_CONFIG_VALUES; + + foreach ($crucialStoreConfigs as $configKey => $path) { + $configValue = $this->scopeConfig->getValue( + $path, + ScopeInterface::SCOPE_STORE, + $this->storeId + ); + + $storeConfig[$configKey] = $configValue; + } + + $action->setStoreConfig($storeConfig); + } + /** * Set "404 Not Found" response * diff --git a/src/View/Result/Page.php b/src/View/Result/Page.php index 9c5b1e4..e5d7871 100644 --- a/src/View/Result/Page.php +++ b/src/View/Result/Page.php @@ -127,10 +127,10 @@ class Page extends ExtendedPage */ protected $catalogDefaultSortBy; - /** - * @var string + /** + * @var array|null */ - protected $catalogDefaultSortByConfig; + protected $storeConfig; /** * Constructor @@ -188,7 +188,7 @@ public function __construct( $this->identifier = ''; $this->description = ''; $this->catalogDefaultSortBy = ''; - $this->catalogDefaultSortByConfig = ''; + $this->storeConfig = null; } /** @@ -322,19 +322,19 @@ public function getCatalogDefaultSortBy() return $this->catalogDefaultSortBy; } - public function setCatalogDefaultSortByConfig(string $catalogDefaultSortByConfig) + public function setStoreConfig($storeConfig) { - if($this->catalogDefaultSortByConfig === '') { - $this->catalogDefaultSortByConfig = $catalogDefaultSortByConfig; + if($this->storeConfig === null) { + $this->storeConfig = $storeConfig; return $this; } - return ''; + return null; } public function getCatalogDefaultSortByConfig() { - return $this->catalogDefaultSortByConfig; + return $this->storeConfig; } /**