0

I have override a template file Magento_Payment::info/default.phtml using following method this template is declared only in

/Magento2/vendor/magento/module-payment/Block/Info.php

file.

The below code works fine, but i want to know how it actually works as i did not find toHtml method in the above file.

Any thoughts ?

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">
    <type name="Magento\Payment\Block\Info">
        <plugin name="module_paympent_view_override_template" type="Vendor\Module\Plugin\Payment\Block\Info" />
    </type>
</config>

Info.php

<?php
namespace Vendor\Module\Plugin\Payment\Block;
 
class Info
{
    public function beforeToHtml(\Magento\Payment\Block\Info $subject)
    {        
            $subject->setTemplate('Vendor_Module::info/default.phtml');        
    }
}
1
  • any thoughts on this ?
    – devhs
    Commented Mar 16, 2021 at 11:55

2 Answers 2

1

toHtml function defined in "Magento/Framework/View/Element/Template.php" file in which "Magento/Payment/Block/Info.php" file extends. Check below screenshot: enter image description here

2
  • Can we use it on plugin beforeToHtml ?
    – devhs
    Commented Mar 16, 2021 at 12:08
  • Thanks for the answer.
    – devhs
    Commented Mar 16, 2021 at 12:22
1

You're "plugging in" to the toHtml method called inside the \Magento\Payment\Block\Info, this generates an interceptor class which intercepts all calls that can be intercepted and searches any associated plugins for methods defined with the before, after, or around pattern. So when the \Magento\Payment\Block\Info block calls toHtml (which is most definitely defined in its parent class \Magento\Framework\View\Element\Template=> \Magento\Framework\View\Element\AbstractBlock) the interceptor will intercept it and call beforeToHtml.
For more information I would recommend reading this documentation for how plugins work and their limitations

1
  • Thanks for the answer.
    – devhs
    Commented Mar 16, 2021 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.