cancel
Showing results for 
Search instead for 
Did you mean: 

Appending "extension_attributes" to API call: V1/products/attributes

Appending "extension_attributes" to API call: V1/products/attributes

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;
}

 

2 REPLIES 2

Re: Appending "extension_attributes" to API call: V1/products/attributes

Re: Appending "extension_attributes" to API call: V1/products/attributes

Potentially related to: https://github.com/magento/magento2/issues/16406

 

I am on 2.2.10