Magento 2 – Topmenu Navigation Not Rendering

magento2navigation

Does anyone have advice on how to debug why the main navigation wouldn't be rendering on my site? It's definitely caused by my custom module (which extends Nav's Topmenu.php), but everything's working locally so I'm having difficulties determining why.

Any tips would be appreciated!

Edit: Are there any logs I could check / ways of determining where exactly in the process that this fails?

Here's my code for Topmenu.php:

namespace Ibex\Nav\Block\Html;

use Magento\Framework\Data\Tree\Node;
use Magento\Framework\DataObject;
use Magento\Framework\View\Element\Template;

class Topmenu extends \Magento\Theme\Block\Html\Topmenu
{
    /**
     * Get top menu html
     *
     * @param string $outermostClass
     * @param string $childrenWrapClass
     * @param int $limit
     * @return string
     */
     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-';

         /** @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 . '" ';
                 $child->setClass($outermostClass);
             }

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

             $href = 'href="' . $child->getUrl() . '"';
             $html .= '<li ' . $this->_getRenderedMenuItemAttributes($child) . '>';
             $html .= '<a ' . $href . $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>';
         }

         if($childLevel == 2){
           $html .= '<li><a href="#" class="shop-all">Shop All</a></li>';
         }
         return $html;
     }

     /**
      * Add sub menu HTML code for current menu item
      *
      * @param \Magento\Framework\Data\Tree\Node $child
      * @param string $childLevel
      * @param string $childrenWrapClass
      * @param int $limit
      * @return string HTML code
      */
     protected function _addSubMenu($child, $childLevel, $childrenWrapClass, $limit)
    {
        $html = '';
        if (!$child->hasChildren()) {
            return $html;
        }

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

        $extraclass = 'isactive';

        if ($childLevel == 0) {
          $html .= '<div class="megamenu-wrapper"><div class="megamenu">';
          $html .= '<ul class="level' . $childLevel . ' ' . $childrenWrapClass . ' ' . $extraclass . '">';
          $html .= $this->_getHtml($child, $childrenWrapClass, $limit, $colStops);
          $html .= '<li class="level1"><a href="shop.html"><span>Shop All Categories</span></a></li>';
          $html .= '</ul>';
          $html .= $this->getChildHtml();
          $html .= '</div></div>';
        } else {
          $html .= '<ul class="level' . $childLevel . ' ' . $childrenWrapClass . '">';
          $html .= $this->_getHtml($child, $childrenWrapClass, $limit, $colStops);
          $html .= '</ul>';
        }

        return $html;
    }

}

Aside from this, there's just CSS — no layout/template files that could be interfering. Very odd. Any assistance in pinpointing the error would be greatly appreciated.

Best Answer

Welp, I would've never thought that this would be the solution, but it turns out that removing the block and re-adding it without the TTL parameter was the fix:

<referenceBlock name="catalog.topnav" remove="true"/>

<referenceBlock name="store.menu">
    <block class="Magento\Theme\Block\Html\Topmenu" name="catalog.topnav.fix" template="Magento_Theme::html/topmenu.phtml" before="-"/>
</referenceBlock>

Thanks to Gabriel in this thread: Magento 2 https ssl missing navigation menu

Related Topic