There is a hereditary relationship between the themes (inheritance fallback mechanism) in Magento 2. This connection gives the advantage of easy editing and maintaining the themes during Magento 2 development. For example, you can create a theme with certain decorations, based on the basic design. Instead of copying the extensive theme files and changing what you want to change, you can add overriding and extending files.
The essence of the inheritance fallback mechanism is that if the view files are not found in the current theme, the system looks for these files in parent themes, modules or libraries.
Parent & Child theme
Parent theme is a common theme, e.g. Magento_blank theme, which comes with Magento 2 and does not have its parent theme. Or it’s a theme from which all the files for the child theme will be inherited.
A child theme is a theme whose files are inherited from a Parent theme. A Parent theme is specified in the child theme.xml file:
/app/design/frontend/(vendor)/(theme_name)/theme.xml
(<parent>Magento/Blank</parent>).
1 2 3 4 5 6 7 8 |
<?xml version="1.0"?> <theme xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Config/etc/theme.xsd"> <title>My Theme</title> <parent>Magento/blank</parent> <media> <preview_image>media/preview.jpg</preview_image> </media> </theme> |
Unlike Magento 1, the number levels of inheritance in Magento 2 is not restricted. Let’s take a look at several examples of how to extend or override child theme files.
Layout files
The processing mechanism for layout files in the fallback system is not involved. The system collects layout in the following order:
- Current theme layouts: <theme_dir>/<Vendor>_<Module>/layout/
- Ancestor themes layouts, starting from the most distant ancestor, recursively until a theme with no parent is reached: <parent_theme_dir>/<Vendor>_<Module>/layout/
- Module layouts for the frontend area: <module_dir>/view/frontend/layout/
- Module layouts for the base area: <module_dir>/view/base/layout/
Unlike templates files, layout files can be easily supplemented. All you need is to create a new .xml file in the child theme in the layout folder of the required module.
For example, if you want to move the page title to the content block on the product catalog page, you need to create the catalog_product_view.xml file at \app\design\frontend\vendor_name\theme_name\Magento_Catalog\layout\catalog_category_view.xml and add the following content:
1 2 3 4 5 6 |
<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <move element="page.main.title" as="page.main.title" destination="content" before="-"/> </body> </page> |
This will extend the layout, without overriding any other rules.
But if you still need to override the .xml file, you need to create a file with the same name as the overridden file at:
theme_dir/vendor_module/layout/override/theme/vendor_name/<ancestor_theme>
Template Files
The fallback scheme for templates is the following:
- Current theme templates: <theme_dir>/<Namespace>_<Module>/templates
- Ancestors themes templates, recursively, until a theme with no ancestor is reached: <parent_theme_dir>/<Namespace>_<Module>/templates
- Module templates: <module_dir>/view/frontend/templates
If you need to override a template file, for example, a product directory file, you need to copy the file:
Magento_Catalog_module_dir\view\frontend\templates\product\list.phtml
along the way:
app\design\frontend\vendor_name\theme_name\Magento_Catalog\templates\product\list.phtml
and make the necessary changes to it. The same applies to static files (js, css, images, fonts and .less).
The specific directories, in which the system searches during the backup, depend on whether the module context for the file is known.
If the module context is not defined for the file:
- Current theme static files for a specific locale (the locale set for the storefront): theme_dir/web/i18n/<locale>
- Current theme static files: theme_dir/web/
- Ancestor’s static files, recursively, until a theme with no parent is reached:
- parent_theme_dir/web/i18n/<locale>
- parent_theme_dir/web/
- Library static view files: lib/web/
If the module context is defined:
- Current theme and current locale module static files: theme_dir/Namespace_Module/web/i18n/<locale>/
- Current theme module static files theme_dir/Namespace_Module/web/. Example: app/design/frontend/BelVg/BelvgTheme/Magento_Catalog/web/
- Ancestor themes module static files, recursively, until a theme with no ancestor is reached:
- parent_theme_dir/Namespace_Module/web/i18n/<locale>/
- parent_theme_dir/Namespace_Module/web/
- Module static view files for the current locale and frontend area: module_dir/view/frontend/web/i18n/<locale>/
- Module static view files for the current locale and base area: module_dir/view/base/web/i18n/<locale>/
- Module static view files for the frontend area: module_dir/view/frontend/web/
- Module static view files for the base area: module_dir/view/base/web/
That’s it, thanks for reading. Hope the article has provided a deeper insight into the theme structure of Magento 2.