Magento

Magento 2 add ifconfig in override block XML

Written by Jigar Patel

Sep 27

Magento 2 add ifconfig in override block XML

The use of ifconfig is for condition which is coming from system config values. The value of this will be 0 and 1(boolean). According to that value, the block will be rendered. Here we learn about in Magento 2 add ifconfig in override block XML.

Any block can be configured to show or not based on a Magento/Config/Model/Config/Source/Yesno system configuration field, using the ifconfig argument. For the value, use the XPath to the needed field.

[dt_code]<block class=”Namespace\Module\Block\Type” name=”block.example” ifconfig=”my/yesno/field”/>[/dt_code]

There are three ways to set the template for a block:

  • using the template attribute
  • using the <argument> instruction
  • using the <action method="setTemplate"> instruction

XPath to the system configuration field. E.g. “section/group/field” (company_users/password/enabled).

I want to add ifconfig in override block XML in Magento 2

Ifconfig will show template only when it has the value true, it does not work as else condition. if you use in else condition then i suggest you creating a helper function and add the conditions in the helper method.

so please follow below steps.

Step:1 Create  Data.php File

Create Data.php at app/code/Custom/Module/Helper/Data.php

<?php

namespace Custom\Module\Helper;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    /**
    * Configuration paths enable module
    */
    const XML_PATH_COMPANY_USERS_PASSWORD_ENABLED = 'company_users/password/enabled';
    /**
    * @var ScopeConfigInterface
    */
    protected $_scopeConfig;

    public function __construct
    (
    	ScopeConfigInterface $scopeConfig
    ) {
        $this->_scopeConfig = $scopeConfig;
    }

    public function getTemplate()
    {
    	$enabledModule = $this->_scopeConfig->getValue(self::XML_PATH_COMPANY_USERS_PASSWORD_ENABLED,ScopeInterface::SCOPE_STORE);
    	
        if ($enabledModule) {
            $template =  'Custom_Module::company/customer/add.phtml';
        } else {
            $template = 'Magento_Company::company/management/dialog/customer/add.phtml';
        }

        return $template;
    }
}

In the above helper class  code , is specified according to the following:

[dt_code] $this->_scopeConfig->getValue ( $path,$scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT,$scopeCode = null ) [/dt_code]

  • $path: The path through the tree of configuration values, e.g., ‘general/store_information/name’
  • $scopeType: The scope to use to determine config value, e.g., ‘store’ or ‘default’
  • $scopeCode

Step:2 Include your block in the layout Instead of this

<referenceBlock name="red.block.name">
    <action method="setTemplate">
        <argument name="template" xsi:type="helper" helper="Custom\Module\Helper\Data::getTemplate"></argument>
    </action>
</referenceBlock>

In the above layout code , is specified according to the following:

  • setTemplate: The highest priority template is one with setTemplate action <action method=”setTemplate”>.Second priority has the attribute specified as <referenceBlock name=”…” template=”…”/>, and the lowest priority has the template using <argument>.
  • helper: helper attribute you define a helper class method.Data is a helper class and getTemplate is a method name that return a template.

If you have any queries regarding this post, use the comment section below!

I hope this help you.

Keep sharing !!

Written by Jigar Patel

Our Magento Store

Do not miss a chance to grab exciting offers on Magento Development at Dolphin Web Solution. We are providing discounts on various Magento services this season. Go and grab yours today at our Magento Store.

Multiple Wishlist for Magento 2

Multiple Wishlist for Magento 2

₹ 3106

Wallet and Reward Points for Magento 2

Wallet and Reward Points for Magento 2

₹ 9476

RMA for Magento 2

RMA for Magento 2

₹ 11865

₹ 14254
Abandoned Cart Email for Magento 2

Abandoned Cart Email for Magento 2

₹ 6291

Simple Details on Configurable Product for Magento 2

Simple Details on Configurable Product for Magento 2

₹ 7883

₹ 9476
Frequently Bought Together for Magento 2

Frequently Bought Together for Magento 2

₹ 5494

₹ 7087

Let's Start Your Project

Get free consultation for your digital product idea to turn it into reality!

Copyright © 2023 DOLPHIN WEB SOLUTION. All rights reserved.

TO TOP