How to Add "Use Default Value" Checkbox in UI Form of Online Store?

On an online store, there are numerous variations of default value checkbox, especially in UI form. Magento offers to use default value checkbox in UI forms. Having a default value checkbox is a very useful factor especially when there are multiple store views.

The default value checkbox indicates that in another store view if the value is different or not compare to the default store view. But when we develop a custom module, there are “Use Default Value” checkbox not provided by UI form. We need to add such a checkbox programmatically.

When we develop a custom EAV module, we need to manage the data on the basis of a distinct store view. So, if admin changes data in one then, it becomes difficult to identify the modification of the data. This is when the default value checkbox is useful.

Let’s follow the steps to add this functionality to a custom module.

First of all, I assume that you have created simple module with UI form. Now, you need to open your UI form data provider file. Before that, let’s confirm your UI form data provider file.

1) Firstly, Open your UI form XML file and find below code :

<dataSource name="md_helloworld_helloworld_form_data_source">
<argument name="dataProvider" xsi:type="configurableObject">
<argument name="class" xsi:type="string">MD\Helloworld\Ui\Component\Form\Helloworld\DataProvider</argument>
<argument name="name" xsi:type="string">md_helloworld_helloworld_form_data_source</argument>
<argument name="primaryFieldName" xsi:type="string">entity_id</argument>
<argument name="requestFieldName" xsi:type="string">entity_id</argument>
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="submit_url" xsi:type="url" path="*/*/save"/>
</item>
</argument>
</argument>
<argument name="data" xsi:type="array">
<item name="js_config" xsi:type="array">
<item name="component" xsi:type="string">Magento_Ui/js/form/provider</item>
</item>
</argument>
</dataSource>

2) Here, you can see that path of file in class argument. Just open that UI form data provider file and paste this below code :

<?php
namespace MD\Helloworld\Model\Ui\Component\Form\Helloworld;
use MD\Helloworld\Model\ResourceModel\Helloworld\CollectionFactory;
class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
{
protected $loadedData;
public function __construct(
$name,
$primaryFieldName,
$requestFieldName,
CollectionFactory $JobCollectionFactory,
array $meta = [],
array $data = []
) {
$this->collection = $JobCollectionFactory->create();
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
}
public function getData()
{
if (isset($this->loadedData)) {
return $this->loadedData;
}
$items = $this->collection->getItems();
foreach ($items as $value) {
$this->loadedData[$value->getId()] = $value->getData();
}
return $this->loadedData;
}
public function getMeta() {
$meta = parent::getMeta();
$meta['main_fieldset']['children']['status']['arguments']['data']['config']['service']['template'] = 'ui/form/element/helper/service';
$meta['main_fieldset']['children']['status']['arguments']['data']['config']['disabled'] = 1;
return $meta;
}
}

You can see here in DataProvider file that there are getMeta() function add to display “Use Default Value” checkbox in UI Form.  You also need to replace below values based on your requirement

  • main_fieldset => Your Fieldset name
  • status => Your Field Name

That’s it !!! Now, Just clean the cache and check it in your form.

I hope all your doubts related to developing a default checkbox are cleared here. If not, contact us and get further assistance from our expert and certified Magento 2 developers.