Skip to content

Commit e43d8e6

Browse files
author
Jose Ortega
committed
Init files
1 parent d2d8c3c commit e43d8e6

10 files changed

+248
-0
lines changed

Api/BlockManagerInterface.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Snowdog\CmsApi\Api;
4+
5+
interface BlockManagerInterface
6+
{
7+
/**
8+
* @param int $blockId
9+
* @return \Magento\Cms\Api\Data\BlockInterface
10+
*/
11+
public function getById($blockId);
12+
13+
/**
14+
* @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
15+
* @return \Magento\Cms\Api\Data\BlockSearchResultsInterface
16+
* @throws \Magento\Framework\Exception\LocalizedException
17+
*/
18+
public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria);
19+
}

Api/PageManagerInterface.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Snowdog\CmsApi\Api;
4+
5+
interface PageManagerInterface
6+
{
7+
/**
8+
* @param int $pageId
9+
* @return \Magento\Cms\Api\Data\PageInterface
10+
*/
11+
public function getById($pageId);
12+
13+
/**
14+
* @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
15+
* @return \Magento\Cms\Api\Data\PageSearchResultsInterface
16+
* @throws \Magento\Framework\Exception\LocalizedException
17+
*/
18+
public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria);
19+
}

CHANGELOG.md

Whitespace-only changes.

Model/BlockManager.php

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace Snowdog\CmsApi\Model;
4+
5+
use Magento\Cms\Api\BlockRepositoryInterface;
6+
use Magento\Cms\Model\Template\FilterProvider;
7+
use Snowdog\CmsApi\Api\BlockManagerInterface;
8+
9+
class BlockManager implements BlockManagerInterface
10+
{
11+
/**
12+
* @var BlockRepositoryInterface
13+
*/
14+
private $blockRepository;
15+
16+
/**
17+
* @var FilterProvider
18+
*/
19+
private $filterProvider;
20+
21+
public function __construct(
22+
BlockRepositoryInterface $blockRepository,
23+
FilterProvider $filterProvider
24+
) {
25+
$this->blockRepository = $blockRepository;
26+
$this->filterProvider = $filterProvider;
27+
}
28+
29+
/**
30+
* @inheritdoc
31+
*/
32+
public function getById($blockId)
33+
{
34+
$block = $this->blockRepository->getById($blockId);
35+
$content = $this->getBlockContentFiltered($block->getContent());
36+
$block->setContent($content);
37+
38+
return $block;
39+
}
40+
41+
/**
42+
* @inheritdoc
43+
*/
44+
public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
45+
{
46+
$blockCollection = $this->blockRepository->getList($searchCriteria);
47+
$blocks = $blockCollection->getItems();
48+
$parsedBlocks = [];
49+
50+
foreach ($blocks as $block) {
51+
$content = $this->getBlockContentFiltered($block->getContent());
52+
$parsedBlocks[] = $block->setContent($content);
53+
}
54+
55+
$blockCollection->setItems($parsedBlocks);
56+
57+
return $blockCollection;
58+
}
59+
60+
/**
61+
* @param string $content
62+
* @return string
63+
*/
64+
private function getBlockContentFiltered($content)
65+
{
66+
return $this->filterProvider->getBlockFilter()
67+
->filter($content);
68+
}
69+
}

