6

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?

asked Dec 13, 2017 at 6:48

5 Answers 5

7

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!

answered Dec 13, 2017 at 7:04
3
  • 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? Commented 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. Commented Dec 13, 2017 at 9:51
  • Then you have to create new block and remove old one. Commented Dec 13, 2017 at 10:12
1

Use this:

<referenceBlock name="page.main.title">
 <block class="Magento\Catalog\Block\Product\View" name="abc" template="Vendor_Module::html/title.phtml"/>
</referenceBlock>
answered Dec 13, 2017 at 6:58
1
  • 1
    Thanks for answer. But this will add new block in page.main.title . I need to override title.phtml with block class Commented Dec 13, 2017 at 7:00
1

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"/>
answered Dec 13, 2017 at 10:03
1
  • This isn't working for me. Commented Aug 18, 2021 at 2:47
0

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

answered Jan 26, 2021 at 17:04
0

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>
answered Jul 12, 2022 at 9:29

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.