1

I have created the custom attribute to upload the business proof file during registration. I am able to store the file in /pub/media and file name in Database and the file name also displaying in customer grid.

How to access the file from admin? Shall we create the href to that column in customer grid to access the file. I have added the screen shot below for reference.

enter image description here

1 Answer 1

4

Try following way:

  1. Add column into your grid list

Vendor/Module/view/adminhtml/ui_component/route_model_listing.xml

<column name="reference" class="Vendor\Module\Ui\Component\Listing\Columns\Reference">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="component" xsi:type="string">Vendor_Module/js/grid/columns/reference</item>
            <item name="sortable" xsi:type="boolean">false</item>
            <item name="label" xsi:type="string" translate="true">Reference</item>
            <item name="sortOrder" xsi:type="number">20</item>
        </item>
    </argument>
</column>
  1. Create a column class

Vendor/Module/Ui/Component/Listing/Columns/Reference.php

<?php
namespace Vendor\Module\Ui\Component\Listing\Columns;

use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Framework\View\Element\UiComponent\ContextInterface;

class Reference extends \Magento\Ui\Component\Listing\Columns\Column
{
    const NAME = 'reference';

    const ALT_FIELD = 'name';

    /**
     * @param ContextInterface $context
     * @param UiComponentFactory $uiComponentFactory
     * @param \Magento\Framework\UrlInterface $urlBuilder
     * @param array $components
     * @param array $data
     */
    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        \Magento\Framework\UrlInterface $urlBuilder,
        array $components = [],
        array $data = []
    ) {
        parent::__construct($context, $uiComponentFactory, $components, $data);
        $this->urlBuilder = $urlBuilder;
    }

    /**
     * Prepare Data Source
     *
     * @param array $dataSource
     * @return array
     */
    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            $fieldName = $this->getData('name');
            foreach ($dataSource['data']['items'] as & $item) {
                $label = $item[$fieldName];
                $item[$fieldName.'_Url'] = $this->urlBuilder->getUrl(
                    'your/custom/url'
                );
                $item[$fieldName.'_Label'] = $label;
            }
        }

        return $dataSource;
    }
}
  1. Create component

Vendor/Module/view/adminhtml/web/js/grid/columns/reference.js

define([
    'Magento_Ui/js/grid/columns/column',
    'jquery',
    'mage/template'
], function (Column, $, mageTemplate) {
    'use strict';

    return Column.extend({
        defaults: {
            bodyTmpl: 'Vendor_Module/grid/cells/reference',
            fieldClass: {
                'data-grid-actions-cell': true
            },
        },
        getLabel: function (record) {
            return record[this.index+'_Label'];
        },
        getUrl: function (record) {
            return record[this.index+'_Url'];
        }
    });
});
  1. Create template

Vendor/Module/view/adminhtml/web/template/grid/cells/reference.html

<a 
  class="action-menu-item"
  text="$col.getLabel($row())"
  attr="href: $col.getUrl($row())"/>
  1. Clear cache
$  php bin/magento cache:flush
$  php bin/magento cache:clean
4
  • thank you for providing the solution. I will check and let you know. Commented Feb 3, 2017 at 10:52
  • Is it working for you ? Commented Mar 24, 2017 at 13:09
  • I didn't check but I did it in alternate way(added it in Customer Grid->Edit->Customer View->Personal Information) because I have added lot of fields. There, I added the href to uploaded files. Commented Mar 24, 2017 at 13:25
  • I use your tricks. Its working fine. Thank you!! Commented Mar 25, 2017 at 4:58

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.