0

I am creating menu items in magento 2. I is working fine and generated according to category order in admin dashboard.

But I wanted to sort the menu items alphabetically on store front ir-respective of their order in admin. How can I achieve this. Here is my piece of code:

public function getHtml($outermostClass = '', $childrenWrapClass = '', $limit = 0)
{
    $this->_eventManager->dispatch(
        'page_block_html_topmenu_gethtml_before',
        ['menu' => $this->getMenu(), 'block' => $this, 'request' => $this->getRequest()]
    );

    $this->getMenu()->setOutermostClass($outermostClass);
    $this->getMenu()->setChildrenWrapClass($childrenWrapClass);

    $html = $this->_getHtml($this->getMenu(), $childrenWrapClass, $limit);

    $transportObject = new \Magento\Framework\DataObject(['html' => $html]);
    $this->_eventManager->dispatch(
        'page_block_html_topmenu_gethtml_after',
        ['menu' => $this->getMenu(), 'transportObject' => $transportObject]
    );
    $html = $transportObject->getHtml();
    return $html;
}

protected function _getHtml(
    \Magento\Framework\Data\Tree\Node $menuTree,
    $childrenWrapClass,
    $limit,
    array $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 . '-' : 'nxb-';

    /** @var \Magento\Framework\Data\Tree\Node $child */
    foreach ($children as $child) {
        if ($childLevel === 0 && $child->getData('is_parent_active') === false) {
            continue;
        }
        $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 . '" ';
            $currentClass = $child->getClass();

            if (empty($currentClass)) {
                $child->setClass($outermostClass);
            } else {
                $child->setClass($currentClass . ' ' . $outermostClass);
            }
        }

        if (is_array($colBrakes) && count($colBrakes) && $colBrakes[$counter]['colbrake']) {
            $html .= '</ul></li><li class="column"><ul>';
        }

        $html .= '<li class="nxb-menu-item '.$this->_getRenderedMenuItemAttributes($child).'';

        if ($child->hasChildren()){
            $html .= ' nxb-has-child';
        }

        $html .= '">';
        $html .= '<a href="' . $child->getUrl() . '" ' . $outermostClassCode . '>' . $this->escapeHtml(
                $child->getName()
            ) . '</a>' . $this->_addSubMenu(
                $child,
                $childLevel,
                $childrenWrapClass,
                $limit
            ) . '</li>';
        $itemPosition++;
        $counter++;
    }

    if (is_array($colBrakes) && count($colBrakes) && $limit) {
        $html = '<li class="column"><ol>' . $html . '</ol></li>';
    }

    return $html;
}

protected function _addSubMenu($child, $childLevel, $childrenWrapClass, $limit)
{
    $html = '';
    if (!$child->hasChildren()) {
        return $html;
    }

    $colStops = [];
    if ($childLevel == 0 && $limit) {
        $colStops = $this->_columnBrake($child->getChildren(), $limit);
    }

    $html .= '<ul class="nxb-depth-' . $childLevel . ' ' . $childrenWrapClass . '">';
    $html .= $this->_getHtml($child, $childrenWrapClass, $limit, $colStops);
    $html .= '</ul>';

    return $html;
}

Above are the two main functions.

  • I need to be able to something similar. I have multiple translated sites that require the translated text be sorted alphabetically. – guerinteed_mike Aug 3 at 23:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.