From 53871238940a173c8ba015236c41ce747ac7eb8c Mon Sep 17 00:00:00 2001 From: krystian15 <31726767+krystian15@users.noreply.github.com> Date: Wed, 11 Dec 2019 09:22:22 +0100 Subject: [PATCH 1/2] Update Page.php Respect store id --- src/Model/Resolver/Page.php | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/src/Model/Resolver/Page.php b/src/Model/Resolver/Page.php index a5357c4..4951ae6 100644 --- a/src/Model/Resolver/Page.php +++ b/src/Model/Resolver/Page.php @@ -2,14 +2,12 @@ /** * ScandiPWA_CmsGraphQl * - * @category Scandiweb - * @package ScandiPWA_CmsGraphQl - * @author Kriss Andrejevs - * @copyright Copyright (c) 2018 Scandiweb, Ltd (https://scandiweb.com) + * @category Scandiweb + * @package ScandiPWA_CmsGraphQl + * @author Kriss Andrejevs + * @copyright Copyright (c) 2018 Scandiweb, Ltd (https://scandiweb.com) */ - namespace ScandiPWA\CmsGraphQl\Model\Resolver; - use Magento\CmsGraphQl\Model\Resolver\DataProvider\Page as PageDataProvider; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\GraphQl\Config\Element\Field; @@ -19,29 +17,29 @@ use Magento\Framework\GraphQl\Query\Resolver\ValueFactory; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; use Magento\Cms\Model\PageFactory; - +use Magento\Store\Model\StoreManagerInterface; /** * Class Page * @package ScandiPWA\CmsGraphQl\Model\Resolver */ class Page extends \Magento\CmsGraphQl\Model\Resolver\Page { - /** * @var PageDataProvider */ private $pageDataProvider; - /** * @var ValueFactory */ private $valueFactory; - /** * @var PageFactory */ private $pageFactory; - + /** + * @var StoreManagerInterface + */ + private $storeManager; /** * @param PageDataProvider $pageDataProvider * @param ValueFactory $valueFactory @@ -49,7 +47,8 @@ class Page extends \Magento\CmsGraphQl\Model\Resolver\Page public function __construct( PageDataProvider $pageDataProvider, ValueFactory $valueFactory, - PageFactory $pageFactory + PageFactory $pageFactory, + StoreManagerInterface $storeManager ) { parent::__construct( $pageDataProvider @@ -57,8 +56,8 @@ public function __construct( $this->pageDataProvider = $pageDataProvider; $this->valueFactory = $valueFactory; $this->pageFactory = $pageFactory; + $this->storeManager = $storeManager; } - /** * @inheritdoc */ @@ -69,16 +68,13 @@ public function resolve( array $value = null, array $args = null ) : Value { - $result = function () use ($args) { $pageId = $this->getPageId($args); $pageData = $this->getPageData($pageId); - return $pageData; }; return $this->valueFactory->create($result); } - /** * @param array $args * @return int @@ -86,18 +82,15 @@ public function resolve( */ private function getPageId(array $args): int { + $storeId = $this->storeManager->getStore()->getId(); if (isset($args['id'])) { return (int)$args['id']; } - if (isset($args['url_key'])) { - return (int)$this->pageFactory->create()->load($args['url_key'])->getId(); + return (int)$this->pageFactory->create()->setStoreId($storeId)->load($args['url_key'])->setStoreId($storeId)->getId(); } - throw new GraphQlInputException(__('Page id should be specified')); - } - /** * @param int $pageId * @return array @@ -112,5 +105,4 @@ private function getPageData(int $pageId): array } return $pageData; } - } From 16b1ef840b4d3f75246108beb73f1c33a487a7da Mon Sep 17 00:00:00 2001 From: krystian15 <31726767+krystian15@users.noreply.github.com> Date: Wed, 11 Dec 2019 09:36:32 +0100 Subject: [PATCH 2/2] Update Page.php --- src/Model/Resolver/Page.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Model/Resolver/Page.php b/src/Model/Resolver/Page.php index 4951ae6..02bb854 100644 --- a/src/Model/Resolver/Page.php +++ b/src/Model/Resolver/Page.php @@ -7,7 +7,9 @@ * @author Kriss Andrejevs * @copyright Copyright (c) 2018 Scandiweb, Ltd (https://scandiweb.com) */ + namespace ScandiPWA\CmsGraphQl\Model\Resolver; + use Magento\CmsGraphQl\Model\Resolver\DataProvider\Page as PageDataProvider; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\GraphQl\Config\Element\Field; @@ -18,6 +20,7 @@ use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; use Magento\Cms\Model\PageFactory; use Magento\Store\Model\StoreManagerInterface; + /** * Class Page * @package ScandiPWA\CmsGraphQl\Model\Resolver @@ -40,6 +43,7 @@ class Page extends \Magento\CmsGraphQl\Model\Resolver\Page * @var StoreManagerInterface */ private $storeManager; + /** * @param PageDataProvider $pageDataProvider * @param ValueFactory $valueFactory @@ -58,6 +62,7 @@ public function __construct( $this->pageFactory = $pageFactory; $this->storeManager = $storeManager; } + /** * @inheritdoc */ @@ -67,14 +72,17 @@ public function resolve( ResolveInfo $info, array $value = null, array $args = null - ) : Value { + ): Value { $result = function () use ($args) { $pageId = $this->getPageId($args); $pageData = $this->getPageData($pageId); + return $pageData; }; + return $this->valueFactory->create($result); } + /** * @param array $args * @return int @@ -87,10 +95,15 @@ private function getPageId(array $args): int return (int)$args['id']; } if (isset($args['url_key'])) { - return (int)$this->pageFactory->create()->setStoreId($storeId)->load($args['url_key'])->setStoreId($storeId)->getId(); + return (int)$this->pageFactory->create() + ->setStoreId($storeId) + ->load($args['url_key']) + ->setStoreId($storeId) + ->getId(); } throw new GraphQlInputException(__('Page id should be specified')); } + /** * @param int $pageId * @return array @@ -103,6 +116,7 @@ private function getPageData(int $pageId): array } catch (NoSuchEntityException $e) { throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e); } + return $pageData; } }