I'm trying to add some code to protected function _getHtml (in Topmenu.php).

I've tried both the "preference" and the "plugin" angle, but still can't properly override override the function. Information I've found online seems lacking.

What's the best way to do this, in this particular case? Any thoughts/examples would be greatly appreciated!


Alright, so the route I went that ended up working:

-Using preference. So in di.xml: <preference for="Magento\Theme\Block\Html\Topmenu" type="My\Module\Block\Html\Topmenu" />

-In My/Module/Block/Html/Topmenu.php, I have:

namespace My\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

[functions I'm overriding]


And then the thing that REALLY stumped me was that I had to reset the template for the block in the layout:

  <referenceBlock name="catalog.topnav">
    <action method="setTemplate">
      <argument name="template" xsi:type="string">Magento_Theme::html/topmenu.phtml</argument>

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.