0

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)

2

You need to create your own field to add download link in Ui form

Add new field in ui form with elementTmpl

<item name="elementTmpl" xsi:type="string">Vendor_Module/form/element/download</item>

your_ui_form.xml

<field name="download">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="template" xsi:type="string">ui/form/field</item>
            <item name="elementTmpl" xsi:type="string">Vendor_Module/form/element/download</item>
            <item name="label" xsi:type="string">Download Here</item>
            <item name="dataType" xsi:type="string">text</item>
            <item name="formElement" xsi:type="string">input</item>
            <item name="source" xsi:type="string">sampleform</item>
        </item>
    </argument>
</field>

Now create download.html at

app/code/Vendor/Module/web/template/form/element/download.html

Add your custom html like add <a href="download_link"></a> in download.html

|improve this answer|||||
  • how can we pass dynamic file name in app/code/Vendor/Module/web/template/form/element/download.html? – Rutvee Sojitra Nov 13 '18 at 9:11
  • What's the purpose of template and elementTmpl? – Adarsh M Nov 18 '19 at 12:51
  • Where to add the file to download? – Abhilash Acharya Jan 2 at 9:42
  • @RutveeSojitra please check my answer – 26vivek Feb 5 at 10:26
1

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

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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