If you are building a module and you encounter a block that cannot be referenced by name then you probably need to create a plugin to overwrite the template. This can be achieved in three simple steps.
STEP 1 :
Copy telephone.phtml template to your custom module (in my case it’s Bajaj_Customer)
/vendor/magento/module-customer/view/frontend/templates/widget/telephone.phtml
to
/app/code/Bajaj/Customer/view/frontend/templates/widget/telephone.phtml
Do the changes according to your need.
STEP 2 :
Now we need to create a Plugin class.
/app/code/Bajaj/Customer/Plugin/Customer/Block/Widget/Telephone.php
<?php namespace Bajaj\Customer\Plugin\Customer\Block\Widget; class Telephone { public function beforeToHtml(\Magento\Customer\Block\Widget\Telephone $subject) { $subject->setTemplate('Bajaj_Customer::widget/telephone.phtml'); } }
STEP 3 :
Update di.xml
/app/code/Bajaj/Customer/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"> <type name="Magento\Customer\Block\Widget\Telephone"> <plugin name="module_customer_block_widget_telephone_override_template" type="Bajaj\Customer\Plugin\Customer\Block\Widget\Telephone"/> </type> </config>
Similarly you can override any templates defined in the widget folder.
Thank you 🙏 !