Skip to content

Magento246 ProductAttachment Ui_Component grid not working properly

I am trying to show all my attachments in my grid but the grid is showing only the last one that I uploaded, but in the database and on the frontend it shows all and when i do filter it show all without filter its only show last one.
can anyone please help me?

Thanks in Advance

this is my XML

`

tekfile_file_listing.tekfile_file_listing_data_source
tekfile_file_listing.tekfile_file_listing_data_source

tekfile_file_columns

AmfileFileGridDataProvider
tekfile_file_listing_data_source
id
id

Magento_Ui/js/grid/provider

ui/grid/toolbar

tekfile_file_listing

tekfile_file_listing.tekfile_file_listing.tekfile_file_columns

Magento_Ui/js/grid/controls/columns
dataGridActions

tekfile_file_listing.tekfile_file_listing.tekfile_file_columns

tekfile_file_listing.tekfile_file_listing.listing_top.bookmarks
current.filters

Magento_Ui/js/form/element/ui-select
ui/grid/filters/elements/ui-select

tekfile_file_listing.tekfile_file_listing.listing_top.listing_filters

tekfile_file_listing.tekfile_file_listing.tekfile_file_columns.ids

                <item name="indexField" xsi:type="string">history_id</item>
            </item>
        </argument>
        <action name="delete">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="confirm" xsi:type="array">
                        <item name="title" xsi:type="string" translate="true">Delete items</item>
                        <item name="message" xsi:type="string" translate="true">Delete selected items?</item>
                    </item>
                    <item name="type" xsi:type="string">delete</item>
                    <item name="label" xsi:type="string" translate="true">Delete</item>
                    <item name="url" xsi:type="url" path="tekfile/file/massDelete"/>
                </item>
            </argument>
        </action>
    </massaction>
    <paging name="listing_paging">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="storageConfig" xsi:type="array">
                    <item name="provider" xsi:type="string">tekfile_file_listing.tekfile_file_listing.listing_top.bookmarks</item>
                    <item name="namespace" xsi:type="string">current.paging</item>
                </item>
                <item name="selectProvider" xsi:type="string">tekfile_file_listing.tekfile_file_listing.tekfile_file_columns.ids</item>
            </item>
        </argument>
    </paging>
</container>
<columns name="tekfile_file_columns">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="storageConfig" xsi:type="array">
                <item name="provider" xsi:type="string">tekfile_file_listing.tekfile_file_listing.listing_top.bookmarks</item>
                <item name="namespace" xsi:type="string">current</item>
            </item>
            <item name="childDefaults" xsi:type="array">
                <item name="storageConfig" xsi:type="array">
                    <item name="provider" xsi:type="string">tekfile_file_listing.tekfile_file_listing.listing_top.bookmarks</item>
                    <item name="root" xsi:type="string">columns.${ $.index }</item>
                    <item name="namespace" xsi:type="string">current.${ $.storageConfig.root }</item>
                </item>
            </item>
        </item>
    </argument>
    <selectionsColumn name="ids">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="indexField" xsi:type="string">id</item>
                <item name="sortOrder" xsi:type="number">0</item>
            </item>
        </argument>
    </selectionsColumn>
    <column name="id">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="filter" xsi:type="string">textRange</item>
                <item name="sorting" xsi:type="string">asc</item>
                <item name="label" xsi:type="string" translate="true">ID</item>
            </item>
        </argument>
    </column>
    <column name="file_name" >
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="filter" xsi:type="string">text</item>
                <item name="label" xsi:type="string" translate="true">File Name</item>
            </item>
        </argument>
    </column>
    <column name="product_id">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="filter" xsi:type="string">textRange</item>
                <item name="label" xsi:type="string" translate="true">Product Id</item>
            </item>
        </argument>
    </column>
    <column name="sku">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="filter" xsi:type="string">text</item>
                <item name="label" xsi:type="string" translate="true">Product SKU</item>
            </item>
        </argument>
    </column>
    <column name="file_size" class="TekglideProductAttachmentUiComponentListingColumnFileSize">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="filter" xsi:type="string">text</item>
                <item name="label" xsi:type="string" translate="true">File Size</item>
            </item>
        </argument>
    </column>
    <actionsColumn name="actions" class="TekglideProductAttachmentUiComponentListingColumnDownloadFile">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="indexField" xsi:type="string">id</item>
                <item name="url" xsi:type="string">tekfile/file/download</item>
                <item name="urlEntityParamName" xsi:type="string">id</item>
            </item>
        </argument>
    </actionsColumn>
</columns>

`

