Can I safely remove code in an extending theme file that redefines block from the original core files?

magento.stackexchange https://magento.stackexchange.com/questions/159328

  •  06-10-2020
  •  | 
  •  

Question

this is related to several qestions that I have found on Stackexchange, including one I posted myself, as well as what I have read in the Magento 2.1 docs. But I'm a bit worried about consequences.

I have employed the use of a theme in my Magento 2.1.2 store. The Magento_Catalog module in the theme extends the catalog_product_view.xml file from the base Magento theme. There are snippets of code in the extending file that are the exact pieces of code from the Magento core files, and are causing my site to throw exceptions over and over and over. Below the is the code:

Magento Core:

<block class="Magento\Catalog\Block\Product\View\Options" name="product.info.options" as="product_options" template="product/view/options.phtml">
    <block class="Magento\Catalog\Block\Product\View\Options\Type\DefaultType" as="default" template="product/view/options/type/default.phtml"/>
    <block class="Magento\Catalog\Block\Product\View\Options\Type\Text" as="text" template="product/view/options/type/text.phtml"/>
    <block class="Magento\Catalog\Block\Product\View\Options\Type\File" as="file" template="product/view/options/type/file.phtml"/>
    <block class="Magento\Catalog\Block\Product\View\Options\Type\Select" as="select" template="product/view/options/type/select.phtml"/>
    <block class="Magento\Catalog\Block\Product\View\Options\Type\Date" as="date" template="product/view/options/type/date.phtml"/>
</block>

Theme code:

<block class="Magento\Catalog\Block\Product\View\Options" name="product.info.options" as="product_options" template="product/view/options.phtml">
    <block class="Magento\Catalog\Block\Product\View\Options\Type\DefaultType" as="default" template="product/view/options/type/default.phtml"/>
    <block class="Magento\Catalog\Block\Product\View\Options\Type\Text" as="text" template="product/view/options/type/text.phtml"/>
    <block class="Magento\Catalog\Block\Product\View\Options\Type\File" as="file" template="product/view/options/type/file.phtml"/>
    <block class="Magento\Catalog\Block\Product\View\Options\Type\Select" as="select" template="product/view/options/type/select.phtml"/>
    <block class="Magento\Catalog\Block\Product\View\Options\Type\Date" as="date" template="product/view/options/type/date.phtml"/>
</block>

There are other snippets of code that are the exact same as well, but this block is the only area that is throwing the exception. Exception is:

[2017-02-07 22:17:38] main.CRITICAL: Magento\Framework\Exception\LocalizedException: The element 'product.info.options' already has a child with alias 'file' in /home/{username}/public_html/vendor/magento/framework/Data/Structure.php:611

...with exceptions for each of the alias (as="") names "default", "text", "file", "select", "date". The other alias names in the extending file code are not throwing exceptions for some reason.

  1. Why are these alias names ("default", "text", "file", "select", "date") throwing exceptions, but the others alias names that are also in the core code do not?
  2. Can I remove this code from the extending file safely? I tried changing the alias names, but I was worried that it may be called somewhere that I don't know and mess up the storefront.

I have found a few questions regarding alias name and some say that the alias name can be "anything" but IF it is called somewhere by getChildHtml then won't there be a problem?

I have also read the Docs about extending and I have a feeling the theme code writer is not correct in re-writing the same block code (but, why did they do it?)

Thanks in advance

Was it helpful?

Solution

The issue with these blocks specifically is that they don't have a name specified. The name is what Magento uses to uniquely identify blocks in the layout.

In this case, when you're extending it, Magento doesn't know to overwrite the parent block and tries to add another block with the same alias.

There is nothing wrong with the theme code. This is an issue with Magento's code. Name is a required attribute for all blocks and containers.

I've opened a pull request to fix the issue: https://github.com/magento/magento2/pull/8514/files

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top