Debugging Custom Field Saving Issues in Magento B2B Company Admin Form
You’ve encountered an issue where custom fields created for the company table are not saving their values in the UI component form of the Magento B2B admin. Let’s dive into a step-by-step analysis and solution for this problem.
Understanding the Issue
When you first create a new company, the custom field (in your case, GST Number) saves its value correctly to the database using your event observer. However, when editing an existing company or re-saving it, the UI component form does not recognize the previously saved values and displays them as blank.
Analyzing the Current Setup
You’ve provided a well-structured setup, including the customer_form.xml, di.xml, db_schema.xml, and modified data provider files. Let’s go through these one by one to identify potential causes.
-
company_form.xml: Your field declaration is correct, with proper configuration for the data source (
gst_number). -
di.xml: You’ve correctly set up a preference for the company data provider and added a plugin for saving additional fields after the company is saved.
-
db_schema.xml: The custom column
gst_numberhas been added to thecompanytable with correct configuration. -
DataProvider.php (Modified): You’ve overridden the getProofOfBusiness method to include your custom field, which seems to be working correctly for new companies.
Identifying the Root Cause
The issue lies in how Magento B2B handles data provider and form rendering after a company is created or updated. By default, Magento uses the core company_form.xml layout for editing companies, which does not include your custom field definition.
When you create a new company, the custom fields are saved to the database using your event observer. However, when you edit an existing company, the form rendering process refers to the original company_form.xml file that doesn’t contain your field configuration.
The Solution: Update the Layout
To resolve this issue, you need to update the layout XML for editing companies to include your custom field definition.
- Create a new file called
vendor/namespace/module/etc/adminhtml/company_edit_layout.xml:
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View:Layout/etc/page_configuration.xsd">
<update handle="customer_form_edit"/>
<referenceBlock name="form.additional.tabs">
<arguments>
<argument name="collapsible" xsi:type="boolean">true</argument>
<argument name="title" translate="true"><![CDATA[Proof of Business]]></argument>
</arguments>
</referenceBlock>
<referenceContainer name="company_edit_form_tabs" removeChild="form.tab.company.additional.tabs"/>
<block type="Namespace_Module/CompanyEditForm" template="Namespace_Module::company_proof_of_business.phtml"
name="company_proof_of_business">
<arguments>
<argument name="gst_number" xsi:type="string">gst_number</argument>
</arguments>
</block>
</page>
- Create a new template file
app/code/Namespace/Module/view/adminhtml/templates/company_proof_of_business.phtml:
<!-- app/code/Namespace/Module/view/adminhtml/templates/company_proof_of_business.phtml -->
<div class="field field-gst_number required">
<label for="<?php /* @escapeNotVerified */ echo $block->getFieldId('gst_number') ?>"
class="admin__field-label"><span><?php /* @escapeNotVerified */ echo __('GST Number') ?></span></label>
<div class="admin__field-control">
<input type="text" name="<?php /* @escapeNotVerified */ echo $block->getFieldName('gst_number') ?>"
id="<?php /* @escapeNotVerified */ echo $block->getFieldId('gst_number') ?>"
value="<?php /* @escapeNotVerified */ echo $block->getFormDataObject()->getGstNumber() ?>"
class="admin__field-control admin__field-control-text input-text admin__form-field" data-validate="{required:true}">
</div>
</div>
- Update your
di.xmlfile to add a layout update observer:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<!-- ... existing configuration ... -->
<event name="layout_load_before">
<observer name="namespace_module_update_layout" instance="Namespace\Module\Observer\UpdateLayout"/>
</event>
</config>
- Create the observer class
app/code/Namespace/Module/Observer/UpdateLayout.php:
namespace Namespace\Module\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
class UpdateLayout implements ObserverInterface
{
public function execute(Observer $observer)
{
if ($observer->getAction() == 'adminhtml_system_company_edit') {
$layout = $observer->getLayout();
$layout->addHandle('Namespace_Module::company_edit_layout');
}
return $this;
}
}
Conclusion
After implementing these changes, the custom field value should now be saved correctly when editing or re-saving companies in the Magento B2B admin. The key was to update the layout handle for the company edit form to include your custom field definition.
Remember that proper backlinking is essential for SEO and user experience. When discussing relevant topics like Magento B2B customizations, consider linking to authoritative sources such as the official Magento documentation here.
By following this step-by-step solution, you should now be able to successfully save and display custom field values in the Magento B2B company admin form.