2

I want to set a form field value from the URL params in UI form. I am aware that static values can be set via default property of the field but how to set dynamic values as default (to be specific request URL params).

Also, what is the standard way to do it?

`http://localhost/magento/2.2.4/admin/mymodule/myform/edit/type/vendor/`

Here, type/vendor the value vendor needs to be set on type field as default.

<field name="type">
        <argument name="data" xsi:type="array">
            <item name="options" xsi:type="object">MyModule\MyVendor\Model\Source\Type</item>
            <item name="config" xsi:type="array">
                <item name="visible" xsi:type="boolean">true</item>
                <item name="dataType" xsi:type="string">select</item>
                <item name="formElement" xsi:type="string">select</item>
                <item name="source" xsi:type="string">mymodule_myform_form_data_source</item>
                <item name="label" xsi:type="string" translate="true">Type</item>
                <item name="disabled" xsi:type="boolean">true</item>
                <item name="default" xsi:type="string">inventory</item>
            </item>
        </argument>
    </field>

MyModule\MyVendor\Model\Source\Type.php

<?php

namespace MyModule\MyVendor\Model\Source;

use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;

class Type extends AbstractSource
{
    /**
     * @return array
     */
    public function getAllOptions()
    {
        return [
            [
                'value' => 'vendor',
                'label' => __('Vendor'),
            ],
        ];
    }
}

form

3
  • why not set it on your source type since data coming from there
    – fmsthird
    May 21, 2019 at 7:28
  • can you post MyModule\MyVendor\Model\Source\Type codes
    – fmsthird
    May 21, 2019 at 7:28
  • Datasource works only if there is data i.e the form is editing a saved value. In the case of new form, the values are ignored as there is no id for it. May 21, 2019 at 7:29

1 Answer 1

1

This is untested but you can give it a try.
In your Type.php class, inject \Magento\Framework\App\Request\Http $request to get the value of the parameter type in the URL which is the value vendor.

<?php

namespace MyModule\MyVendor\Model\Source;

class Type implements \Magento\Framework\Option\ArrayInterface
{
    protected $request;
    public function __construct(
        \Magento\Framework\App\Request\Http $request
    )
    {
        $this->request = $request;
    }
    public function toOptionArray()
    {
        $type = $this->request->getParam('type');

        return [
            [
                'value' => $type,
                'label' => __('Vendor'),
            ],
        ];

    }
}  

In that way, you are setting the value of options from the URL parameter.

2
  • This looks a little out of the way. It may be working as UI form takes the first field, but then I need to update labels too. May 21, 2019 at 7:51
  • Is label the same with the value? I mean the text of the label? or it will be taken from somewhere else
    – fmsthird
    May 21, 2019 at 7:52

Your Answer

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

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