Magento2: referenceBlock cannot be used in CMS update?

Created on 2 Nov 2015  路  11Comments  路  Source: magento/magento2

I'm trying to remove the page title in the Design tab of a CMS page by using this xml code :
<referenceBlock name="page.main.title" remove="true"/>

But after saving, it says :
_Please correct the XML data and try again. Element 'referenceBlock': This element is not expected. Expected is one of ( referenceContainer, container, update, move ). Line: 1_

The xml does work, tried it out in the cms_index_index.xml. Using referenceContainer is not correct in this case.

Bug or am I missing something?

Thanks.

Most helpful comment

Hello,

Like I said, the referenceContainer version works just fine, referenceBlock isn't.
That's why I was (am) thinking this is a technical issue, as there's no real reason why you should'nt be able to reference blocks on a single CMS page.

The Magento Community forum is rather dead, to be honest, I've posted there before, never got a reply.

All 11 comments

I have not checked, but I suspect the issue is CMS pages are only intended to deal with reorganizing the container tree, so only container releated commands have been included. If you put it in a layout file, they can manipulate blocks.

(I don't know the details, so I will go out on a limb and say I suspect it is related to the sequencing of processing. We start with skeleton container tree, then add in all the content. I suspect the CMS instructions are executed before all the blocks are added in, so they are not possible to reference yet. Adding into the other layout file is executed later, when all the blocks are loaded. This may not be 100% correct, but I believe it is something like that.)

@debuysserk I believe layout XML there must include the node which is used in the layout files per urn:magento:framework:View/Layout/etc/page_configuration.xsd

The GitHub issue tracker is intended for technical issues only. Please refer to the Community Forums or Magento Stack Exchange site for technical questions. In your case, the programming questions forum is likely the most appropriate. Feel free to reopen this issue if you think you have encountered a bug in Magento 2.

Hello,

Like I said, the referenceContainer version works just fine, referenceBlock isn't.
That's why I was (am) thinking this is a technical issue, as there's no real reason why you should'nt be able to reference blocks on a single CMS page.

The Magento Community forum is rather dead, to be honest, I've posted there before, never got a reply.

Is this going to be added? It's quite an important feature.

@debuysserk have you solved?

I wrote my own solution to this problem since I really needed to reference a block in a layout update for a category. I ended up using di.xml to replace the file "MagentoFrameworkViewModelLayoutUpdateValidator".

I then added this at the end of the new constructor in my class after the call to parent:
$this->_xsdSchemas[self::LAYOUT_SCHEMA_PAGE_HANDLE] = $urnResolver->getRealPath(
'urn:magento:module:Vti_RelaxedLayoutUpdates:etc/page_layout.xsd'
);

I also had to make my own verion of page_layout.xsd which adds this additional line to the "pageLayoutType" sequence tag:
xs:element ref="referenceBlock" minOccurs="0" maxOccurs="unbounded"

I'm not sure whey they don't allow referenceBlock in the core code, but I'm only using it for a very simple field update on category pages ("name"). This has not been stress-tested whatsoever, so use it at your own discretion.

@AirmanAJK If you read the layout types docs it says

A page layout file defines the page wireframe, for example, one-column layout. Technically page layout is an .xml file defining the structure inside the section of the HTML page markup. Page layouts feature only containers. All page layouts used for page rendering should be declared in the page layout declaration file.

By altering the page_layout.xsd you are basically creating an hybrid between the _Page layout_ and the _Generic Layout_

To include this in the core I think Magento would have to introduce a new functionality that allows to add a _Generic Layout update_ in the DB via Admin - which I don't think is possible at the moment.

In case any other devs are looking for a quick-and-dirty answer to allow blocks to be removed via custom XML, this is my (terrible) solution:
http://magento.stackexchange.com/questions/120146/remove-block-using-admin-custom-layout-update-xml/120319#120319

@alankent, this is a missing feature of Magento 1. If you have Container->SubBlock1->SubBlock1A in order to do a simple set template on SubBlock1A you have to duplicate the SubBlock1 level in your override. @davidalger, this is an issue, just because you think something is a question it's a question often times because Magento 2 is missing a Magento 1 feature. If this is so very possible, try something simple, set a new template to this stock block. Inside of a single category within the Magento Admin.

<block class="Magento\Catalog\Block\Product\ListProduct" name="category.products.list" as="product_list" template="Magento_Catalog::product/list.phtml">

This should be re-opened (or at least reevaluated). It's clearly not intended nor expected behaviour.. How can a layout update XML field not do the single most important thing you expect from it? Things like setting/unsetting values on blocks, changing templates, removing blocks, etc.

Besides, it seems to work fine if you wrap it in a dummy referenceContainer clause:

<referenceContainer name="blabla">
    <referenceBlock name="homepage.hero.content">
        <action method="setTitle">
            <argument name="title" xsi:type="string">Hi there.</argument>
        </action>
    </referenceBlock>
</referenceContainer>

Also see https://magento.stackexchange.com/questions/136748/please-correct-the-xml-data-and-try-again-element-referenceblock-this-elemen

Can I tag someone from Magento?

edit: there's an open ticket about the same issue here: https://github.com/magento/magento2/issues/7510

Was this page helpful?
0 / 5 - 0 ratings