2

I have set up a custom category setting called 'enable_megamenu'.

The code of that you can find here

My question now is i also have a Plugin in my module and i was wondering how i can now check the value there. Do i need to make a Helper or how does this work? I have done it for normal custom setting before but never for these category settings.

Can someone help me out with that?

I thank you so much in advance!

edit : clarification. So idealy i want to do a

if ($enablemenu == 1) {

Edit 2 : Current Plugin MegaMenu.php is giving a blank page :

<?php

namespace Designit\Megamenu\Plugin;

class MegaMenu
{

    public function aroundGetHtml(\Magento\Theme\Block\Html\Topmenu $topmenu, $html)
    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category
        $enablemenu  = $category->getData('enable_megamenu');

        if (!empty($enablemenu )) {
            //your code
        } else {
            // Something else
        }
    }

}

Edit 3 : I'm trying to get the top menu and overwrite it when enable_megamenu is enabled. Code so far :

<?php

namespace Designit\Megamenu\Plugin;
use \Magento\Framework\Data\Tree\Node;

class MegaMenu
{
    protected $_registry;


    public function __construct(\Magento\Framework\Registry $registry,
                                \Magento\Framework\Data\Tree\Node $menuTree) {
        $this->_menuTree = $menuTree;
        $this->_registry = $registry;
    }

    public function getCurrentCategory()
    {
        return $this->_registry->registry('current_category');
    }

// Get cms page or current category page??
//return $this->_registry->registry('current_category');
//        return $this->_registry->registry('current_cms_page');


    public function afterGetHtml(\Magento\Theme\Block\Html\Topmenu $topmenu, $html)
    {

        $children = $this->_menuTree->getChildren();

        foreach ($children as $child) {
            $enablemenu = $this->getCurrentCategory()->getData('enable_megamenu');

            if (!empty($enablemenu)) {
                // if mega menu is enabled in the backend
            } else {
                // Else
            }
        }
    }

}

This gives me the next error : Exception #0 (BadMethodCallException): Missing required argument $data of Magento\Framework\Data\Tree\Node.

Edit 4 :

I'm able to edit the menu now. I just need to know how to call for the option (if it is enabled or not)

code :

Vendor\Module\Block\Html\Topmenu.php

<?php
namespace Vendor\Module\Block\Html;

use Magento\Framework\Data\Tree\NodeFactory;
use Magento\Framework\Data\TreeFactory;
use Magento\Framework\View\Element\Template;

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

    public function __construct(Template\Context $context, NodeFactory $nodeFactory, TreeFactory $treeFactory, array $data = [])
    {
        parent::__construct($context, $nodeFactory, $treeFactory, $data);
    }

    protected function _getHtml(
        \Magento\Framework\Data\Tree\Node $menuTree,
        $childrenWrapClass,
        $limit,
        $colBrakes = []
    )
    {
        $html = '';

        $children = $menuTree->getChildren();
        $parentLevel = $menuTree->getLevel();
        $childLevel = $parentLevel === null ? 0 : $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 (count($colBrakes) && $colBrakes[$counter]['colbrake']) {
                $html .= '</ul></li><li class="column"><ul>';
            }

            $html .= '<li ' . $this->_getRenderedMenuItemAttributes($child) . '>';
            $html .= '<a href="' . $child->getUrl() . '" ' . $outermostClassCode . '><span>' . $this->escapeHtml(
                    $child->getName()
                ) . '</span></a>' . $this->_addSubMenu(
                    $child,
                    $childLevel,
                    $childrenWrapClass,
                    $limit
                ) . '</li>';
            $itemPosition++;
            $counter++;
        }

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

        return $html;
    }

}

?>

Vendor\Module\etc\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="Magento\Theme\Block\Html\Topmenu" type="Vendor\Module\Block\Html\Topmenu" />
</config>
1
  • I am out of ideas to be honnest. This is a part of magento that i still need to learn. Anyone a clue how i can override the menu with my own custom mega menu when that option is enabled? Commented Jul 20, 2017 at 20:37

1 Answer 1

1

Try the below code

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category
$enablemenu  = $category->getData('enable_megamenu');

if (!empty($enablemenu )) {
    //your code
} else {
    // Something else
}

Hope it will work.

Comment by SwAt

To get the ID (in Topmenu.php) i used

        $id = preg_replace("/[^0-9]/", "", $child->getId());

Then its import not to use !empty because if you enable it and disable it again it gets a value but its 0 so change the code to validate :

        if ($getContent == 1) {}
8
  • Thanks for the comment. Do i need this in a Data helper? Or just in my Plugin? I come from Wordpress development mostly and i'm trying to figure this out :D Sorry Commented Jul 20, 2017 at 12:10
  • I added a new edit with the current code Commented Jul 20, 2017 at 12:12
  • Did you get value from $enablemenu ?
    – Vino
    Commented Jul 20, 2017 at 12:47
  • at the moment i just get a blank page when i use that code in my plugin MegaMenu.php Commented Jul 20, 2017 at 15:03
  • I editted the original post with what i'm trying now Commented Jul 20, 2017 at 15:37

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.