/** * Recursively generates top menu html from data that is specified in $menuTree * * @param \Magento\Framework\Data\Tree\Node $menuTree * @param string $childrenWrapClass * @param int $limit * @param array $colBrakes * @return string * * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ protected function _getHtml(\Magento\Framework\Data\Tree\Node $menuTree, $childrenWrapClass, $limit, $colBrakes = []) { $html = ''; $children = $menuTree->getChildren(); $parentLevel = $menuTree->getLevel(); $childLevel = $parentLevel === null ? 0 : $parentLevel + 1; $counter = 1; $itemPosition = 1; $childrenCount = $children->count(); $parentPositionClass = $menuTree->getPositionClass(); $itemPositionClassPrefix = $parentPositionClass ? $parentPositionClass . '-' : 'nav-'; foreach ($children as $child) { $child->setLevel($childLevel); $child->setIsFirst($counter == 1); $child->setIsLast($counter == $childrenCount); $child->setPositionClass($itemPositionClassPrefix . $counter); $outermostClassCode = ''; $outermostClass = $menuTree->getOutermostClass(); if ($childLevel == 0 && $outermostClass) { $outermostClassCode = ' class="' . $outermostClass . '" '; $child->setClass($outermostClass); } if (count($colBrakes) && $colBrakes[$counter]['colbrake']) { $html .= '</ul></li><li class="column"><ul>'; } $html .= '<li ' . $this->_getRenderedMenuItemAttributes($child) . '>'; $html .= '<a href="' . $child->getUrl() . '" ' . $outermostClassCode . '><span>' . $this->escapeHtml($child->getName()) . '</span></a>' . $this->_addSubMenu($child, $childLevel, $childrenWrapClass, $limit) . '</li>'; $itemPosition++; $counter++; } if (count($colBrakes) && $limit) { $html = '<li class="column"><ul>' . $html . '</ul></li>'; } return $html; }
/** * Add child nodes * * @param array $children * @param string $path * @param Node $parentNode * @param bool $withChildren * @param int $level * @return void */ protected function _addChildNodes($children, $path, $parentNode, $withChildren = false, $level = 0) { if (isset($children[$path])) { foreach ($children[$path] as $child) { $nodeId = isset($child[$this->_idField]) ? $child[$this->_idField] : false; if ($parentNode && $nodeId && ($node = $parentNode->getChildren()->searchById($nodeId))) { $node->addData($child); } else { $node = new Node($child, $this->_idField, $this, $parentNode); $node->setLevel($node->getData($this->_levelField)); $node->setPathId($node->getData($this->_pathField)); $this->addNode($node, $parentNode); } if ($withChildren) { $this->_loaded = false; $node->loadChildren(1); $this->_loaded = false; } if ($path) { $childrenPath = explode('/', $path); } else { $childrenPath = array(); } $childrenPath[] = $node->getId(); $childrenPath = implode('/', $childrenPath); $this->_addChildNodes($children, $childrenPath, $node, $withChildren, $level + 1); } } }
/** * 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; }
/** * @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(); }
/** * @param \Magento\Framework\Data\Tree\Node $node * @param int $depth * @param int $currentLevel * @return \Magento\Catalog\Api\Data\CategoryTreeInterface[]|[] */ protected function getChildren($node, $depth, $currentLevel) { if ($node->hasChildren()) { $children = []; foreach ($node->getChildren() as $child) { if ($depth !== null && $depth <= $currentLevel) { break; } $children[] = $this->getTree($child, $depth, $currentLevel + 1); } return $children; } return []; }
/** * Get child categories * * @param \Magento\Framework\Data\Tree\Node $tree * @param string $name * @return mixed */ protected function findTreeChild($tree, $name) { $foundChild = null; if ($name) { foreach ($tree->getChildren() as $child) { if ($child->getName() == $name) { $foundChild = $child; break; } } } return $foundChild; }
/** * @param array|Node $node * @param int $level * @return array * @SuppressWarnings(PHPMD.NPathComplexity) */ protected function _getNodeJson($node, $level = 1) { $item = []; $item['text'] = $this->escapeHtml($node->getName()); if ($this->_withProductCount) { $item['text'] .= ' (' . $node->getProductCount() . ')'; } $item['id'] = $node->getId(); $item['path'] = $node->getData('path'); $item['cls'] = 'folder ' . ($node->getIsActive() ? 'active-category' : 'no-active-category'); $item['allowDrop'] = false; $item['allowDrag'] = false; if (in_array($node->getId(), $this->getCategoryIds())) { $this->setExpandedPath($node->getData('path')); $item['checked'] = true; } if ($node->getLevel() < 2) { $this->setExpandedPath($node->getData('path')); } if ($node->hasChildren()) { $item['children'] = []; foreach ($node->getChildren() as $child) { $item['children'][] = $this->_getNodeJson($child, $level + 1); } } if (empty($item['children']) && (int) $node->getChildrenCount() > 0) { $item['children'] = []; } $item['expanded'] = in_array($node->getId(), $this->getExpandedPath()); return $item; }