Magento Asked by Bhaumik Upadhyay on December 26, 2021
I read about the Parameter configuration inheritance
in Magento documentation but not able to understand the concept properly!
Can anyone please guide and provide a detailed explanation with an example?
Many many thanks!
I have worked on an example for this inheritance issue you are interested in.
If you look at this class MagentoFrameworkViewElementTemplate
, you will see one of its parameter is $_logger
and although this parameter is a convenient way to log message when debugging and so on, it is common to find your log file contains so many messages that it makes your task to debug more complicated than you'd want.
With that in mind, I have created a new Block class called MbsBlockWithCustomLoggerBlockARandomBlock
and within this class, I have added a dependency like below
class ARandomBlock extends MagentoFrameworkViewElementTemplate
implements MbsBlockWithCustomLoggerBlockWithCustomLoggerInterface
{
private $customLogger;
public function __construct(
TemplateContext $context,
MbsBlockWithCustomLoggerCustomLoggerInterface $customLogger,
array $data = []
) {
parent::__construct($context, $data);
$this->customLogger = $customLogger;
}
...
then in my di.xml I have defined what is the class to use for customLogger.
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="MbsBlockWithCustomLoggerBlockARandomBlock">
<arguments>
<argument name="customLogger" xsi:type="object">MbsBlockWithCustomLoggerLoggerGeneric</argument>
</arguments>
</type>
..
finally, the logger class is MbsBlockWithCustomLoggerLoggerGeneric
and this file will log its debug message on a custom file
class LoggerGeneric implements MbsBlockWithCustomLoggerCustomLoggerInterface
{
private static $defaultLogFile = 'blockgeneric.log';
protected $logFile = null;
..
At this point, we have now a way for our custom code to log its debug info on a file that is different than system.xml.
I assume you have seen the above is a use of the devdocs step1 you have read and would like info about.
Then, to illustrate step 2, I have also created another Block class
class BlockWithLogging1 extends MbsBlockWithCustomLoggerBlockARandomBlock
implements MbsBlockWithCustomLoggerBlockWithCustomLoggerInterface
{
}
..
If I was using my class doing nothing else in my di.xml file, for this second block, its debug message would go to the same file blockgeneric.log.
However, if I add a definition in my di.xml like
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="MbsBlockWithCustomLoggerBlockARandomBlock">
<arguments>
<argument name="customLogger" xsi:type="object">MbsBlockWithCustomLoggerLoggerGeneric</argument>
</arguments>
</type>
<type name="MbsBlockWithCustomLoggerBlockBlockWithLogging1">
<arguments>
<argument name="customLogger" xsi:type="object">MbsBlockWithCustomLoggerLoggerType1</argument>
</arguments>
</type>
</config>
and the log class like below
class LoggerType1 extends MbsBlockWithCustomLoggerLoggerGeneric
{
protected $logFile = 'blocktype1.log';
}
then this new block will use another logger class.
This illustrates the same concept as the devdocs article. I have a full module at https://bitbucket.org/magstaging/blockwithcustomlogger/src/master/ that you can try and experiment to verify the above works in your head too.
Great question, thanks
Answered by Herve Tribouilloy on December 26, 2021
I found another answer in the below post with an explanation and example by @Ryan.
More: Configured for a class type parameters are configured for all of their descendants automatically. And descendants are able to override parameters configured for the supertype.
Answered by Bhaumik Upadhyay on December 26, 2021
As per this example
<type name="MagentoFrameworkViewElementContext">
<arguments>
<argument name="urlBuilder" xsi:type="object">MagentoCoreModelUrl</argument>
</arguments>
</type>
This above configuration means That the class 'MagentoFrameworkViewElementContext' configures all instances of MagentoFrameworkViewElementContext which set in their contractor and also from here pass in 'MagentoCoreModelUrl' instance as $urlBuilder in their constructors.
<type name="MagentoBackendBlockContext">
<arguments>
<argument name="urlBuilder" xsi:type="object">MagentoBackendModelUrl</argument>
</arguments>
</type>
This above means:
already magento extend 'MagentoBackendBlockContext' class from 'MagentoFrameworkViewElementContext'
Answered by Ritesh Santra on December 26, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP