Home  >  Q&A  >  body text

Overwriting block file from vendor module, changes not showing

We use the Codazon MegaMenu module for the main menu, but the links don't have title tags. I want to override this so that a title attribute is added to the category link with the value being the category name.

I created my own module and copied the specific method (_getHtml) where I create the tags for the category links and tried adding the title tag. I think I'm doing everything right, but after refreshing the page I never see the title appear.

I refreshed/cleaned the cache, upgraded and refreshed the static files.

This is the Block/Widgets file in my module (originally located in app/code/Codazon/MegaMenu/Block/Widget):

<?php


namespace MyModule\MegaMenu\Block\Widget;

use Codazon\MegaMenu\Block\Widget\CategoriesTree as OriginalCategoriesTree;

class CategoriesTree extends OriginalCategoriesTree
{
    protected function _getHtml(
        \Magento\Framework\Data\Tree\Node $menuTree,
        $childrenWrapClass,
        $limit,
        $colBrakes = []
    ) {
        $html = '';
        $col = 1;
        
        $itemCount = $this->getData('item_count');
        
        $children = $menuTree->getChildren();
        $parentLevel = $menuTree->getLevel();
        $childLevel = ($parentLevel === null) ? 1 : $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 ($this->shouldAddNewColumn($colBrakes, $counter)) {
                $col = 24/ceil($childrenCount/$limit);
                $html .= '</ul></li><li class="col-sm-'.$col.'"><ul>';
            }

            $html .= '<li ' . $this->_getRenderedMenuItemAttributes($child) . '>';
            $html .= '<a class="menu-link" href="' . $child->getUrl() . '" ' . $outermostClassCode . ' title="' . $this->escapeHtml($child->getName()) . '"><span>' . $this->escapeHtml(
                $child->getName()
            ) . '</span></a>' . $this->_addSubMenu(
                $child,
                $childLevel,
                $childrenWrapClass,
                $limit
            ) . '</li>';

            $itemPosition++;
            if($itemCount){
                if($itemCount == $counter){
                    break;      
                }
            }
            $counter++;
        }

        if (is_array($colBrakes) && count($colBrakes) && $limit) {
            $html = '<li class="col-sm-'.$col.'"><ul>' . $html . '</ul></li>';
        }

        return $html;
    }   
}

This is my di.xml:

<?xml version="1.0"?> <config 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Codazon\MegaMenu\Block\Widget\CategoriesTree" 
type="MyModule\MegaMenu\Block\Widget\CategoriesTree" /> </config>

This is my module.xml:

<?xml version="1.0"?> <config 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="MyModule_MegaMenu" setup_version="1.0.0">
        <sequence>
            <module name="Codazon_MegaMenu"/>
        </sequence>
    </module> </config>

Any ideas why this doesn't work? This is my first time doing coverage like this.

Thanks!

P粉321584263P粉321584263300 days ago379

reply all(1)I'll reply

  • P粉714844743

    P粉7148447432024-01-17 17:46:06

    I think you need to add all the dependencies of the parent block in the module block.

    reply
    0
  • Cancelreply