Magento – Magento 2 : Add link in Admin UI form

adminformmagento-2.1uicomponent

I want to have a downloadable link in my admin form. I want to fetch file name from my table and file is located under Magento_Root/pub/media/ directory.
Note: this form is being used for just edit few fields (mainly informative)

Best Answer

in ui_form.xml

<field name="custom_link" sortOrder="20" formElement="input">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="source" xsi:type="string">custom_test</item>
            <item name="elementTmpl" xsi:type="string">Vendor_Module/input.html</item>
        </item>
    </argument>
    <settings>
        <dataType>text</dataType>
        <label translate="true">Custom Url</label>
        <dataScope>my_url</dataScope>
    </settings>
</field>

note following things:

(custom_link) field ID


(elementTmpl) template path


(my_url) dynamic value will set in Data Provider


in app/code/Vendor/Module/view/adminhtml/web/template/input.html

<a data-bind="attr: {href : value}">My Link</a>

In DataProvider.php

foreach ($items as $item) {
    $this->loadedData[$item->getId()] = $item->getData();
    $this->loadedData[$item->getId()]['my_url'] = "https://my_custom_link.com";
}

Replace my_custom_link with your link

You can see the output in the screenshot enter image description here

Related Topic