Skip to content

Commit

Permalink
Merge pull request #28 from matesich/add-cms-to-action
Browse files Browse the repository at this point in the history
 add cms page to action
  • Loading branch information
AleksandrsKondratjevs authored Feb 2, 2023
2 parents 80a29c4 + 47df16f commit a8db01f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 27 deletions.
14 changes: 7 additions & 7 deletions src/Controller/Pwa.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ class Pwa extends Action implements HttpGetActionInterface, HttpPostActionInterf
protected $display_mode;

/**
* @var string
* @var array|null
*/
protected $identifier;
protected $cmsPage;

/**
* @var string
Expand Down Expand Up @@ -147,12 +147,12 @@ public function setDisplayMode(string $display_mode): self
}

/**
* @param string $identifier
* @param $cmsPage
* @return Pwa
*/
public function setIdentifier(string $identifier): self
public function setCmsPage($cmsPage): self
{
$this->identifier = $identifier;
$this->cmsPage = $cmsPage;
return $this;
}

Expand Down Expand Up @@ -192,7 +192,7 @@ public function __construct(
$this->sku = '';
$this->name = '';
$this->display_mode = '';
$this->identifier = '';
$this->cmsPage = null;
$this->description = '';
$this->catalogDefaultSortBy = '';
parent::__construct($context);
Expand All @@ -212,7 +212,7 @@ public function execute()
$resultLayout->setId($this->id);
$resultLayout->setSku($this->sku);
$resultLayout->setName($this->name);
$resultLayout->setIdentifier($this->identifier);
$resultLayout->setCmsPage($this->cmsPage);
$resultLayout->setDisplayMode($this->display_mode);
$resultLayout->setDescription($this->description);
$resultLayout->setCatalogDefaultSortBy($this->catalogDefaultSortBy);
Expand Down
34 changes: 24 additions & 10 deletions src/Controller/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
use Magento\Catalog\Model\ProductRepository;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use ScandiPWA\CmsGraphQl\Model\Resolver\DataProvider\Page as PageProvider;

class Router extends BaseRouter
{
const XML_PATH_CMS_HOME_PAGE = 'web/default/cms_home_page';
Expand Down Expand Up @@ -99,6 +101,11 @@ class Router extends BaseRouter
*/
protected $productRepository;

/**
* @var PageProvider
*/
protected PageProvider $pageProvider;

/**
* Router constructor.
* @param ActionList $actionList
Expand All @@ -117,6 +124,7 @@ class Router extends BaseRouter
* @param PageFactory $pageFactory
* @param ProductRepository $productRepository
* @param CategoryRepositoryInterface $categoryRepository
* @param PageProvider $pageProvider
*/
public function __construct(
ActionList $actionList,
Expand All @@ -135,6 +143,7 @@ public function __construct(
PageFactory $pageFactory,
ProductRepository $productRepository,
CategoryRepositoryInterface $categoryRepository,
PageProvider $pageProvider,
array $ignoredURLs = []
) {
$this->scopeConfig = $scopeConfig;
Expand All @@ -145,6 +154,7 @@ public function __construct(
$this->pageFactory = $pageFactory;
$this->productRepository = $productRepository;
$this->categoryRepository = $categoryRepository;
$this->pageProvider = $pageProvider;
$this->ignoredURLs = $ignoredURLs;
$this->storeId = $this->storeManager->getStore()->getId();

Expand Down Expand Up @@ -276,8 +286,15 @@ protected function setResponseHomePage(ActionInterface $action)
$this->storeId
);

$action->setType(self::PAGE_TYPE_CMS_PAGE);
$action->setIdentifier($homePageIdentifier ?? '');
try {
$page = $this->pageProvider->getDataByPageIdentifier($homePageIdentifier, (int) $this->storeId);

$action->setType(self::PAGE_TYPE_CMS_PAGE);
$action->setId($page['page_id'] ?? '');
$action->setCmsPage($page);
} catch (\Throwable $th) {
$action->setType('PWA_ROUTER');
}
}

/**
Expand All @@ -290,17 +307,14 @@ protected function setResponseHomePage(ActionInterface $action)
*/
protected function setResponseCmsPage($id, ActionInterface $action)
{
$page = $this->pageFactory->create()
->setStoreId($this->storeId)
->load($id);
try {
$page = $this->pageProvider->getDataByPageId((int) $id, (int) $this->storeId);

if (!$page->getId() || !$page->isActive()) {
$action->setId($page['page_id'] ?? '');
$action->setCmsPage($page);
} catch (\Throwable $th) {
$this->setNotFound($action);
return;
}

$action->setId($page->getId() ?? '');
$action->setIdentifier($page->getIdentifier() ?? '');
}

/**
Expand Down
19 changes: 9 additions & 10 deletions src/View/Result/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ class Page extends ExtendedPage
protected $display_mode;

/**
* @var string
* @var array|null
*/
protected $identifier;
protected $cmsPage;

/**
* @var string
Expand Down Expand Up @@ -180,7 +180,7 @@ public function __construct(
$this->sku = '';
$this->name = '';
$this->display_mode = '';
$this->identifier = '';
$this->cmsPage = null;
$this->description = '';
$this->catalogDefaultSortBy = '';
}
Expand Down Expand Up @@ -271,19 +271,19 @@ public function getDisplayMode()
return $this->display_mode;
}

public function setIdentifier(string $identifier)
public function setCmsPage($cmsPage)
{
if($this->identifier === '') {
$this->identifier = $identifier;
if($this->cmsPage === null) {
$this->cmsPage = $cmsPage;
return $this;
}

return '';
return null;
}

public function getIdentifier()
public function getCmsPage()
{
return $this->identifier;
return $this->cmsPage;
}

public function setDescription(string $description)
Expand Down Expand Up @@ -330,5 +330,4 @@ public function setRootTemplate($template)
}
return $this;
}

}

0 comments on commit a8db01f

Please sign in to comment.