init
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
|
||||
namespace Stemcell\Catalog\Block\Theme\Html;
|
||||
|
||||
use Magento\Framework\App\ObjectManager;
|
||||
use Magento\Framework\Data\Tree\Node;
|
||||
use Magento\Framework\Data\Tree\Node\Collection;
|
||||
use Magento\Framework\Data\Tree\NodeFactory;
|
||||
use Magento\Framework\Data\TreeFactory;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class Topmenu extends \Magento\Theme\Block\Html\Topmenu
|
||||
{
|
||||
/**
|
||||
* @var NodeFactory
|
||||
*/
|
||||
protected $nodeFactory;
|
||||
|
||||
/**
|
||||
* @var TreeFactory
|
||||
*/
|
||||
protected $treeFactory;
|
||||
|
||||
protected function getChildLevel($parentLevel): int
|
||||
{
|
||||
return $parentLevel === null ? 0 : $parentLevel + 1;
|
||||
}
|
||||
|
||||
protected function removeChildrenWithoutActiveParent(Collection $children, int $childLevel): void
|
||||
{
|
||||
/** @var Node $child */
|
||||
foreach ($children as $child) {
|
||||
if ($childLevel === 0 && $child->getData('is_parent_active') === false) {
|
||||
$children->delete($child);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function setCurrentClass(Node $child, string $outermostClass): void
|
||||
{
|
||||
$currentClass = $child->getClass();
|
||||
if (empty($currentClass)) {
|
||||
$child->setClass($outermostClass);
|
||||
} else {
|
||||
$child->setClass($currentClass . ' ' . $outermostClass);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @override
|
||||
*
|
||||
* @param array $colBrakes
|
||||
* @param int $counter
|
||||
* @return bool
|
||||
*/
|
||||
protected function shouldAddNewColumn(array $colBrakes, int $counter): bool
|
||||
{
|
||||
return count($colBrakes) && $colBrakes[$counter]['colbrake'];
|
||||
}
|
||||
|
||||
protected function getCategoryUrl(Node $node)
|
||||
{
|
||||
// $logger = ObjectManager::getInstance()->get(LoggerInterface::class);
|
||||
// $logger->info('stemcell $node', ['$node->getData()'=>$node->getData()]);
|
||||
if ($node->getUrl()) {
|
||||
$path = parse_url($node->getUrl(), PHP_URL_PATH);
|
||||
if (ltrim($path, '/') === 'products.html') {
|
||||
return '/products/product-types.html';
|
||||
}
|
||||
}
|
||||
|
||||
return $node->getUrl();
|
||||
}
|
||||
|
||||
/**
|
||||
* @override
|
||||
*
|
||||
* @param Node $menuTree
|
||||
* @param string $childrenWrapClass
|
||||
* @param int $limit
|
||||
* @param array $colBrakes
|
||||
* @return string
|
||||
*/
|
||||
protected function _getHtml(
|
||||
Node $menuTree,
|
||||
$childrenWrapClass,
|
||||
$limit,
|
||||
array $colBrakes = []
|
||||
) {
|
||||
$html = '';
|
||||
|
||||
$children = $menuTree->getChildren();
|
||||
$childLevel = $this->getChildLevel($menuTree->getLevel());
|
||||
$this->removeChildrenWithoutActiveParent($children, $childLevel);
|
||||
|
||||
$counter = 1;
|
||||
$childrenCount = $children->count();
|
||||
|
||||
$parentPositionClass = $menuTree->getPositionClass();
|
||||
$itemPositionClassPrefix = $parentPositionClass ? $parentPositionClass . '-' : 'nav-';
|
||||
|
||||
/** @var Node $child */
|
||||
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 . '" ';
|
||||
$this->setCurrentClass($child, $outermostClass);
|
||||
}
|
||||
|
||||
if ($this->shouldAddNewColumn($colBrakes, $counter)) {
|
||||
$html .= '</ul></li><li class="column"><ul>';
|
||||
}
|
||||
|
||||
$html .= '<li ' . $this->_getRenderedMenuItemAttributes($child) . '>';
|
||||
$html .= '<a href="' . $this->getCategoryUrl($child) . '" ' . $outermostClassCode . '><span>' . $this->escapeHtml(
|
||||
$child->getName()
|
||||
) . '</span></a>' . $this->_addSubMenu(
|
||||
$child,
|
||||
$childLevel,
|
||||
$childrenWrapClass,
|
||||
$limit
|
||||
) . '</li>';
|
||||
$counter++;
|
||||
}
|
||||
|
||||
if (is_array($colBrakes) && !empty($colBrakes) && $limit) {
|
||||
$html = '<li class="column"><ul>' . $html . '</ul></li>';
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user