From a955bfe84427eb8386db0c697ebefb020b172902 Mon Sep 17 00:00:00 2001 From: Jose Ortega Date: Tue, 10 Jul 2018 11:31:22 +0200 Subject: [PATCH 1/2] Added 2 new endpoints which retrieve a page/block by their identifier and store id --- Api/BlockManagerInterface.php | 7 ++++++ Api/PageManagerInterface.php | 7 ++++++ Model/BlockManager.php | 41 ++++++++++++++++++++++++++++++++++- Model/PageManager.php | 41 ++++++++++++++++++++++++++++++++++- README.md | 8 ++++--- etc/webapi.xml | 12 ++++++++++ 6 files changed, 111 insertions(+), 5 deletions(-) diff --git a/Api/BlockManagerInterface.php b/Api/BlockManagerInterface.php index 11103c2..765b613 100644 --- a/Api/BlockManagerInterface.php +++ b/Api/BlockManagerInterface.php @@ -10,6 +10,13 @@ interface BlockManagerInterface */ public function getById($blockId); + /** + * @param string $identifier + * @param int $storeId + * @return \Magento\Cms\Api\Data\BlockInterface + */ + public function getByIdentifier($identifier, $storeId = null); + /** * @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria * @return \Magento\Cms\Api\Data\BlockSearchResultsInterface diff --git a/Api/PageManagerInterface.php b/Api/PageManagerInterface.php index 8dc60a8..6ce82de 100644 --- a/Api/PageManagerInterface.php +++ b/Api/PageManagerInterface.php @@ -10,6 +10,13 @@ interface PageManagerInterface */ public function getById($pageId); + /** + * @param string $identifier + * @param int $storeId + * @return \Magento\Cms\Api\Data\PageInterface + */ + public function getByIdentifier($identifier, $storeId = null); + /** * @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria * @return \Magento\Cms\Api\Data\PageSearchResultsInterface diff --git a/Model/BlockManager.php b/Model/BlockManager.php index b8df928..e093960 100644 --- a/Model/BlockManager.php +++ b/Model/BlockManager.php @@ -3,7 +3,11 @@ namespace Snowdog\CmsApi\Model; use Magento\Cms\Api\BlockRepositoryInterface; +use Magento\Cms\Api\Data\BlockInterface; +use Magento\Cms\Model\BlockFactory; +use Magento\Cms\Model\ResourceModel\Block; use Magento\Cms\Model\Template\FilterProvider; +use Magento\Framework\Exception\NoSuchEntityException; use Snowdog\CmsApi\Api\BlockManagerInterface; class BlockManager implements BlockManagerInterface @@ -18,12 +22,26 @@ class BlockManager implements BlockManagerInterface */ private $filterProvider; + /** + * @var BlockFactory + */ + private $blockFactory; + + /** + * @var Block + */ + private $blockResource; + public function __construct( BlockRepositoryInterface $blockRepository, - FilterProvider $filterProvider + FilterProvider $filterProvider, + BlockFactory $blockFactory, + Block $blockResource ) { $this->blockRepository = $blockRepository; $this->filterProvider = $filterProvider; + $this->blockFactory = $blockFactory; + $this->blockResource = $blockResource; } /** @@ -38,6 +56,27 @@ public function getById($blockId) return $block; } + /** + * @inheritdoc + */ + public function getByIdentifier($identifier, $storeId = null) + { + $block = $this->blockFactory->create(); + $block->setStoreId($storeId); + $this->blockResource->load($block, $identifier, BlockInterface::IDENTIFIER); + + if (!$block->getId()) { + throw new NoSuchEntityException( + __('CMS Block with identifier "%1" does not exist.', $identifier) + ); + } + + $content = $this->getBlockContentFiltered($block->getContent()); + $block->setContent($content); + + return $block; + } + /** * @inheritdoc */ diff --git a/Model/PageManager.php b/Model/PageManager.php index 9507b61..6c71873 100644 --- a/Model/PageManager.php +++ b/Model/PageManager.php @@ -2,8 +2,12 @@ namespace Snowdog\CmsApi\Model; +use Magento\Cms\Api\Data\PageInterface; use Magento\Cms\Api\PageRepositoryInterface; +use Magento\Cms\Model\PageFactory; +use Magento\Cms\Model\ResourceModel\Page; use Magento\Cms\Model\Template\FilterProvider; +use Magento\Framework\Exception\NoSuchEntityException; use Snowdog\CmsApi\Api\PageManagerInterface; class PageManager implements PageManagerInterface @@ -18,12 +22,26 @@ class PageManager implements PageManagerInterface */ private $filterProvider; + /** + * @var PageFactory + */ + private $pageFactory; + + /** + * @var Page + */ + private $pageResource; + public function __construct( PageRepositoryInterface $pageRepository, - FilterProvider $filterProvider + FilterProvider $filterProvider, + PageFactory $pageFactory, + Page $pageResource ) { $this->pageRepository = $pageRepository; $this->filterProvider = $filterProvider; + $this->pageFactory = $pageFactory; + $this->pageResource = $pageResource; } /** @@ -38,6 +56,27 @@ public function getById($pageId) return $page; } + /** + * @inheritdoc + */ + public function getByIdentifier($identifier, $storeId = null) + { + $page = $this->pageFactory->create(); + $page->setStoreId($storeId); + $this->pageResource->load($page, $identifier, PageInterface::IDENTIFIER); + + if (!$page->getId()) { + throw new NoSuchEntityException( + __('CMS Page with identifier "%1" does not exist.', $identifier) + ); + } + + $content = $this->getPageContentFiltered($page->getContent()); + $page->setContent($content); + + return $page; + } + /** * @inheritdoc */ diff --git a/README.md b/README.md index 341d3a5..43efac0 100644 --- a/README.md +++ b/README.md @@ -4,13 +4,15 @@ The module provides endpoints to get CMS blocks and pages filtered. ### 1. Installation: -`composer require snowdog/module-cms-api` -`bin/magento module:enable Snowdog_CmsApi` -`bin/magento setup:upgrade` +* `composer require snowdog/module-cms-api` +* `bin/magento module:enable Snowdog_CmsApi` +* `bin/magento setup:upgrade` ### 2. Available endpoints: * `/rest/V1/snowdog/cmsPage/:pageId`: retrieves page info by its id (integer value) * `/rest/V1/snowdog/cmsPage/search`: retrieves the list of pages (accepts search criteria filters) +* `/rest/V1/snowdog/cmsPageIdentifier/:identifier/storeId/:storeId`: retrieves page info by its identifier (string value) and store id (integer value) * `/rest/V1/snowdog/cmsBlock/:blockId`: retrieves block info by its id (integer value) +* `/rest/V1/snowdog/cmsBlockIdentifier/:identifier/storeId/:storeId`: retrieves block info by its identifier (string value) and store id (integer value) * `/rest/V1/snowdog/cmsBlock/search`: retrieves the list of blocks (accepts search criteria filters) diff --git a/etc/webapi.xml b/etc/webapi.xml index 24fd313..0bdff9c 100644 --- a/etc/webapi.xml +++ b/etc/webapi.xml @@ -7,6 +7,12 @@ + + + + + + @@ -20,6 +26,12 @@ + + + + + + From 130dc9c421e9b3ef436bb0f9b9de97bcf4c4d8c6 Mon Sep 17 00:00:00 2001 From: Jose Ortega Date: Thu, 12 Jul 2018 09:02:40 +0200 Subject: [PATCH 2/2] #41856 Added throw annotations --- Api/BlockManagerInterface.php | 1 + Api/PageManagerInterface.php | 1 + 2 files changed, 2 insertions(+) diff --git a/Api/BlockManagerInterface.php b/Api/BlockManagerInterface.php index 765b613..0e26c5d 100644 --- a/Api/BlockManagerInterface.php +++ b/Api/BlockManagerInterface.php @@ -14,6 +14,7 @@ public function getById($blockId); * @param string $identifier * @param int $storeId * @return \Magento\Cms\Api\Data\BlockInterface + * @throws \Magento\Framework\Exception\NoSuchEntityException */ public function getByIdentifier($identifier, $storeId = null); diff --git a/Api/PageManagerInterface.php b/Api/PageManagerInterface.php index 6ce82de..8a5161b 100644 --- a/Api/PageManagerInterface.php +++ b/Api/PageManagerInterface.php @@ -14,6 +14,7 @@ public function getById($pageId); * @param string $identifier * @param int $storeId * @return \Magento\Cms\Api\Data\PageInterface + * @throws \Magento\Framework\Exception\NoSuchEntityException */ public function getByIdentifier($identifier, $storeId = null);