Skip to content

Commit 7fc6f58

Browse files
author
Samir Hajiyev
committedMay 29, 2019
Merge branch 'release/2.10.0'
2 parents fc63022 + fdc4399 commit 7fc6f58

File tree

6 files changed

+95
-4
lines changed

6 files changed

+95
-4
lines changed
 

‎Api/NodeRepositoryInterface.php

+9
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,13 @@ public function deleteById($id);
4444
* @return \Snowdog\Menu\Api\Data\NodeInterface[]
4545
*/
4646
public function getByMenu($menuId);
47+
48+
/**
49+
* Return node by identifier
50+
*
51+
* @api
52+
* @param string $identifier
53+
* @return \Snowdog\Menu\Api\Data\NodeInterface[]
54+
*/
55+
public function getByIdentifier($identifier);
4756
}

‎Block/NodeType/Category.php

+27-2
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,14 @@ class Category extends AbstractNode
3030
* @var Registry
3131
*/
3232
private $coreRegistry;
33-
3433
/**
3534
* @var ModelCategory
3635
*/
3736
private $_categoryModel;
37+
/**
38+
* @var array
39+
*/
40+
private $categories;
3841

3942
/**
4043
* Category constructor.
@@ -102,7 +105,7 @@ public function fetchData(array $nodes)
102105
{
103106
$storeId = $this->_storeManager->getStore()->getId();
104107

105-
list($this->nodes, $this->categoryUrls) = $this->_categoryModel->fetchData($nodes, $storeId);
108+
list($this->nodes, $this->categoryUrls, $this->categories) = $this->_categoryModel->fetchData($nodes, $storeId);
106109
}
107110

108111
/**
@@ -150,6 +153,28 @@ public function getCategoryUrl($nodeId, $storeId = null)
150153
return false;
151154
}
152155

156+
/**
157+
* @param int $nodeId
158+
*
159+
* @return object|false
160+
* @throws \InvalidArgumentException
161+
*/
162+
public function getCategory(int $nodeId)
163+
{
164+
if (!isset($this->nodes[$nodeId])) {
165+
throw new \InvalidArgumentException('Invalid node identifier specified');
166+
}
167+
168+
$node = $this->nodes[$nodeId];
169+
$categoryId = (int) $node->getContent();
170+
171+
if (isset($this->categories[$categoryId])) {
172+
return $this->categories[$categoryId];
173+
}
174+
175+
return false;
176+
}
177+
153178
/**
154179
* @param int $nodeId
155180
* @param int $level

‎CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
## [Unreleased]
88

9+
10+
## [2.10.0] - 2019-05-29
11+
### Added
12+
- Provide access to entity associated with menu node (#83)
13+
- New API endpoint to get nodes by identifier, additional information to the response (#70)
14+
915
## [2.9.0] - 2019-04-21
1016
### Added
1117
- Wrapper node

‎Model/Menu/NodeRepository.php

+12
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,16 @@ public function getByMenu($menuId)
111111
$collection->addOrder('position', AbstractCollection::SORT_ORDER_ASC);
112112
return $collection->getItems();
113113
}
114+
115+
public function getByIdentifier($identifier)
116+
{
117+
$collection = $this->collectionFactory->create();
118+
$collection->addFilter('main_table.is_active', 1);
119+
$collection->addOrder('level', AbstractCollection::SORT_ORDER_ASC);
120+
$collection->addOrder('parent_id', AbstractCollection::SORT_ORDER_ASC);
121+
$collection->addOrder('position', AbstractCollection::SORT_ORDER_ASC);
122+
$collection->join(['menu' => 'snowmenu_menu'], 'main_table.menu_id = menu.menu_id', 'identifier');
123+
$collection->addFilter('identifier', $identifier);
124+
return $collection->getItems();
125+
}
114126
}

‎Model/NodeType/Category.php

+35-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Catalog\Api\Data\CategoryInterface;
1414
use Magento\Framework\EntityManager\MetadataPool;
1515
use Magento\Framework\Profiler;
16+
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
1617

1718
class Category extends AbstractNode
1819
{
@@ -21,6 +22,11 @@ class Category extends AbstractNode
2122
*/
2223
private $metadataPool;
2324

25+
/**
26+
* @var CollectionFactory
27+
*/
28+
private $categoryCollection;
29+
2430
/**
2531
* @inheritDoc
2632
*/
@@ -35,12 +41,15 @@ protected function _construct()
3541
*
3642
* @param Profiler $profiler
3743
* @param MetadataPool $metadataPool
44+
* @param CollectionFactory $categoryCollection
3845
*/
3946
public function __construct(
4047
Profiler $profiler,
41-
MetadataPool $metadataPool
48+
MetadataPool $metadataPool,
49+
CollectionFactory $categoryCollection
4250
) {
4351
$this->metadataPool = $metadataPool;
52+
$this->categoryCollection = $categoryCollection;
4453
parent::__construct($profiler);
4554
}
4655

@@ -104,9 +113,33 @@ public function fetchData(array $nodes, $storeId)
104113
}
105114

106115
$categoryUrls = $this->getResource()->fetchData($storeId, $categoryIds);
116+
$categories = $this->getCategories($storeId, $categoryIds);
107117

108118
$this->profiler->stop(__METHOD__);
109119

110-
return [$localNodes, $categoryUrls];
120+
return [$localNodes, $categoryUrls, $categories];
121+
}
122+
123+
/**
124+
* @param int|string|\Magento\Store\Model\Store $store
125+
* @param array $categoryIds
126+
* @return array
127+
*/
128+
public function getCategories($store, array $categoryIds)
129+
{
130+
$return = [];
131+
$categories = $this->categoryCollection->create()
132+
->addAttributeToSelect('*')
133+
->setStoreId($store)
134+
->addFieldToFilter(
135+
'entity_id',
136+
['in' => $categoryIds]
137+
);
138+
139+
foreach ($categories as $category) {
140+
$return[$category->getId()] = $category;
141+
}
142+
143+
return $return;
111144
}
112145
}

‎etc/webapi.xml

+6
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,10 @@
1212
<resource ref="Snowdog_Menu::menus"/>
1313
</resources>
1414
</route>
15+
<route url="/V1/menus/:identifier/nodes" method="GET">
16+
<service class="Snowdog\Menu\Api\NodeRepositoryInterface" method="getByIdentifier"/>
17+
<resources>
18+
<resource ref="Snowdog_Menu::menus"/>
19+
</resources>
20+
</route>
1521
</routes>

0 commit comments

Comments
 (0)