Question

I'm not sure what the docs are saying.

I know they're used to add attributes (complex or not) via etc/extension_attributes.xml and I managed to make the compilation process create an interface with auto-generate custom methods, but that's about it.

Let's take for example a couple of interfaces: github link 1 and github link 2. How can I use extension attributes to add certain attributes in one of those interfaces? I'm not interested in complex joins. Just adding a scalar attribute, say "attr1" of type string.

The Data interfaces are the gateway to the model's erm... data, so I need to understand how the extension of those interfaces work in order to properly use the system. I frequently get a Data interface as a param instead of an actual model, and that's ok actually. But I'm a little confused on how the extensions work.

Was it helpful?

Solution

Extension attribute is a way to extend the Interface. Let's take as an example the first link you provided to the ProductAttributeMediaGalleryEntryInterface. If you look at methods there you'll see that it has this method

/**
 * Retrieve existing extension attributes object or create a new one.
 *
 * @return \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryExtensionInterface|null
 */
public function getExtensionAttributes();

Note the @return type of the method ProductAttributeMediaGalleryEntryExtensionInterface -- this is an interface which would be re-generated if you define extension attribute for ProductAttributeMediaGalleryEntryInterface (by default it is generated empty with no methods). The name of the attribute you registered will be used to create methods of interface.

Let's assume you added attr1 of type string. What you can do after interface get regenerated is to access it from the instance of interface.

$entity = $objectManager->get('..\ProductAttributeMediaGalleryEntryInterface')
$entity->getExtensionAttributes()->getAttr1();

To set the attribute, you would need to instantiate Extension Attributes interface

$extension = $objectManager->get('..\ProductAttributeMediaGalleryEntryExtensionInterface')
$extension->setAttr1('value');
$entity->setExtensionAttributes($extension)

The latter is default scenario available, it might be simplified depending on how ExtensionInterface and parent interface are implemented.

[Updated]

Custom attributes and extension attributes serve different purposes.

Custom attributes are needed to represent the EAV attributes of the entity. Most of the EAV attributes are dynamic: they can be added after Magento is deployed via the admin UI. That's why you cannot get code auto-completion for EAV attributes: you don't know about all of them ahead of time.

However, as extension developer, you do know about some attributes for sure -- the ones which you created at development time. It can be a new field in database, field in the related database or an EAV attribute. You can register them as extension attribute because they never change unless the codebase is changed. You can get code auto-completion for them.

OTHER TIPS

In order to complete @EugeneTulika answer, we need to create /etc/extension_attributes.xml :

<?xml version="1.0"?>
<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\Data\ProductInterface">
        <attribute code="attr1" type="string" />
    </extension_attributes>
</config>

Then you can set your new attribut with setAttr1() method, like below:

/** @var ProductExtension $extensionAttributes */
$extensionAttributes = $product->getExtensionAttributes();
$extensionAttributes->setAttr1('super data');
$product->setExtensionAttributes($extensionAttributes);

Hope this can help someone too :)

Will

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top