在Magento2中以编程方式删除块

在Magento2中以编程方式删除块,magento2,Magento2,我们可以使用unsetBlock()方法删除Magento1中的块,但在Magento2中它不起作用。所以,请帮助我们如何以编程方式删除Magento2中的块?使用unsetElement()方法删除块。 差不多 您需要尝试这种方法,例如,我正在从侧边栏中删除比较,以便将default.xml覆盖到app/design/frontend/Your_Theme/Theme_name/Magento_Catalog/layout <?xml version="1.0"?&g

我们可以使用
unsetBlock()
方法删除Magento1中的块,但在Magento2中它不起作用。所以,请帮助我们如何以编程方式删除Magento2中的块?

使用
unsetElement()
方法删除块。 差不多


您需要尝试这种方法,例如,我正在从侧边栏中删除比较,以便将
default.xml
覆盖到
app/design/frontend/Your_Theme/Theme_name/Magento_Catalog/layout

<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<body>
    <referenceBlock name="catalog.compare.sidebar" remove="true"/>
</body>

要从页面中删除特定块,请打开自定义布局xml并将下面的代码放在body标记下

    <?xml version="1.0"?>
    <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body><referenceBlock name="Your_Block" remove="true"/>
    </body>
    </page>


将此块更改为块名需要删除

理想情况下,有不同的方法来执行此操作。最好的方法是使用布局文件

1) 如果您构建了一个模块,您可以在app/code/Namespace/Your_module/view/frontend/layout/frontname_controllername_controlleraction.xml中创建一个布局,它是一个xml文件,并添加以下代码

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <referenceBlock name="block_name" remove="true" />
</page>

2) 如果您还没有创建自己的自定义模块,只需在app/design/frontend/custom_-Theme/Theme_-name/module_-name/layout中编写自定义xml,然后添加以下代码即可

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <referenceBlock name="block_name" remove="true" />
</page>


我正试图像这样删除
联系人表单
,但没有成功…@GielBerkers:您是如何在块或控制器中删除的。你能分享你的代码吗?
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <referenceBlock name="block_name" remove="true" />
</page>