Removing a Specific Link from ‘top.links’ in Magento Header
Are you currently facing an issue where your Magento 2 header dropdown includes unwanted links? This is a common problem faced by many developers, especially when third-party extensions add their own links to the header navigation.
In your case, you have identified that the “sell” link was added by a marketplace module. The code snippet you provided shows how this link is being rendered:
<div class="dropdown dropdown-toplinks">
<button class="dropdown-toggle" type="button" data-toggle="dropdown"><i class="pe-7s-user"></i></button>
<div class="dropdown-menu">
<?php echo $block->getChildHtml('top.links') ?>
</div>
</div>
This code references the ‘top.links’ block, which contains all the links displayed in the dropdown menu. However, as you correctly noted, removing this specific “sell” link from your default.xml file doesn’t seem to work:
<referenceBlock name="advanced-search-link" remove="true"/>
<referenceBlock name="skip_to_content" remove="true"/>
<referenceBlock name="authentication-popup" remove="true"/>
<referenceBlock name="navigation.sections" remove="true"/>
The issue lies in the fact that ‘top.links’ is not a standard Magento block but rather a composite block created by your marketplace module. This means you cannot simply remove it using default.xml.
To resolve this, let’s explore two approaches:
Approach 1: Overriding the Template
The first method involves overriding the template file responsible for rendering the ‘top.links’ block. This requires a deeper understanding of Magento’s theme and layout system.
First, navigate to your custom theme directory (e.g., app/design/frontend/{vendor}/{theme}). Next, create a new folder called Magento_Theme if it doesn’t exist already. Inside this folder, locate or create the file templates/html/top.links.phtml.
In this file, you can manually remove the “sell” link by modifying the PHP code as needed. For example:
<?php
$links = $block->getLinks();
foreach ($links as $link) {
if ($link['id'] !== 'your-sell-link-id') {
echo '<li><a href="' . $link['url'] . '">' . __($link['label']) . '</a></li>';
}
}
?>
Remember to replace 'your-sell-link-id' with the actual ID of your “sell” link.
Approach 2: Customizing the Block
The second approach involves creating a custom block that extends the original ‘top.links’ block but excludes the unwanted “sell” link. This requires more advanced knowledge of Magento’s object manager and dependency injection.
In your custom module (e.g., app/code/{Vendor}/{Module}), create a file called 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">
<type name="Magento\Theme\Block\Html\Toplinks">
<arguments>
<argument name="links" xsi:type="array">
<item name="sell" xsi:type="string">sell</item>
</argument>
</arguments>
</type>
</config>
This configuration removes the “sell” link from the ‘top.links’ block.
To implement this approach, you would need to create a custom module and configure your theme to use it. For detailed instructions on creating a custom module, refer to Laravel Company’s guide on Magento 2 Module Development.
Conclusion
In summary, removing a specific link from the ‘top.links’ block in Magento requires either overriding the template file or customizing the block itself. Both approaches require a solid understanding of Magento’s theme and layout system.
Remember to always backup your files before making changes, as direct modifications can sometimes lead to issues if not handled correctly.
If you find yourself struggling with such tasks frequently, consider seeking assistance from professionals at Laravel Company. Our team specializes in Magento development and is ready to help you overcome any technical challenges.
In the meantime, feel free to explore more of our blog posts on Magento 2 customization and best practices.