How to override template with block class in Magento 2.2.1
-
15-04-2021 - |
Question
I want to override product main page title of product view page with block class.
So I use referenceBlock page.main.title
with class like this
<referenceBlock class="Magento\Catalog\Block\Product\View" name="page.main.title" template="Vendor_Module::html/title.phtml"/>
But Magento 2.2.1 shows error like this: not allow attrbute class
with referenceBlock
So how can i achieve this in Magento 2.2.1?
Solution 3
I found the solution
I just keep same name of block ad change both class and template.
Code:
<block class="Vendor\Module\Block\Product\View" name="page.main.title" template="Vendor_Module::html/title.phtml" before="product.info.main"/>
OTHER TIPS
Use this :
<referenceBlock name="page.main.title">
<action method="setTemplate">
<argument name="template" xsi:type="string">Vendor_Module::html/title.phtml</argument>
</action>
</referenceBlock>
To override class using di.xml
:
<preference for="Magento\Catalog\Block\Product\View" type="Vendor\ModuleName\Block\Product\View" />
Hope it Helps!
Use this:
<referenceBlock name="page.main.title">
<block class="Magento\Catalog\Block\Product\View" name="abc" template="Vendor_Module::html/title.phtml"/>
</referenceBlock>
As the error clearly says, you cannot use class
attribute within referenceBlock
tag.
You are referencing the name of the existing block with already defined class.
So just lose class='....'
part and it will work, like so
<referenceBlock name="page.main.title" template="Vendor_Module::html/title.phtml" />
Also, i couldnt make it work with passing template as argument.
Best of luck