Magento2: Programmatically Create Custom Layout XML

This article shows how you can create a custom layout XML handle programmatically in your custom module in Magento 2. I will be using event observer for this. app/code/Company/Module/etc/frontend/events.xml <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> <event name="layout_load_before"> <observer name="mycompany_custom_layout" instance="Company\Module\Model\Observer\CustomLayout" /> </event> </config> app/code/Company/Module/Observer/CustomLayout.php Here, I am checking if a customer is logged in or not … Read more

Magento: Programmatically Remove Layout Block

This article shows how you can remove layout blocks through the PHP Program instead of working in the layout.xml file in Magento 2. I will be using event observer. app/code/Company/Module/etc/frontend/events.xml <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> <event name="layout_generate_blocks_after"> <observer name="remove_block" instance="Company\Module\Model\Observer\RemoveBlock" /> </event> </config> app/code/Company/Module/Observer/RemoveBlock.php Here, I am checking if a customer is logged in or … Read more

Magento: How to change default page layout?

Here, I will show you how you can change the default page layout in Magento. By page layout, I mean all CMS page, category page, etc. There are different ways to change the page layout in Magento. Here are some of them:- Changing individual category page layout You can modify the page layout of each … Read more

Magento: Set/Change page layout, title tag, meta keywords and description

Earlier in this blog, I had written about setting title, keywords, and description from xml layout file. In this article, I will be showing, how you can set/change title, keywords and description of any page programmatically. I mean, by php code. Here is the layout XML file. I have set title and template in the … Read more