Whenever we have created two di.xml file for different area like,
one di.xml file in etc base folder and one di.xml file are call inside {area}/di.xml file.
Final outcomes are come from base di.xml file not from area di.xml
file.
Below result are outcomes of override files,
This code for override topmenu block,
app/code/Package/Module/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Package_Module',
__DIR__
);
app/code/Package/Module/etc/di.xml file,
<?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="Package\Module\Block\Topmenu"/>
</config>
same Topment.php file override by area file like in frontend di.xml file,
app/code/Package/Module/etc/frontend/di.xml file,
<?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="Package\Module\Block\Fronttop"/>
</config>
app/code/Package/Module/Topmenu.php
<?php
namespace Package\Module\Block;
class Topmenu extends \Magento\Theme\Block\Html\Topmenu
{
protected $categoryFactory;
protected $_filterProvider;
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Catalog\Model\CategoryFactory $categoryFactory,
\Magento\Framework\Data\Tree\NodeFactory $nodeFactory,
\Magento\Framework\Data\TreeFactory $treeFactory,
\Magento\Cms\Model\Template\FilterProvider $filterProvider,
array $data = []
){
echo 'final file call base area...';
$this->categoryFactory = $categoryFactory;
$this->_filterProvider = $filterProvider;
parent::__construct($context,$nodeFactory,$treeFactory,$data);
}
public function test1(){
//code goes here...
}
}
app/code/Package/Module/Fronttop.php
<?php
namespace Package\Module\Block;
class Fronttop extends \Magento\Theme\Block\Html\Topmenu
{
protected $categoryFactory;
protected $_filterProvider;
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Catalog\Model\CategoryFactory $categoryFactory,
\Magento\Framework\Data\Tree\NodeFactory $nodeFactory,
\Magento\Framework\Data\TreeFactory $treeFactory,
\Magento\Cms\Model\Template\FilterProvider $filterProvider,
array $data = []
){
echo 'final file call frontend area...';
$this->categoryFactory = $categoryFactory;
$this->_filterProvider = $filterProvider;
parent::__construct($context,$nodeFactory,$treeFactory,$data);
}
public function test1(){
//code goes here...
}
}
After checking inside frontend,
you have to see at top, message are display from construct with final file call base area..
from etc/di.xml
file override method. in our case Fronttop.php
file;
Here we have create two di.xml file with calling same Topmenu.php file and final result are comes from base di.xml file.
Override file using frontend di.xml in our case, Package\Module\Block\Topmenu.php
file is not call at all.