- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Question about Override block from design ?
Is it possible to override a block (in my case i need to extend a core Block) from app/design/frontend/Vendor/ThemeName (theme folder) or do i have to do it from app/code/Vendor/ModuleName? And if so can anyone provide a simple example still new to magento.
I would like to do it this way because i would like it to be part of the theme.
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Question about Override block from design ?
Hello @awes_ome
You have to create your own module under app/code folder. For that you have to create some file :
app/code/Vendor/Module/etc/module.xml
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Vendor_Module" setup_version="1.0.0"></module> </config>
app/code/Vendor/Module/registration.php
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Vendor_Module', __DIR__ );
After that we have to use preference to override the class in Magento 2.
app/code/Vendor/Module/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"> <preference for="Magento\Theme\Block\Html\Topmenu" type="Vendor\Module\Block\Html\Topmenu" /> </config>
app/code/Vendor/Module/Block/Html/Topmenu.php
<?php namespace Vendor\Module\Block\Html; class Topmenu extends \Magento\Theme\Block\Html\Topmenu { protected function _addSubMenu($child, $childLevel, $childrenWrapClass, $limit) { } }
Hope it will work for you.
If my answer is useful, please Accept as Solution & give Kudos.