如何有条件地布局中删除阻止或容器编程?

问题描述 投票:2回答:2

如果任何一个要删除容器(块)等基于某些条件或产品从产品详细信息页面product.info.main具有分配的值属性。那么什么是实现这一目标的最佳方法?

谢谢

layout rendering block magento2 magento2.2
2个回答
2
投票

我们可以使用事件观察器的方式...

在YOUR_VENDOR \ YOUR_MODULE \等\前端\ events.xml文件,需要添加下面的代码:

<event name="layout_generate_blocks_after">
    <observer name="personalize-theme-pdp-customize" instance="YOUR_VENDOR\YOUR_MODULE\Observer\ApplyThemeCustomizationObserver" />
</event>

而在YOUR_VENDOR \ YOUR_MODULE \观察员\ ApplyThemeCustomizationObserver.php文件,需要添加下面的代码:

public function execute(Observer $observer)
{
    $action = $observer->getData('full_action_name');
    if ($action !== 'catalog_product_view') {
        return;
    }

    $product = $this->_registry->registry('product');

    if ($product) {
        $attribute = $product->getCustomAttribute('g3d_app_url_default');
        if ($attribute && $attribute->getValue()) {
            /** @var \Magento\Framework\View\Layout $layout */
            $layout = $observer->getData('layout');
            $layout->unsetElement('product.info.main');
        }
    }
}

会......这将有助于有人...

谢谢,


0
投票

使用站点范围内的事件从一个特定的页面删除容器/块是矫枉过正,而不是最好的方法,因为你的病情都会在每一个页面加载进行评估,增加了少量的开销到所有页面。

从一个特定的页面删除容器/块最好是通过创建要去除容器/块页面控制器的after方法的execute插件来实现的。用这种方法加载预期页面时,才会执行您的病情。

public function afterExecute(\Magento\[Module]\Controller\[ControllerName] $subject, $result)
{
    if ([your condition]) {
        $result->getLayout()->unsetElement('name_of_container_or_block');
    }

    return $result;
}
© www.soinside.com 2019 - 2024. All rights reserved.