<?php

namespace TekglideProductAttachmentUiComponentListingColumnAttachment;

use MagentoFrameworkViewElementUiComponentFactory;
use MagentoFrameworkViewElementUiComponentContextInterface;

class Upload extends MagentoUiComponentListingColumnsColumn
{
const NAME = ‘attachment’;

const ALT_FIELD = 'name';

/**
 * @var MagentoFrameworkObjectManagerInterface
 */
protected $objectManager;

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

public function prepareDataSource(array $dataSource)
{
    if (isset($dataSource['data']['items'])) {
        $fieldName = $this->getData('name');
        $storeId = $this->context->getRequestParam('store');

        foreach ($dataSource['data']['items'] as &$item) {
            $item[$fieldName . '_form_key'] = $this->getFormKey();
            $item[$fieldName . '_upload_max_size'] = $this->getMaxFileSize();
            $item[$fieldName . '_store_id'] = $storeId;
            $item[$fieldName . '_upload_url'] = $this->getUploadUrl();
        }
    }

    return $dataSource;
}


/**
 * @param array $row
 *
 * @return null|string
 */
protected function getAlt($row)
{
    $altField = $this->getData('config/altField') ?: self::ALT_FIELD;
    return isset($row[$altField]) ? $row[$altField] : null;
}

/**
 * @return TekglideProductAttachmentModelFile
 */
protected function getFileModel()
{
    return $this->objectManager->get('TekglideProductAttachmentModelFile');
}

protected function getFormKey()
{
    /**
     * @var MagentoFrameworkDataFormFormKey $formKeyInstance
     */
    $formKeyInstance = $this->objectManager->get('MagentoFrameworkDataFormFormKey');

    return $formKeyInstance->getFormKey();
}

protected function getUploadUrl()
{
    /**
     * @var MagentoBackendModelUrl $urlBuilder
     */
    $urlBuilder = $this->objectManager->get('MagentoBackendModelUrlFactory')->create();

    return $urlBuilder->getUrl('tekfile/product/upload', ['_secure' => true]);
}

protected function getMaxFileSize() {
    /**
     * @var MagentoFrameworkFileSize $uploaderSize
     */
    $uploaderSize = $this->objectManager->create('MagentoFrameworkFileSize');

    return $uploaderSize->getMaxFileSize();
}

}

<?php

namespace TekglideProductAttachmentUiComponentListingColumn;

use MagentoFrameworkViewElementUiComponentFactory;
use MagentoFrameworkViewElementUiComponentContextInterface;

class Attachment extends MagentoUiComponentListingColumnsColumn
{
const NAME = ‘attachment’;

const ALT_FIELD = 'name';

/**
 * @var MagentoFrameworkObjectManagerInterface
 */
protected $objectManager;

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

public function prepareDataSource(array $dataSource)
{
    if (isset($dataSource['data']['items'])) {
        $fieldName = $this->getData('name');
        $productIds = array_column($dataSource['data']['items'], 'entity_id');
        $storeId = $this->context->getRequestParam('store');
        $files = $this->getFileModel()->getFilesProductGrid($productIds, $storeId);
        foreach ($dataSource['data']['items'] as &$item) {
            $product = new MagentoFrameworkDataObject($item);
            $item[$fieldName . '_alt'] = $this->getAlt($item) . ' Attachment';
            $item[$fieldName . '_list'] 
                = array_key_exists($product->getEntityId(), $files)
                    ? $files[$product->getEntityId()] : [];
        }
    }

    return $dataSource;
}

/**
 * @param array $row
 *
 * @return null|string
 */
protected function getAlt($row)
{
    $altField = $this->getData('config/altField') ?: self::ALT_FIELD;
    return isset($row[$altField]) ? $row[$altField] : null;
}

/**
 * @return TekglideProductAttachmentModelFile
 */
protected function getFileModel()
{
    return $this->objectManager->get('TekglideProductAttachmentModelFile');
}

}

frontend