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?
5 Answers 5
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!
-
Thanks for answer. But I also want to change block class of referanceBlock. How can I change both block class and template with reference block?Rahul– Rahul2017年12月13日 08:39:33 +00:00Commented Dec 13, 2017 at 8:39
-
I know that how override block with di.xml. But I want just override template with custom block class with referenceBlock.Rahul– Rahul2017年12月13日 09:51:19 +00:00Commented Dec 13, 2017 at 9:51
-
Then you have to create new block and remove old one.Ronak Chauhan– Ronak Chauhan2017年12月13日 10:12:25 +00:00Commented Dec 13, 2017 at 10:12
Use this:
<referenceBlock name="page.main.title">
<block class="Magento\Catalog\Block\Product\View" name="abc" template="Vendor_Module::html/title.phtml"/>
</referenceBlock>
-
1Thanks for answer. But this will add new block in page.main.title . I need to override title.phtml with block classRahul– Rahul2017年12月13日 07:00:33 +00:00Commented Dec 13, 2017 at 7:00
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"/>
-
This isn't working for me.Nick Rolando– Nick Rolando2021年08月18日 02:47:17 +00:00Commented Aug 18, 2021 at 2:47
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
Seems like you can do both on latest version of Magento straight from a layout :
<referenceBlock name="customer_form_register" class="Vendor\Customer\Block\Form\Register">
<action method="setTemplate">
<argument name="template" xsi:type="string">Vendor_Customer::form/register.phtml</argument>
</action>
</referenceBlock>