我们使用 Codazon MegaMenu 模块作为主菜单,但链接没有标题标签。我想重写它,以便将标题属性添加到类别链接,其值为类别名称。
我创建了自己的模块,并复制了特定方法 (_getHtml),其中创建了类别链接的标签并尝试添加标题标签。我认为我做的一切都是正确的,但是刷新页面后我从未看到标题出现。
我刷新/清理了缓存,升级并刷新了静态文件。
这是我的模块中的 Block/Widgets 文件(最初位于 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;
}
}
这是我的 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>
这是我的 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>
有什么想法为什么这不起作用吗?这是我第一次进行这样的覆盖。
谢谢!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
我认为您需要在模块块中添加父块的所有依赖项。