11

I cant set any default value for textfield or select in my custom admin new ui form. In edit form it works. I want to set system config values as default values.

Here is my Dataprovider.php

public function getData(){

if (isset($this->loadedData)) {
    return $this->loadedData;
}

if ($itemId)
    $items = $this->collection->getItems();

$data = $this->dataPersistor->get('form_item');
if (!empty($data)) {
   //Do something
} else {
   //When form init without any data
   // I want to setData for some field here
}

return $this->loadedData;
}

Here is my form.xml

<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">

    <argument name="data" xsi:type="array">
        <item name="js_config" xsi:type="array">
             <item name="provider" xsi:type="string">vendor_newform.vendor_newform_data_source</item>
             <item name="deps" xsi:type="string">vendor_newform.vendor_newform_data_source</item>
        </item>
        <item name="label" xsi:type="string" translate="true">Vendor Form</item>
        <item name="config" xsi:type="array">
            <item name="dataScope" xsi:type="string">data</item>
            <item name="namespace" xsi:type="string">vendor_newform</item>
    </item>
        <item name="template" xsi:type="string">templates/form/collapsible</item>
        <item name="buttons" xsi:type="array">
            <item name="save" xsi:type="string">Company\Module\Block\Adminhtml\Vendor\Edit\Button\Save</item>
            <item name="delete" xsi:type="string">Company\Module\Block\Adminhtml\Vendor\Edit\Button\Delete</item>
            <item name="back" xsi:type="string">Company\Module\Block\Adminhtml\Vendor\Edit\Button\Back</item>        
        </item>
    </argument>

    <dataSource name="vendor_newform_data_source">
         <argument name="dataProvider" xsi:type="configurableObject">
             <argument name="class" xsi:type="string">Company\Module\Model\ResourceModel\Store\DataProvider</argument>
             <argument name="name" xsi:type="string">vendor_newform_data_source</argument>
             <argument name="primaryFieldName" xsi:type="string">store_details_id</argument>
             <argument name="requestFieldName" xsi:type="string">id</argument>
             <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="submit_url" xsi:type="url" path="module/custom/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>

    <fieldset name="base_fieldset">
     <argument name="data" xsi:type="array">
         <item name="config" xsi:type="array">
             <item name="label" xsi:type="string" translate="true">Details</item>
             <item name="collapsible" xsi:type="boolean">false</item>
             <item name="sortOrder" xsi:type="number">10</item>
             <item name="openOnShow" xsi:type="boolean">true</item>
         </item>
     </argument>

    <field name="store_details_id">
         <argument name="data" xsi:type="array">
             <item name="config" xsi:type="array">
                 <item name="visible" xsi:type="boolean">false</item>
                 <item name="dataType" xsi:type="string">text</item>
                 <item name="formElement" xsi:type="string">input</item>
                 <item name="source" xsi:type="string">store_details_id</item>
             </item>
         </argument>
    </field>

    <field name="store_name">
         <argument name="data" xsi:type="array">
             <item name="config" xsi:type="array">
                 <item name="label" xsi:type="string">Store Name</item>
                 <item name="visible" xsi:type="boolean">true</item>
                 <item name="dataType" xsi:type="string">text</item>
                 <item name="formElement" xsi:type="string">input</item>
                 <item name="source" xsi:type="string">store_name</item>
                 <item name="required" xsi:type="boolean">true</item>
                 <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">true</item>
                 </item>
             </item>
         </argument>
     </field>
    <field name="store_url">
         <argument name="data" xsi:type="array">
             <item name="config" xsi:type="array">
                 <item name="label" xsi:type="string">Store URL</item>
                 <item name="visible" xsi:type="boolean">true</item>
                 <item name="dataType" xsi:type="string">text</item>
                 <item name="formElement" xsi:type="string">input</item>
                 <item name="source" xsi:type="string">store_url</item>
                 <item name="required" xsi:type="boolean">true</item>
                 <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">true</item>
                 </item>                

             </item>
         </argument>
    </field>

        <field name="support_phone">
             <argument name="data" xsi:type="array">
                 <item name="config" xsi:type="array">
                     <item name="label" xsi:type="string">Phone</item>
                     <item name="visible" xsi:type="boolean">true</item>
                     <item name="dataType" xsi:type="string">text</item>
                     <item name="formElement" xsi:type="string">input</item>
                     <item name="source" xsi:type="string">support_phone</item>
                     <item name="required" xsi:type="boolean">true</item>
                     <item name="validation" xsi:type="array">
                            <item name="required-entry" xsi:type="boolean">true</item>
                     </item>                
                 </item>
             </argument>
        </field>    

    </fieldset>

