I've added a static Customer Address attribute (for some reason, other didn't work, but whatever; another time):
$setup->addAttribute('customer_address', 'xxx', [
'type' => 'static',
'input' => 'text',
'label' => 'XXX',
'global' => 1,
'visible' => 1,
'required' => 0,
'user_defined' => 1,
'visible_on_front' => 1,
'group' => 'General',
// 'used_in_forms' => ['adminhtml_customer']
'used_in_forms' => ['adminhtml_customer_address']
]);
The attribute is showing up in the admin panel and, on load, the object is populated with the correct value.
But now I want to make sure this attribute gets correctly transferred to the Data model as well (\Magento\Customer\Api\Data\AddressInterface
):
$x = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Customer\Api\AddressRepositoryInterface::class);
/** @var \Magento\Customer\Api\Data\AddressInterface $addr */
$addr = $x->getById(1); // <-- "xxx" is not available here; others are
At first, I thought that only by having the static attribute, M2 would transfer its value to the Data model, or at least to its getCustomAttributes()
or getExtensionAttribute()
. It didn't. So I've created an extension_attrbutes.xml
, telling it to do so:
<?xml version="1.0"?>
<config path="to.xsd">
<extension_attributes for="Magento\Customer\Api\Data\AddressInterface">
<attribute code="xxx" type="string" />
</extension_attributes>
</config>
Damn it, that didn't work either, because I didn't specify a join directive and thus failing some conditions. But do I have to specify a join condition when my attribute is static/column? Furthermore, the Address Data interface is implemented (not auto-generated) so I think the only way is through extension attributes, right?
Do I really have to do it manually?
I'm really starting to hate those Data layers. There's not only many criterias to get an attribute functioning correctly, but, once you finally have made a valid attribute, some other find-them-if-you-can criteria need to be satisfied in order to have attributes into the Data layer.