I have a 3rd party extension installed and I needed to overwrite on block because I could not use events or plugins on it.
The block is added in the layout file like this:
<block class="Vendor\Module\Block" name="some-name" template="path/to/template.phtml" />
Notice that there is no module name specified for the template.
I've overwritten it with this in my di.xml
<preference for="Vendor\Module\Block" type="MyVendor\MyModule\Block" />
now when loading a certain page I get the error path/to/template.phtml
does not exist.
I know I can solve this by redeclaring the block in my own layout file and making the template Vendor_Module::path/to/module.phtml
and it will work but this is not upgrade proof.
If I upgrade the extension and the template to that specific block changes I will have problems.
My question: Is there an upgrade proof way to overwrite blocks and still keep the templates from the original module?
getTemplate()
you can keep the original module template if you will upgrade the moduleMagento\Framework\View\Element\Template
and add the "Vendor_Module::" to the configured template path if no vendor/module prefix is set.protected function _construct() { parent::_construct(); if ($this->hasData('template')) { if (strpos( $this->getData('template') , "::") === false){ $this->setTemplate("Vendor_Module::".$this->getData('template')); } } }