---
title: "Magento 2 add ifconfig in override block XML"
url: "https://dolphinwebsolution.com/magento-2-add-ifconfig-in-override-block-xml"
date: "2021-09-27T07:39:22+05:30"
modified: "2023-08-25T09:46:09+05:30"
author:
  name: "Jigar Patel"
categories:
  - "Magento"
word_count: 503
reading_time: "3 min read"
summary: "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 ..."
description: "The use of ifconfig is for condition which is coming from system config values. Here we learn about in Magento 2 add ifconfig in override block XML."
keywords: "Magento 2 add ifconfig, Magento"
language: "en"
schema_type: "Article"
related_posts:
  - title: "5 Efficient PWA (Progressive Web Apps) Providers for Brand"
    url: "https://dolphinwebsolution.com/five-efficient-pwa-progressive-web-apps-providers-for-your-brand"
  - title: "Add Buttons into Ui Component  Form Magento 2"
    url: "https://dolphinwebsolution.com/add-buttons-into-ui-component-form-magento-2"
  - title: "Top 10 Shopware Development Companies in 2026"
    url: "https://dolphinwebsolution.com/blog/top-10-shopware-development-companies/"
---

# Magento 2 add ifconfig in override block XML

_Published: September 27, 2021_  
_Author: Jigar Patel_  

![](https://dolphinwebsolution.com/wp-content/uploads/2023/02/Magento-2-add-ifconfig-in-override-block-XML-1024x536.png)

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](https://github.com/magento/magento2/blob/2.4/app/code/Magento/Config/Model/Config/Source/Yesno.php) 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 !!


---

_View the original post at: [https://dolphinwebsolution.com/magento-2-add-ifconfig-in-override-block-xml](https://dolphinwebsolution.com/magento-2-add-ifconfig-in-override-block-xml)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-08-26 07:38:55 UTC_  
