I added a custom attribute to Customer and it shows on admin forms as expected. But values not getting saved upon saving the form. Bellow is patch file I used to create the attribute.
<?php
namespace Vendor\Module\Setup\Patch\Data;
use Magento\Customer\Model\Customer;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\SchemaPatchInterface;
use Magento\Framework\Setup\Patch\PatchRevertableInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Setup\EavSetupFactory;
/**
* Patch is mechanism, that allows to do atomic upgrade data changes
*/
class CustomerDataPatch implements
DataPatchInterface
{
/**
* @var ModuleDataSetupInterface $moduleDataSetup
*/
private $moduleDataSetup;
private $eavSetupFactory;
private $eavConfig;
/**
* @param ModuleDataSetupInterface $moduleDataSetup
* @param EavSetupFactory $eavSetupFactory
* @param \Magento\Eav\Model\Config $eavConfig
*/
public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
EavSetupFactory $eavSetupFactory,
\Magento\Eav\Model\Config $eavConfig
) {
$this->moduleDataSetup = $moduleDataSetup;
$this->eavSetupFactory = $eavSetupFactory;
$this->eavConfig = $eavConfig;
}
/**
* Do Upgrade
*
* @return void
*/
public function apply()
{
$eavSetup = $this->eavSetupFactory->create();
$eavSetup->addAttribute(
Customer::ENTITY,
'customer_agreement_end_date',
[
'type' => 'static',
'label' => 'Aggrement End Date',
'input' => 'date',
'sort_order' => 20,
'frontend' => \Magento\Eav\Model\Entity\Attribute\Frontend\Datetime::class,
'backend' => \Magento\Eav\Model\Entity\Attribute\Backend\Datetime::class,
'global' => 1,
'visible' => true,
'required' => false,
'system' => false,
'visible_on_front' => true,
'unique' => false,
'group' => 'General',
'input_filter' => 'date',
'position' => 20,
'validate_rules' => '{"input_validation":"date"}',
'note' => 'End date of the aggrement',
]
);
$customAttribute = $this->eavConfig->getAttribute(Customer::ENTITY,'customer_agreement_end_date');
$customAttribute->setData(
'used_in_forms',
[
'adminhtml_customer_address',
'customer_address_edit'
]
);
$customAttribute->save();
}
/**
* {@inheritdoc}
*/
public function getAliases()
{
return [];
}
/**
* {@inheritdoc}
*/
public static function getDependencies()
{
return [
];
}
}
Bellow is the database record created in eav_attribute table. I have got dob field (which comes from default Magento) as well for comparison.
The element is visible in Customer edit form in magento admin. But for some reason, values are not getting saved when I saving the customer. I expect the attribute value be saved upon saving the customer, without any observer or plugin code and Magento handles it itself. Please correct me if I'm wrong or please show me what I have done wrong. Thanks in advance.