Skip to content

Commit

Permalink
Merge branch 'release/2.10.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Samir Hajiyev committed May 29, 2019
2 parents fc63022 + fdc4399 commit 7fc6f58
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 4 deletions.
9 changes: 9 additions & 0 deletions Api/NodeRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,13 @@ public function deleteById($id);
* @return \Snowdog\Menu\Api\Data\NodeInterface[]
*/
public function getByMenu($menuId);

/**
* Return node by identifier
*
* @api
* @param string $identifier
* @return \Snowdog\Menu\Api\Data\NodeInterface[]
*/
public function getByIdentifier($identifier);
}
29 changes: 27 additions & 2 deletions Block/NodeType/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ class Category extends AbstractNode
* @var Registry
*/
private $coreRegistry;

/**
* @var ModelCategory
*/
private $_categoryModel;
/**
* @var array
*/
private $categories;

/**
* Category constructor.
Expand Down Expand Up @@ -102,7 +105,7 @@ public function fetchData(array $nodes)
{
$storeId = $this->_storeManager->getStore()->getId();

list($this->nodes, $this->categoryUrls) = $this->_categoryModel->fetchData($nodes, $storeId);
list($this->nodes, $this->categoryUrls, $this->categories) = $this->_categoryModel->fetchData($nodes, $storeId);
}

/**
Expand Down Expand Up @@ -150,6 +153,28 @@ public function getCategoryUrl($nodeId, $storeId = null)
return false;
}

/**
* @param int $nodeId
*
* @return object|false
* @throws \InvalidArgumentException
*/
public function getCategory(int $nodeId)
{
if (!isset($this->nodes[$nodeId])) {
throw new \InvalidArgumentException('Invalid node identifier specified');
}

$node = $this->nodes[$nodeId];
$categoryId = (int) $node->getContent();

if (isset($this->categories[$categoryId])) {
return $this->categories[$categoryId];
}

return false;
}

/**
* @param int $nodeId
* @param int $level
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]


## [2.10.0] - 2019-05-29
### Added
- Provide access to entity associated with menu node (#83)
- New API endpoint to get nodes by identifier, additional information to the response (#70)

## [2.9.0] - 2019-04-21
### Added
- Wrapper node
Expand Down
12 changes: 12 additions & 0 deletions Model/Menu/NodeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,16 @@ public function getByMenu($menuId)
$collection->addOrder('position', AbstractCollection::SORT_ORDER_ASC);
return $collection->getItems();
}

public function getByIdentifier($identifier)
{
$collection = $this->collectionFactory->create();
$collection->addFilter('main_table.is_active', 1);
$collection->addOrder('level', AbstractCollection::SORT_ORDER_ASC);
$collection->addOrder('parent_id', AbstractCollection::SORT_ORDER_ASC);
$collection->addOrder('position', AbstractCollection::SORT_ORDER_ASC);
$collection->join(['menu' => 'snowmenu_menu'], 'main_table.menu_id = menu.menu_id', 'identifier');
$collection->addFilter('identifier', $identifier);
return $collection->getItems();
}
}
37 changes: 35 additions & 2 deletions Model/NodeType/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\Framework\Profiler;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;

class Category extends AbstractNode
{
Expand All @@ -21,6 +22,11 @@ class Category extends AbstractNode
*/
private $metadataPool;

/**
* @var CollectionFactory
*/
private $categoryCollection;

/**
* @inheritDoc
*/
Expand All @@ -35,12 +41,15 @@ protected function _construct()
*
* @param Profiler $profiler
* @param MetadataPool $metadataPool
* @param CollectionFactory $categoryCollection
*/
public function __construct(
Profiler $profiler,
MetadataPool $metadataPool
MetadataPool $metadataPool,
CollectionFactory $categoryCollection
) {
$this->metadataPool = $metadataPool;
$this->categoryCollection = $categoryCollection;
parent::__construct($profiler);
}

Expand Down Expand Up @@ -104,9 +113,33 @@ public function fetchData(array $nodes, $storeId)
}

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

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

return [$localNodes, $categoryUrls];
return [$localNodes, $categoryUrls, $categories];
}

/**
* @param int|string|\Magento\Store\Model\Store $store
* @param array $categoryIds
* @return array
*/
public function getCategories($store, array $categoryIds)
{
$return = [];
$categories = $this->categoryCollection->create()
->addAttributeToSelect('*')
->setStoreId($store)
->addFieldToFilter(
'entity_id',
['in' => $categoryIds]
);

foreach ($categories as $category) {
$return[$category->getId()] = $category;
}

return $return;
}
}
6 changes: 6 additions & 0 deletions etc/webapi.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@
<resource ref="Snowdog_Menu::menus"/>
</resources>
</route>
<route url="/V1/menus/:identifier/nodes" method="GET">
<service class="Snowdog\Menu\Api\NodeRepositoryInterface" method="getByIdentifier"/>
<resources>
<resource ref="Snowdog_Menu::menus"/>
</resources>
</route>
</routes>

0 comments on commit 7fc6f58

Please sign in to comment.