2

I'm trying to add some text (label) to product page. Following works for me:

        <referenceContainer name="product.info.main">
        <block class="Magento\Framework\View\Element\Text" name="my.custom.label" after="-">
            <arguments>
                <argument translate="true" name="text" xsi:type="string">My Custom Text</argument>
            </arguments>
                </block>
    </referenceContainer>

and given below doesn't:

 <referenceBlock name="product.info.review">
        <block class="Magento\Framework\View\Element\Text" name="my.custom.label" after="-">
            <arguments>
                <argument translate="true" name="text" xsi:type="string">My Custom Text</argument>
            </arguments>
                </block>
    </referenceBlock>

Any explanation why?!

2

Im not sure if you've found the solution for this, but I've had the exact same problem. And its a core featured inside of Magento. If you look inside of:

/vendor/magento/framework/View/Layout/etc/elements.xsd

You'll see what elements you can use and what you can pass through to these elements. If your current block...

product.info.review

Has arguments you can't reference that block and pass arguments to it, the bound is 1 and 1 only. So no more arguments allowed, even if you have another block inside.

<xs:element ref="arguments" minOccurs="0" maxOccurs="1"/>
        <xs:element ref="block" minOccurs="0"/>
        <xs:element name="container" type="containerType" minOccurs="0"/>
        <xs:element ref="referenceBlock" minOccurs="0" />
        <xs:element ref="referenceContainer" minOccurs="0"/>
        <xs:element ref="uiComponent" minOccurs="0" />

This is around line 307 there are plenty more occurrences of it, through out this file.

The work around I have come up with is the following:

So I wanted to add a title to the short description inside of:

product.info.main

I tried like you did to reference the block but you can't, but i still tried with the following:

<referenceBlock name="product.info.overview">
        <block class="Magento\Framework\View\Element\Text" name="title.description.section">
            <arguments>
                <argument translate="true" name="text" xsi:type="string">
                    <![CDATA[<h3>Description</h3>]]>
                </argument>
            </arguments>
        </block>
    </referenceBlock>

You can see here the block is referenced and then I wanted to load the title block inside of this, no luck due to this block already having arguments to it. So I looked to the dev docs for some help.

https://devdocs.magento.com/guides/v2.2/frontend-dev-guide/layouts/xml-instructions.html

Dev docs were and weren't a help. Yes they say you can pass argument to the reference block but what it doesn't tell you is if that block is already set with an argument you can't add more.

What i did was the following:

<container name="description.section" htmlTag="div" htmlClass="description" after="product.info.extrahint">
            <block class="Magento\Framework\View\Element\Text" name="title.description.section">
                <arguments>
                    <argument translate="true" name="text" xsi:type="string">
                        <![CDATA[<h3>Description</h3>]]>
                    </argument>
                </arguments>
            </block>
        </container>

Created a container with my block in it.

<move element="product.info.overview" destination="description.section" after="-"/>

Move the other block to that container.

Hope this helps.

  • 1
    Interesting.. can't remember how I got around the issue back then but thanks for the detailed insight, it will definitely be helpful in future – Devtype Oct 17 '18 at 9:28
  • Brilliant answer!!... – Himanshu Dec 18 '18 at 13:42
1

Basically it is depend on define tag in layout.

If define as <container name="..." /> can be accessed by referenceContainer.

If declared as <block .... /> can be referenced by referenceBlock.

Create a container

Use the following sample to create (declare) a container:

<container name="some.container" as="someContainer" label="Some Container" htmlTag="div" htmlClass="some-container" />

Reference a container

To update a container use the instruction.

Example: add links to the page header panel.

<referenceContainer name="header.panel">
        <block class="Magento\Framework\View\Element\Html\Links" name="header.links">
            <arguments>
                <argument name="css_class" xsi:type="string">header links</argument>
            </arguments>
        </block>
</referenceContainer>

Create a block

Blocks are created (declared) using the instruction.

Example: add a block with a product SKU information.

<block class="Magento\Catalog\Block\Product\View\Description" name="product.info.sku" template="product/view/attribute.phtml" after="product.info.type">
    <arguments>
        <argument name="at_call" xsi:type="string">getSku</argument>
        <argument name="at_code" xsi:type="string">sku</argument>
        <argument name="css_class" xsi:type="string">sku</argument>
    </arguments>
</block>

Reference a block

To update a block use the instruction.

Example: pass the image to the logo block.

<referenceBlock name="logo">
        <arguments>
            <argument name="logo_file" xsi:type="string">images/logo.png</argument>
        </arguments>
</referenceBlock>
  • Thanks for the reply but I'm not sure above examples clarifies much. I can reference a container and add a block to it e.g. a string but I'm not able to reference a container such as "product.info.sku" and add a string to it, is it that you can't reference an existing block and add further blocks to it by extending the layout or there something I'm completely missing!! – Devtype Oct 21 '16 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.