</form>
2
  • please share your ui xml code Dec 22, 2016 at 7:17
  • do you have got the solution for set system config values as default values? Please help...
    – Ashish Raj
    Apr 17, 2018 at 13:01

4 Answers 4

11

You need to add below code:

<item name="default" xsi:type="string">1</item>

For reference you can check category_form as below

    <field name="is_active">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="sortOrder" xsi:type="number">10</item>
                    <item name="dataType" xsi:type="string">boolean</item>
                    <item name="formElement" xsi:type="string">checkbox</item>
                    <item name="source" xsi:type="string">category</item>
                    <item name="prefer" xsi:type="string">toggle</item>
                    <item name="label" xsi:type="string" translate="true">Enable Category</item>
                    <item name="valueMap" xsi:type="array">
                        <item name="true" xsi:type="string">1</item>
                        <item name="false" xsi:type="string">0</item>
                    </item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">false</item>
                    </item>
                    <item name="default" xsi:type="string">1</item>
                </item>
            </argument>
        </field>
6
  • Thanks @ Prashant .It's working. I want this default value from system config. Dec 22, 2016 at 8:03
  • @JancyAbraham you find any solution ?
    – Ashar Riaz
    Apr 7, 2017 at 10:43
  • No,I didn't get any solution for this Apr 7, 2017 at 10:48
  • 1
    work perfect!!!
    – jruzafa
    Sep 7, 2018 at 9:41
  • 1
    Can we dynamically set a default value?
    – user00247
    Nov 28, 2018 at 8:49
2

If you want to use system.xml configuration you can create a custom field class:

ui component:

<field name="field_with_configurable_default_value" class="Vendor\Module\Ui\Component\Field\ConfigurableDefaultValue">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                ...
                <item name="defaultValueConfigPath" xsi:type="string">
                  path/to/system_xml_field
                </item>
            </item>
        </argument>
    </field>

Vendor\Module\Ui\Component\Field\ConfigurableDefaultValue:

<?php

namespace Vendor\Module\Ui\Component\Field;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Form\Field;

class ConfigurableDefaultValue extends Field
{
    private ScopeConfigInterface $scopeConfig;

    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        ScopeConfigInterface $scopeConfig,
        array $components = [],
        array $data = []
    ) {
        $this->scopeConfig = $scopeConfig;

        parent::__construct(
            $context,
            $uiComponentFactory,
            $components,
            $data
        );
    }

    public function getConfiguration(): array
    {
        $config = parent::getConfiguration();
        $config['default'] = $this->scopeConfig->getValue($config['defaultValueConfigPath']);

        return $config;
    }
}
1

We can set default value via Plugin.

in your etc/di.xml

<type name="Magento\SalesRule\Model\Rule\DataProvider">
        <plugin name="add_default_date_to_rule"
                type="Vendor\Module\Plugin\SalesRule\Model\Rule\DataProvider" sortOrder="10" />
</type>

app/code/Vendor/Module/Plugin/SalesRule/Model/Rule/DataProvider.php

<?php

namespace Vendor\Module\Plugin\SalesRule\Model\Rule;

class DataProvider
{
    public function afterGetData(\Magento\SalesRule\Model\Rule\DataProvider $subject, $result)
    {
        if(empty($result)) {
            $result[null] = ['field_name' => 'your default value'];
        } 
        
        return $result;
    }
}
0

For the new entry form, do the following:

public function getData()
{
    $data[null] = ['desired-field': 'desired-value'];
    return $data;
}
1
  • Its not working Error unexpected ':', expecting ']' Oct 3, 2022 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.