4

I've added a new column to inventory_source successfully and it's working, here is the tutorial I've followed.

The issue is the data saved to the database is not showed in the form, the field appear empty but the data is in the database.

The Steps I've done are:

  1. Created extension_attributes

  2. Created event to save the data

  3. Created inventory_source_form.xml to view the field

    <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
        <fieldset name="general" sortOrder="10">
            <field name="branch_erp_id" formElement="input" sortOrder="25">
                <settings>
                    <validation>
                        <rule name="validate-number" xsi:type="boolean">true</rule>
                    </validation>
                    <dataType>number</dataType>
                    <label translate="true">Branch ERP Id</label>
                </settings>
            </field>
        </fieldset>
    </form>```
    

The di.xml file:

<type name="Magento\InventoryApi\Api\SourceRepositoryInterface">
    <plugin name="plugin_source_save" type="vendor\module\Plugin\InventorySource\Source"/>
</type>

The plugin file Source.php:

<?php

namespace vendor\module\Plugin\InventorySource;

use Magento\InventoryApi\Api\SourceRepositoryInterface; use Magento\InventoryApi\Api\Data\SourceInterface; use Magento\InventoryApi\Api\Data\SourceSearchResultsInterface; use Magento\InventoryApi\Api\Data\SourceExtensionFactory; use Magento\InventoryApi\Api\Data\SourceExtensionInterfaceFactory;

class Source { const FIELD_NAME = 'branch_erp_id';

protected $extensionFactory;
protected $sourceFactory;

public function __construct(SourceExtensionInterfaceFactory $extensionFactory)
{
    $this-&gt;extensionFactory = $extensionFactory;
}


public function afterGet(SourceRepositoryInterface $subject, SourceInterface $source)
{
    $sourceBranchId = $source-&gt;getData(self::FIELD_NAME);
    $extensionAttributes = $source-&gt;getExtensionAttributes();
    $extensionAttributes = $extensionAttributes ? $extensionAttributes : $this-&gt;extensionFactory-&gt;create();
    $extensionAttributes-&gt;setBranchErpId($sourceBranchId);
    $source-&gt;setExtensionAttributes($extensionAttributes);

    return $source;
}

public function afterGetList(SourceRepositoryInterface $subject, SourceSearchResultsInterface $result)
{
    $products = [];
    $sources = $result-&gt;getItems();

    foreach ($sources as $source) {
        $sourceBranchId = $source-&gt;getData(self::FIELD_NAME);
        $extensionAttributes = $source-&gt;getExtensionAttributes();
        $extensionAttributes = $extensionAttributes ? $extensionAttributes : $this-&gt;extensionFactory-&gt;create();
        $extensionAttributes-&gt;setBranchErpId($sourceBranchId);
        $source-&gt;setExtensionAttributes($extensionAttributes);
        $products[] = $source;
    }
    $result-&gt;setItems($products);
    return $result;
}

}

2 Answers2

0

I have faced the issue,below is the solution which is working fine for me

create a plugin for this created etc/adminhtml/di.xml

 <type name="Magento\InventoryAdminUi\Ui\DataProvider\SourceDataProvider">
        <plugin name="display_attribute" type="Vendor\Test\Plugin\CustomAttribute" sortOrder="90"/>
    </type>

Create file in Vendor\Test\Plugin\CustomAttribute.php

<?php

namespace Vendor\Test\Plugin;

use Magento\InventoryAdminUi\Ui\DataProvider\SourceDataProvider;

class CustomAttribute {

public function afterGetData(
    SourceDataProvider $subject,
    array              $result
): array
{
    foreach ($result as $key =&gt; $item) {
        if (!is_array($item)) {
            continue;
        }

        if (isset($result[$key]['general']['extension_attributes']['branch_erp_id']) &amp;&amp; $branchErpId = $result[$key]['general']['extension_attributes']['branch_erp_id']) {

            $result[$key]['general']['branch_erp_id'] = $branchErpId;
        }

    }
    return $result;
}

}

0

This solution worked for me, I am adding store_view_code extension attribute in inventory source, You need to create Plugins for save and and get methods of InventoryRepositoryInterface to save and get data. and also add code in listing Ui component and also add form listing ui component

<vedor>/<module>/view/adminhtml/ui_component/inventory_source_listing.xml
<?xml version="1.0" encoding="utf-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<columns name="inventory_source_listing_columns">
    <column
        name="store_view_code"
        component="Magento_Ui/js/grid/columns/select"
        sortOrder="5"
        >
        <settings>
            <options class="<vender>\<module>\Model\Config\Source\StoreViewCodeSource"/>
            <label translate="true">Store View</label>
            <dataScope>extension_attributes.store_view_code</dataScope>
            <visible>false</visible>
            <filter>select</filter>
            <dataType>select</dataType>
        </settings>
    </column>
</columns>

Also add plugin for Grid to display column.

<?php
namespace <Vendor>\<Module>\Plugin\Inventory\Source\DataProvider;
use Magento\InventoryAdminUi\Ui\DataProvider\SourceDataProvider;

class StoreViewCode {

public function afterGetData( SourceDataProvider $subject, array $result ): array { if(count($result) && array_key_exists("items", $result)){ foreach ($result['items'] as $key => $item) { if (!is_array($item)) { continue; }

        if (
            isset($item['extension_attributes']['store_view_code']) &amp;&amp;
            $storeViewCode = $item['extension_attributes']['store_view_code']
        ) {
            $result['items'][$key]['store_view_code'] = $storeViewCode;
        }

    }
}
return $result;

}

}