3

I have created a custom category attribute and it saves correctly in the database.

I am now trying to add this attribute's value to the topmenu. So I have created Topmenu.php in my module (m4tthg0_catcolor) so I can override the _getMenuItemClasses. I have tried several ways to get the attribute value but it always return NULL:

namespace m4tthg0\catcolor\Block;
use Magento\Framework\DataObject\IdentityInterface;
use Magento\Framework\View\Element\Template;
use Magento\Framework\Data\TreeFactory;
use Magento\Framework\Data\Tree\Node;
use Magento\Framework\Data\Tree\NodeFactory;
/**
* Html page top menu block
*/

class Topmenu extends \Magento\Theme\Block\Html\Topmenu


{    

protected function _getMenuItemClasses(\Magento\Framework\Data\Tree\Node $item)
{
    $classes = [];

    $classes[] = 'level' . $item->getLevel();
    $classes[] = $item->getPositionClass();

    if ($item->getIsFirst()) {
        $classes[] = 'first';
    }

    if ($item->getIsActive()) {
        $classes[] = 'active';
    } elseif ($item->getHasActive()) {
        $classes[] = 'has-active';
    }

    if ($item->getIsLast()) {
        $classes[] = 'last';
    }

    if ($item->getClass()) {
        $classes[] = $item->getClass();
    }

    if ($item->hasChildren()) {
        $classes[] = 'parent';
    }

    $classes [] =$item->getData('color_menu');

    return $classes;
 }
}

How would you do that ?

Magento version: 2.1CE

1 Answer 1

9

I know this is an old question but I just worked on a solution for this. So if anyone else is having this issue...

Have a look at the

Magento\Catalog\Plugin\Block\Topmenu

This is where Magento fills category tree for the menu.

Since this one is a plugin I decided to disable it and add a new plugin with same content (+my modifications) in my module.

Below should go in

m4tthg0\Catcolor\etc\di.xml

Disable the Magento plugin first.

<type name="Magento\Theme\Block\Html\Topmenu">
    <plugin name="catalogTopmenu" disabled="true" />
</type>

Then add your plugin

<type name="Magento\Theme\Block\Html\Topmenu">
    <plugin name="catalogTopmenuRewrite" type="m4tthg0\Catcolor\Plugin\Block\Topmenu"/>
</type>

Then create the file

m4tthg0\Catcolor\Plugin\Block\Topmenu.php

and copy everything from

Magento\Catalog\Plugin\Block\Topmenu.php

Make sure to adjust the namespace in your file.

Now let's add additional content from category collection. Have a look at the method

getCategoryTree($storeId, $rootId)

see how it selects the "name" to be added to tree data. Similarly you can add your custom attribute.

Inside getCategoryTree(),

$collection->addAttributeToSelect('custom_attribute');

Then have a look at the method

getCategoryAsArray($category, $currentCategory)

You can add your selected custom attribute here to tree data. Just add below array item.

'custom_attribute' => $category->getData('custom_attribute')

That's it!!

Go to your Topmenu.php (m4tthg0\catcolor\Block\Topmenu.php) and check inside Node($item) data. Your custom attribute value should be there.

5
  • I know the answer is very old, but I can confirm that it works like a charm.
    – M4tth
    Commented Jan 17, 2018 at 2:02
  • Great!! Help me to get CustomUrl category attribute in TopMenu.php
    – Sourav
    Commented Feb 2, 2018 at 11:07
  • Yeh this will work but you aren't extending topmenu, you're completely replacing it with your own version of it.
    – zigojacko
    Commented Apr 16, 2020 at 15:21
  • 1
    I don't like this solution, but it works. Thanks :)
    – Zankar
    Commented Jul 22, 2020 at 9:58
  • Thanks for your reply and solution. You can work on a preference for this plugin and change the methods but as the method getCategoryAsArray is private, then it makes things not that good. So either way it won't be an ideal solution. Commented Jan 5 at 18:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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