Beispiel #1
0
 /**
  * Convert categories tree to array recursively
  *
  * @param  \Magento\Framework\Data\Tree\Node $node
  * @return array
  */
 protected function _getNodesArray($node)
 {
     $result = ['id' => (int) $node->getId(), 'parent_id' => (int) $node->getParentId(), 'children_count' => (int) $node->getChildrenCount(), 'is_active' => (bool) $node->getIsActive(), 'name' => $node->getName(), 'level' => (int) $node->getLevel(), 'product_count' => (int) $node->getProductCount()];
     if ($node->getParentId() == Category::TREE_ROOT_ID && !in_array($result['id'], $this->_allowedCategoryIds)) {
         $result['disabled'] = true;
     }
     if ($node->hasChildren()) {
         $result['children'] = [];
         foreach ($node->getChildren() as $childNode) {
             $result['children'][] = $this->_getNodesArray($childNode);
         }
     }
     $result['cls'] = ($result['is_active'] ? '' : 'no-') . 'active-category';
     $result['expanded'] = !empty($result['children']);
     return $result;
 }
Beispiel #2
0
 /**
  * @param \Magento\Framework\Data\Tree\Node $node
  * @param int $depth
  * @param int $currentLevel
  * @return \Magento\Catalog\Service\V1\Data\Eav\Category\Tree[]
  */
 public function getTree($node, $depth = null, $currentLevel = 0)
 {
     $builder = $this->treeBuilderFactory->create();
     $builder->setId($node->getId())->setParentId($node->getParentId())->setName($node->getName())->setPosition($node->getPosition())->setLevel($node->getLevel())->setActive($node->getIsActive())->setProductCount($node->getProductCount())->setChildren([]);
     if ($node->hasChildren()) {
         $children = array();
         foreach ($node->getChildren() as $child) {
             if (!is_null($depth) && $depth <= $currentLevel) {
                 break;
             }
             $children[] = $this->getTree($child, $depth, $currentLevel + 1);
         }
         $builder->setChildren($children);
     }
     return $builder->create();
 }