通常应该使用<action />
tag 来完成:
<referenceContainer name="content">
<action method="unsetChild" ifconfig="dashboard/settings/remove">
<argument xsi:type="string">dashboard</argument>
</action>
</referenceContainer>
编辑:
唯一的问题是unsetChild只接受别名。您不能使用块名称。
其他解决方案:重写Magento Framework,使其能够将ifconfig与remove =“ true”一起使用
1-创建自己的模块。
2 -添加一个新的文件覆盖Magento的框架:(例如:/Vendor/Module/Override/Magento/Framework/View/Layout/Reader/Block.php
)
namespace Vendor\Module\Override\Magento\Framework\View\Layout\Reader;
use Magento\Framework\App;
use Magento\Framework\Data\Argument\InterpreterInterface;
use Magento\Framework\View\Layout;
/**
* Block structure reader
*/
class Block extends \Magento\Framework\View\Layout\Reader\Block
{
/**
* @var \Magento\Framework\App\ScopeResolverInterface
*/
protected $scopeResolver;
/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
protected $scopeConfig;
/**
* Constructor
*
* @param Layout\ScheduledStructure\Helper $helper
* @param Layout\Argument\Parser $argumentParser
* @param Layout\ReaderPool $readerPool
* @param InterpreterInterface $argumentInterpreter
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Framework\App\ScopeResolverInterface $scopeResolver
* @param string|null $scopeType
*/
public function __construct(
Layout\ScheduledStructure\Helper $helper,
Layout\Argument\Parser $argumentParser,
Layout\ReaderPool $readerPool,
InterpreterInterface $argumentInterpreter,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Framework\App\ScopeResolverInterface $scopeResolver,
$scopeType = null
) {
parent::__construct($helper,
$argumentParser,
$readerPool,
$argumentInterpreter,
$scopeType
);
$this->scopeConfig = $scopeConfig;
$this->scopeResolver = $scopeResolver;
}
protected function scheduleReference(
Layout\ScheduledStructure $scheduledStructure,
Layout\Element $currentElement
) {
$elementName = $currentElement->getAttribute('name');
$elementRemove = filter_var($currentElement->getAttribute('remove'), FILTER_VALIDATE_BOOLEAN);
if ($elementRemove) {
$configPath = (string)$currentElement->getAttribute('ifconfig');
if (empty($configPath)
|| $this->scopeConfig->isSetFlag($configPath, $this->scopeType, $this->scopeResolver->getScope())
) {
$scheduledStructure->setElementToRemoveList($elementName);
}
} else {
$data = $scheduledStructure->getStructureElementData($elementName, []);
$data['attributes'] = $this->mergeBlockAttributes($data, $currentElement);
$this->updateScheduledData($currentElement, $data);
$this->evaluateArguments($currentElement, $data);
$scheduledStructure->setStructureElementData($elementName, $data);
}
}
}
3-添加di.xml文件以覆盖magento文件:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Framework\View\Layout\Reader\Block"
type="Vendor\Module\Override\Magento\Framework\View\Layout\Reader\Block" />
</config>
4-现在您可以在布局中将ifconfig与remove结合使用:
<referenceBlock name="content" remove="true" ifconfig="path/to/myconfig" />
此示例适用于Block,但是如果您覆盖/Magento/Framework/View/Layout/Reader/Container.php的方法containerReference(),则可以对容器执行相同的操作