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">

    <fieldset name="general">
        <field name="type">
            <argument name="data" xsi:type="array">
                <item name="options" xsi:type="object">My_Source</item>
                <item name="config" xsi:type="array">
                    <item name="sortOrder" xsi:type="number">40</item>
                    <item name="dataType" xsi:type="string">string</item>
                    <item name="formElement" xsi:type="string">select</item>
                    <item name="scopeLabel" xsi:type="string">[STORE VIEW]</item>
                    <item name="label" xsi:type="string" translate="true">Type</item>
                    <item name="is_used_in_grid" xsi:type="boolean">true</item>
                </item>
            </argument>
        </field>
    </fieldset>

</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->extensionFactory = $extensionFactory;
        $this->sourceFactory = $sourceFactory->create();
    }


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

        return $source;
    }

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

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

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

}

Please provide guidance to fix this issue.

9
  • I think you need to add a setter (and a getter) for the new field.
    – Radu
    Commented Nov 22, 2019 at 19:27
  • @Radu I have added setter and getter. but values are not saving and fetching. please see my updated question
    – manu
    Commented Nov 26, 2019 at 15:34
  • any reason for the two afterGet methods? also, did you create the db table?
    – Radu
    Commented Nov 26, 2019 at 21:17
  • 1
    @anonymous Yes, the extension attribute is not setting the value for source, So I have solved the issue by overriding its model.
    – manu
    Commented Dec 30, 2019 at 6:11
  • 1
    @anonymous Please provide :) Am looking for a perfect solution
    – manu
    Commented Dec 30, 2019 at 6:49

1 Answer 1

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();
2
  • 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
    Commented Oct 21, 2020 at 4:55
  • @anonymous Can you please help me saving & fetching those data, I am still struggling Commented Apr 28, 2023 at 14:49

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.