tldr: How do I append extension_attributes to V1/products/attributes
When creating a product attribute, I have a custom property that can be assigned; it's stored in the database as "api_attribute_id".
When making API calls to V1/products/attributes, I'd like for this data to be appended to each attribute in the response.
I have a plugin and extension_attribute.xml configured and when I dump "$attribute" within Plugin.php::afterGetList(), I can see "extension_attribute" appended to the data which includes "api_attribute_id" with its appropriate value. However, if I remove my dump, my API data does not include "extension_attributes".
<!--extension_attributes.xml -->
<?xml version="1.0"?>
<!-- File: app/code/Atwix/OrderFeedback/etc/extension_attributes.xml -->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
<extension_attributes for="\Magento\Catalog\Api\ProductAttributeRepositoryInterface">
<attribute code="api_attribute_id" type="string" />
</extension_attributes>
</config>
/* Plugin: ProductAttributeRepositoryPlugin */
/**
* @var AttributeExtensionInterfaceFactory
*/
private $extensionFactory;
/**
* Add "api_attribute_id" extension attribute to order data object to make it accessible in API data
*
* @param ProductAttributeRepositoryInterface $subject
* @param SearchResults $searchResult
*
* @return SearchResults
*/
public function afterGetList(ProductAttributeRepositoryInterface $subject, SearchResults $searchResult)
{
$attributes = $searchResult->getItems();
foreach ($attributes as &$attribute) {
$apiAttributeId = $attribute->getData(self::FIELD_NAME);
$extensionAttributes = $attribute->getExtensionAttributes();
$extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create();
$extensionAttributes->setData('api_attribute_id', $apiAttributeId);
$attribute->setExtensionAttributes($extensionAttributes);
}
return $searchResult;
}
This is the basis of my code presented above: https://devdocs.magento.com/guides/v2.3/extension-dev-guide/extension_attributes/adding-attributes.h...