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