Magento 2 Theme – Modify Layout Guide

layoutlocal.xmlmagento2themexml

In Magento 1, themes had a special file named local.xml. Through this file, a Magento 1 theme could directly modify the layout without needing to replace a specific layout update XML file.

Does Magento 2 have a similar mechanism? I know a Magento 2 theme can replace a layout handle XML file from a module — what I want to know is does Magento 2 have a way for a theme to add layout rules that are specific to that theme.

If not, are there classes/methods provided that would allow a module developer to only add certain layout rules when a certain theme is added?

Best Answer

You have two options, you can either extend or overwrite the existing XML.

Extending

To extend an existing layout in a similar fashion to local.xml in Magento 1 you need to add an XML file in a location like this:

<theme_dir>
 |__/<Namespace>_<Module>
   |__/layout
     |--<layout1>.xml
     |--<layout2>.xml

Where <layout1> and <layout2> are your layout handles, for example:

<your_theme_dir>/Magento_Catalog/layout/catalog_product_view.xml

Official documentation can be found here - http://devdocs.magento.com/guides/v2.0/frontend-dev-guide/layouts/layout-extend.html

Overwriting

Overwriting XML is done in the same way, it's just the file location that changes. You can overwrite the base module's XML or the parent theme's XML, the file location determines which is overwritten.

To overwrite the base module's XML place your XML file in:

<theme_dir>
  |__/<Namespace_Module>
    |__/layout
      |__/override
         |__/base
           |--<layout1>.xml
           |--<layout2>.xml

To overwrite the parent theme's XML place your XML file in:

<theme_dir>
  |__/<Namespace_Module>
    |__/layout
      |__/override
         |__/theme
            |__/<Parent_Vendor>
               |__/<parent_theme>
                  |--<layout1>.xml
                  |--<layout2>.xml

Official documentation can be seen here - http://devdocs.magento.com/guides/v2.0/frontend-dev-guide/layouts/layout-override.html

Related Topic