cancel
Showing results for 
Search instead for 
Did you mean: 

Change order of sources in product view in admin

Change order of sources in product view in admin

Hi there,

 

I've struggling with this all day, i'm trying to reorder the sources of a product in the admin view, by default the sources are ordered alphabetically ascending, and i need them to be alphabetically descending.

 

I tried to do this with a plugin on the dataprovider, according to:

 

vendor/magento/module-inventory-catalog-admin-ui/view/adminhtml/ui_component/product_form.xml

is:  

"Magento\InventoryAdminUi\Ui\DataProvider\SourceDataProvider"

and then in the plugin an 

afterGetData()

method, BUT is not working, i'm debugging and the dataprovider i'm catching according to my logs is: 

[2024-12-26T20:02:16.430+00:00] INFO: [9d0c7590072a] CustomSortOrderPlugin: Current DataProvider: {"name":"catalogstaging_upcoming_grid_data_source","class":"Magento\\Framework\\View\\Element\\UiComponent\\DataProvider\\DataProvider\\Interceptor"}

 

This is my app/code/Vendor/Module/etc/adminhtml/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider">
        <plugin name="custom_sort_order_plugin" type="Vendor\Module\Plugin\CustomSortOrderPlugin"/>
    </type>
    <type name="Magento\InventoryAdminUi\Ui\DataProvider\SourceDataProvider">
        <plugin name="custom_sort_order_source_plugin" type="Vendor\Module\Plugin\CustomSortOrderPlugin"/>
    </type>
</config>

 

And the plugin:

<?php

namespace Vendor\Module\Plugin;

use Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider;
use Vendor\Logging\Logger\VendorLogger;

class CustomSortOrderPlugin
{
    public function __construct(private readonly VendorLogger $logger)
    {
        $this->logger->init('custom-source-data-provider');
        $this->logger->info('CustomSourceDataProvider instantiated');
    }

    public function afterGetData(DataProvider $subject, array $result): array
    {
        // Log all provider names for debugging
        $this->logger->info('CustomSortOrderPlugin: Current DataProvider: ' . json_encode([
                'name' => $subject->getName(),
                'class' => get_class($subject)
            ]));

        // List of possible inventory source data provider names
        $validProviders = [
            'inventory_source_listing_data_source',
            'product_form.product_form_source_listing',
            'product_form.sources',
            'inventory_low_quantity_notification_stock_grid_data_source'
        ];

        if (in_array($subject->getName(), $validProviders)) {
            $this->logger->info('CustomSortOrderPlugin: Found matching provider: ' . json_encode([
                    'provider' => $subject->getName()
                ]));

            if (!empty($result['items'])) {
                $this->logger->info('CustomSortOrderPlugin: Before sorting: ' . json_encode([
                        'items' => array_column($result['items'], 'name')
                    ]));

                usort($result['items'], function ($a, $b) {
                    return strcasecmp($b['name'], $a['name']); // Descending order
                });

                // Reindex the array
                $result['items'] = array_values($result['items']);

                $this->logger->info('CustomSortOrderPlugin: After sorting: ' . json_encode([
                        'items' => array_column($result['items'], 'name')
                    ]));
            } else {
                $this->logger->info('CustomSortOrderPlugin: Result items is empty: ' . json_encode([
                        'result_keys' => array_keys($result)
                    ]));
            }
        }

        return $result;
    }
}

Any ideas??? thanks!