3

Value not getting saved for a newly added column in inventory_source table

inventory_source_form.xml

<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
&lt;fieldset name=&quot;general&quot;&gt;
    &lt;field name=&quot;type&quot;&gt;
        &lt;argument name=&quot;data&quot; xsi:type=&quot;array&quot;&gt;
            &lt;item name=&quot;options&quot; xsi:type=&quot;object&quot;&gt;My_Source&lt;/item&gt;
            &lt;item name=&quot;config&quot; xsi:type=&quot;array&quot;&gt;
                &lt;item name=&quot;sortOrder&quot; xsi:type=&quot;number&quot;&gt;40&lt;/item&gt;
                &lt;item name=&quot;dataType&quot; xsi:type=&quot;string&quot;&gt;string&lt;/item&gt;
                &lt;item name=&quot;formElement&quot; xsi:type=&quot;string&quot;&gt;select&lt;/item&gt;
                &lt;item name=&quot;scopeLabel&quot; xsi:type=&quot;string&quot;&gt;[STORE VIEW]&lt;/item&gt;
                &lt;item name=&quot;label&quot; xsi:type=&quot;string&quot; translate=&quot;true&quot;&gt;Type&lt;/item&gt;
                &lt;item name=&quot;is_used_in_grid&quot; xsi:type=&quot;boolean&quot;&gt;true&lt;/item&gt;
            &lt;/item&gt;
        &lt;/argument&gt;
    &lt;/field&gt;
&lt;/fieldset&gt;

</form>

The column got added in admin UI

enter image description here

But while selecting the option and saving the value. It cannot reflect in the table.

extension_attributes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
<extension_attributes for="Magento\InventoryApi\Api\Data\SourceInterface">
    <attribute code="type" type="string" />
</extension_attributes>
</config>

Plugin:

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\InventoryApi\Api\SourceRepositoryInterface">
        <plugin name="plugin_source_save" type="my_vendor\modulename\Plugin\SourceSave"/>
    </type>
</config>

plugin/SourceSave.php

<?php

namespace vendor\module\Plugin;

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; use vendor\module\Model\Attribute\SourceFactory;

class SourceSave {

const FIELD_NAME = 'type';

protected $extensionFactory;
protected $sourceFactory;

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


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

    return $source;
}

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

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

public function beforeSave(
    SourceRepositoryInterface $subject,
    SourceInterface $source
)
{
    $extensionAttributes = $source-&gt;getExtensionAttributes() ?: $this-&gt;extensionFactory-&gt;create();
    if ($extensionAttributes !== null &amp;&amp; $extensionAttributes-&gt;getType() !== null) {
        $source-&gt;setType($extensionAttributes-&gt;getType());
    }
    //var_dump($source-&gt;getData());die;
    return [$source];
}

}

Please provide guidance to fix this issue.

Bhaumik Upadhyay
  • 893
  • 1
  • 9
  • 18
manu
  • 131
  • 5

1 Answers1

2

As you've already implemented extension_attributes here's how you can save it. You need to observe controller_action_inventory_populate_source_with_data and get value of your field as below:

        $request = $observer->getEvent()->getRequest();
        $requestData = $request->getParam('general', []);

        $type =  $requestData['type'];
        $sourceCode = $requestData['source_code'];

        //Magento\InventoryApi\Api\SourceRepositoryInterface 
        $source = $this->sourceRepository->get($sourceCode);
        $source->setType($type);
        $source->save();
anonymous
  • 3,722
  • 3
  • 25
  • 67
  • Above code will not work while source will save from API. Because it observe controller_action_inventory_populate_source_with_data event. Please share the code for save via API. Thanks in advance. – gks Oct 21 '20 at 04:55
  • @anonymous Can you please help me saving & fetching those data, I am still struggling – GunJan Mehta Apr 28 '23 at 14:49