Removing Blocks in Magento Using Layout XML Files
In this guide, we’ll dive into a common task in Magento development: removing an existing block from the layout using XML files. Specifically, we’re going to remove the “currency” block that’s currently being inserted into the “top.switches” block via the directory.xml file.
Understanding the Current Setup
Let’s first understand what we have right now:
- The
defaultlayout configuration contains a reference to thetop.switchesblock. - Inside this reference, there is a
blockelement of type “directory/currency,” named “currency”, and set before the “store_language” block. - This currency block uses the template “directory/currency.phtml”.
- Additionally, there’s another block “optional_zip_countries” referenced in the
headsection.
Our goal is to remove this specific “currency” block from the layout without affecting other elements or the overall theme structure.
Creating a Custom Layout XML File
The first step is to create a custom layout XML file where we’ll define our modifications. This approach keeps your changes separate from Magento’s core files, ensuring they don’t get overwritten during updates.
Create a new file named remove_currency_block.xml in the following directory:
app/design/frontend/{your_package}/{your_theme}/layout
Defining the Changes
Open this new XML file and add the following code:
<?xml version="1.0"?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layouts.xsd">
<default>
<reference name="top.switches">
<remove name="currency"/>
</reference>
</default>
</layout>
Breaking Down the Code
- The
<?xml version="1.0"?>line specifies the XML version. - The
<layout>element is the root container for our layout updates. - Inside this, we define a new
defaultscope, which applies to all pages unless overridden by more specific scopes. - The
<reference name="top.switches">section targets the “top.switches” block specifically. - Finally, the
<remove name="currency"/>line instructs Magento to remove the “currency” block from this reference.
Configuring the Custom Layout XML
Now, we need to tell Magento to use our custom layout file instead of the core directory.xml for the currency block.
- Open your theme’s
theme.xmlfile located at:
app/design/frontend/{your_package}/{your_theme}/etc/theme.xml
- Add or modify the
<layout>section as follows:
<layout>
<default>
<reference name="directory_currency">
<action method="setTemplate">
<argument name="template" xsi:type="string">{$block->getModuleDirectory('Magento_Directory')}/currency.phtml</argument>
</action>
</reference>
<!-- Add this line -->
<update handle="remove_currency_block"/>
</default>
</layout>
- Save the
theme.xmlfile.
Testing the Changes
Clear your cache and refresh your Magento site:
php bin/magento cache:clean
php bin/magento cache:flush
Navigate to a page where you expect to see the “currency” block, such as the header or top menu area. If everything is set up correctly, you should no longer see this specific currency switcher.
Conclusion
In this guide, we demonstrated how to remove an existing Magento block using layout XML files. This approach allows for cleaner code and easier maintenance compared to directly editing core files.
Remember that changing the layout requires a basic understanding of Magento’s architecture and how it processes layout XML files. If you’re unfamiliar with any part of the process, feel free to explore in-depth resources on Laravel Company for more detailed explanations.
As a final note, always ensure you have a backup before making significant changes to your live site’s codebase. This will protect your data and provide a safety net if anything goes wrong during the testing process.
Happy coding!