Magento 2 : How to check multiple if config in layout?
-
09-10-2020 - |
Question
Here is my code to add toplinks in my custom module
<referenceBlock name="header.links">
<block class="Magento\Framework\View\Element\Html\Link" ifconfig="hello/general/enable" name="hello" >
<arguments>
<argument name="label" xsi:type="string">hello</argument>
<argument name="path" xsi:type="string" translate="false">hello/index/index</argument>
</arguments>
</block>
</referenceBlock>
I want to add toplinks only if both option are true.
How to add one more condition for ifconfig="hello/general/toplinkenable"
with this block or is there any alternate solution to check both conditions to add toplinks?
Solution
ifconfig
supports only one value.
In order to solve your problem, I suggest creating your own block that extends Magento\Framework\View\Element\Html\Link
and add the conditions in the _toHtml
method.
Something like this:
<?php
namespace Vendor\Module\Block;
class Link extends \Magento\Framework\View\Element\Html\Link
{
public function _toHtml()
{
if (
!$this->_scopeConfig->isSetFlag('hello/general/enable') ||
!$this->_scopeConfig->isSetFlag('hello/general/toplinkenable')
) {
return '';
}
return parent::_toHtml();
}
}
then include your block in the layout
Instead of this
<block class="Magento\Framework\View\Element\Html\Link" ifconfig="hello/general/enable" name="hello" >
use this
<block class="Vendor\Module\Block\Link" name="hello" >
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange