Update SubBlock of ReferenceContainer does not work as expected and it appears that update by ReferenceBlock from Admin is a missing feature/bug
Originally I tried the below: Note: I used the blocktype of Magento\Catalog\Block\Navigation as it contained access to the current categories children and other logic I needed.
Set Custom Layout Update to:
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 below layout update works from a visual standpoint, and is not correct from a coding duplication stand point.
<referenceContainer name="content">
<referenceBlock name="category.products">
<block name="product_list" class="Magento\Catalog\Block\Navigation">
<action method="setTemplate">
<argument name="template" xsi:type="string">Magento_Catalog::product/subcat-list.phtml</argument>
</action>
</block>
</referenceBlock>
</referenceContainer>
However, this throws the following error in the exception logs:
Magento\Framework\Exception\LocalizedException: The element 'category.products' already has a child with alias 'product_list' in /var/www/signatureneedle_magento/html/vendor/magento/framework/Data/Structure.php:611
IIRC it only started giving that message here after upgrade to 2.1.3
Element 'referenceBlock': This element is not expected. Expected is one of ( referenceContainer, container, update, move )
Was a bit unexpected to see a validation error for a previously working piece of Custom Layout Update. I thought according to semver "patch" versions were only supposed to add backwards-compatible changes :)
Note that this same issue applies to product page layout XML updates in the admin as well.
@scottsb, thanks for the detailed references. This is a highly used feature of Magento, I can't imagine we are the only ones with this issue.
@magento-admin @olysenko Can someone please help get this into the right hands to be looked at.
Also see this ticket: https://github.com/magento/magento2/issues/2247
I'm surprised that wrapping the code inside a referenceContainer tag works - even if that container doesn't exist:
<referenceContainer name="blabla">
<referenceBlock name="homepage.hero.content">
<action method="setTitle">
<argument name="title" xsi:type="string">Hi there.</argument>
</action>
</referenceBlock>
</referenceContainer>
While this is clearly wrong from a coding standpoint, I'm not getting any warnings/errors in system.log or exception.log. It's also working visually.
Last ticket was closed because it wasn't a technical issue. However, how can a layout update XML field not do the single most important thing you expect from it? It also seems to work fine if you wrap it in a dummy referenceContainer clause, pointing out that there's nothing wrong with the order in which XML is loaded.
@duffner I'm having these thoughts quite often when I encounter (to me) obvious issues.. Layout update XML simply doesn't work.. that previous ticket is 2 years old, and this one is nearly 1 year old.. How are not more people having this issue?
I have nearly the same problem.
If I set a list.phtml for a category in the backend of magento 2 it wont show up.
I think this problem is still there.
<referenceContainer name="content">
<referenceBlock name="category.products.list">
<arguments>
<argument name="template" xsi:type="string">Magento_Catalog::product/list_souvenirs.phtml</argument>
</arguments>
</referenceBlock>
</referenceContainer>
@erfanimani @duffner
While tackling the (probably same) problem I found out that updating the XML via admin seems to work only, if the referenceContainer has not been rewritten via a custom layout xml file.
So if you've rewritten the <referenceContainer name="content"> in a copied catalog_category_view.xml (or a separate default.xml in your module) then the layout update via admin won't work. Make sure you do the layout update/rewrite for a particular block either via a separate XML file _or_ via admin.
This is true.
But I still think this is a bug.
It's strange that it still works for other elements in my custom theme like the <referenceBlock name="page.main.title">.
I can change the "id" of this page.main.title within an update in the backend.
But Im not able to do this for the category.product.list if it is still inside my custom xml.
I have follow you'r instruction and it works but I don't think that this should be a final solution for fixing the problem.
A "bonus bug" is the phantasy <referenceContainer name="blabla">. See comment above from @erfanimani.
I can call it with this referenceContainer around and inside a real referenceBlock and it will work.
Now the question is, how do we get a @magento-admin, @veloraven to get this resolved, or at least acknowledged.
Has anyone checked whether this bug still exists on 2.2RC? Often times I find myself looking up a bug that has already been resolved months ago, but hasn't made it into an official release.
I only checked it in the M2.1.7.
If I have time I will check it but maybe someone is faster than me ;)
@erfanimani and @Hopfrosch, don't you have a ton of extra time with how much faster front-end development is with M2? :| haha just kidding, well a little. I can try and take a look this upcoming weekend but no promises.
Yes I have a bunch of time, that's why I'm here and can easy talk to you ;) thx magento!
It would be nice if you can try it and share your information about the ultimate M2.2 experience. =)
@duffner, thank you for your report.
We were not able to reproduce this issue by following the steps you provided. If you'd like to update it, please reopen the issue.
We tested the issue on 2.1.9
Most helpful comment
@erfanimani @duffner
While tackling the (probably same) problem I found out that updating the XML via admin seems to work only, if the
referenceContainerhas not been rewritten via a custom layout xml file.So if you've rewritten the
<referenceContainer name="content">in a copiedcatalog_category_view.xml(or a separatedefault.xmlin your module) then the layout update via admin won't work. Make sure you do the layout update/rewrite for a particular block either via a separate XML file _or_ via admin.