Maintaining Active State of Custom Menu Items in Magento
Adding custom menu items to your Magento site is a common task when you want to extend the default navigation options provided by the platform. However, maintaining the active state or highlighting the current page in your new menu items can be challenging.
The Current Approach
The initial approach involves adding these custom menu items directly into the topmenu.phtml file using Magento’s CMS block feature. This is done by creating two blocksâone for the “HOME” link and one for additional menu linksâand inserting them into the appropriate positions in the menu structure:
<nav id="nav">
<ol class="nav-primary">
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('home-link')->toHtml(); ?>
<?php echo $_menu; ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('post-menu-links')->toHtml(); ?>
</ol>
</nav>
While this successfully adds the new menu items, it doesn’t automatically highlight the active page. To achieve this, you might have tried using JavaScript or jQuery to set a cookie when a user clicks on a menu item, but as you’ve discovered, this approach has several limitations:
- Real-time highlighting: If a user hovers over an item without clicking, the cookie won’t be set.
- Resetting state: The active state isn’t automatically reset when a user navigates to another page using your custom menu items.
- Cookie persistence: Previous cookies might remain active if a user returns later, leading to incorrect highlighting.
A More Reliable Solution
To overcome these challenges and create a robust solution that works consistently across all situations, we recommend leveraging Magento’s built-in functionality for handling active menu items.
Step 1: Create Custom Layout XML
In your theme or module directory (app/design/frontend/<Vendor>/<Theme>/layout), create a new file called custom_menu.xml with the following content:
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="topmenu">
<action method="setChild">
<block class="Magento\Framework\View\Element\Html\Link::class" after="-"/>
<arguments>
<name>label</name>
<args>
<string>HOME</string>
</args>
</arguments>
</action>
<action method="setChild">
<block class="Magento\Framework\View\Element\Html\Link::class" after="-"/>
<arguments>
<name>label</name>
<args>
<string>Additional Menu Item 1</string>
</args>
</arguments>
</action>
<!-- Add more action blocks as needed -->
</referenceBlock>
</body>
</page>
This layout XML file adds your custom menu items directly into the topmenu block using Magento’s built-in actions. The <action method="setChild"> tag is used to insert new links after all existing links in the topmenu.
Step 2: Update Your Template
With the changes above, you no longer need the two CMS blocks in your topmenu.phtml file. Instead, simply remove them:
<nav id="nav">
<ol class="nav-primary">
<?php echo $_menu; ?>
</ol>
</nav>
Step 3: Active Menu Item Handling
Now that the menu items are added directly into Magento’s topmenu block, the platform will automatically handle their active state. When a user navigates to a page associated with one of your custom menu items, Magento will apply the appropriate CSS classes (e.g., active) based on the current route.
Conclusion
Maintaining the active state for custom added top-menu items in Magento requires leveraging the platform’s built-in functionality rather than relying on external JavaScript or jQuery solutions. By creating a custom layout XML file, you can add your menu items directly into the topmenu block and let Magento handle the highlighting automatically.
Remember to flush your Magento cache after making these changes for them to take effect. You can do this in the Magento admin panel under “System” > “Cache Management”.
For more detailed guidance on customizing Magento menus, check out our comprehensive guide on the Laravel Company Blog: Mastering Magento Menu Customization.
This approach ensures consistency across all browsing scenarios and eliminates the issues associated with using cookies or external scripts. It’s a clean, reliable solution that aligns with Magento’s design principles.
Happy customizing!