Model/PageManager.php

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace Snowdog\CmsApi\Model;
4+
5+
use Magento\Cms\Api\PageRepositoryInterface;
6+
use Magento\Cms\Model\Template\FilterProvider;
7+
use Snowdog\CmsApi\Api\PageManagerInterface;
8+
9+
class PageManager implements PageManagerInterface
10+
{
11+
/**
12+
* @var PageRepositoryInterface
13+
*/
14+
private $pageRepository;
15+
16+
/**
17+
* @var FilterProvider
18+
*/
19+
private $filterProvider;
20+
21+
public function __construct(
22+
PageRepositoryInterface $pageRepository,
23+
FilterProvider $filterProvider
24+
) {
25+
$this->pageRepository = $pageRepository;
26+
$this->filterProvider = $filterProvider;
27+
}
28+
29+
/**
30+
* @inheritdoc
31+
*/
32+
public function getById($pageId)
33+
{
34+
$page = $this->pageRepository->getById($pageId);
35+
$content = $this->getPageContentFiltered($page->getContent());
36+
$page->setContent($content);
37+
38+
return $page;
39+
}
40+
41+
/**
42+
* @inheritdoc
43+
*/
44+
public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
45+
{
46+
$pageCollection = $this->pageRepository->getList($searchCriteria);
47+
$pages = $pageCollection->getItems();
48+
$parsedPages = [];
49+
50+
foreach ($pages as $page) {
51+
$content = $this->getPageContentFiltered($page->getContent());
52+
$parsedPages[] = $page->setContent($content);
53+
}
54+
55+
$pageCollection->setItems($parsedPages);
56+
57+
return $pageCollection;
58+
}
59+
60+
/**
61+
* @param string $content
62+
* @return string
63+
*/
64+
private function getPageContentFiltered($content)
65+
{
66+
return $this->filterProvider->getPageFilter()
67+
->filter($content);
68+
}
69+
}

composer.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "snowdog/module-cms-api",
3+
"description": "Custom CMS API endpoints which retrieves CMS pages and blocks filtered",
4+
"require": {
5+
"magento/framework": "100.1.*|101.0.*",
6+
"magento/module-cms": "101.0.*|102.0.*",
7+
"php": "7.0.2|7.0.4|~7.0.6|~7.1.0"
8+
},
9+
"license": "MIT",
10+
"type": "magento2-module",
11+
"autoload": {
12+
"files": [
13+
"registration.php"
14+
],
15+
"psr-4": {
16+
"Snowdog\\CmsApi\\": ""
17+
}
18+
},
19+
"authors": [
20+
{
21+
"name": "Jose Ortega",
22+
"email": "[email protected]"
23+
}
24+
]
25+
}

etc/di.xml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0"?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
3+
<preference for="Snowdog\CmsApi\Api\PageManagerInterface"
4+
type="Snowdog\CmsApi\Model\PageManager" />
5+
<preference for="Snowdog\CmsApi\Api\BlockManagerInterface"
6+
type="Snowdog\CmsApi\Model\BlockManager" />
7+
</config>

etc/module.xml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0"?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
3+
<module name="Snowdog_CmsApi" setup_version="0.1.0">
4+
</module>
5+
</config>

etc/webapi.xml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0"?>
2+
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
3+
<!-- Cms Page -->
4+
<route url="/V1/snowdog/cmsPage/:pageId" method="GET">
5+
<service class="Snowdog\CmsApi\Api\PageManagerInterface" method="getById"/>
6+
<resources>
7+
<resource ref="Magento_Cms::page"/>
8+
</resources>
9+
</route>
10+
<route url="/V1/snowdog/cmsPage/search" method="GET">
11+
<service class="Snowdog\CmsApi\Api\PageManagerInterface" method="getList"/>
12+
<resources>
13+
<resource ref="Magento_Cms::page"/>
14+
</resources>
15+
</route>
16+
<!-- Cms Block -->
17+
<route url="/V1/snowdog/cmsBlock/:blockId" method="GET">
18+
<service class="Snowdog\CmsApi\Api\BlockManagerInterface" method="getById"/>
19+
<resources>
20+
<resource ref="Magento_Cms::block"/>
21+
</resources>
22+
</route>
23+
<route url="/V1/snowdog/cmsBlock/search" method="GET">
24+
<service class="Snowdog\CmsApi\Api\BlockManagerInterface" method="getList"/>
25+
<resources>
26+
<resource ref="Magento_Cms::block"/>
27+
</resources>
28+
</route>
29+
</routes>

registration.php

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
\Magento\Framework\Component\ComponentRegistrar::register(
3+
\Magento\Framework\Component\ComponentRegistrar::MODULE,
4+
'Snowdog_CmsApi',
5+
__DIR__
6+
);

0 commit comments

Comments
 (0)