

サードパーティの開発者が、Magento Core コードで定義されている API Data インターフェースを変更することは不可能です。とはいえ、これらのエンティティのほとんどには、エクステンションアトリビュートと呼ばれる機能が備わっています。この記事では、Magento 2のエンティティに拡張属性を追加する方法を紹介します。
ステップ 1: Magento APIから商品/商品リストを取得する
Magento API から商品や商品リストを呼び出すには、適切なサービス(ここでは商品リポジトリ)に API リクエストを行う必要があります。これらのリクエストに対するレスポンスは、以下のような構造のオブジェクトを返します。
製品レスポンス
<product>
<id>1</id>
<sku>some-sku</sku>
<custom_attributes><!-- Custom Attributes Data --></custom_attributes>
<extension_attributes><!-- Here should we add extension attributes data --></extension_attributes>
</product>
製品リストのレスポンス
<products>
<item>
<id>1</id>
<sku>some-sku</sku>
<custom_attributes><!-- Custom Attributes Data --></custom_attributes>
<extension_attributes><!-- Here should we add extension attributes data --></extension_attributes>
</item>
<item>
<id>2</id>
<sku>some-sku-2</sku>
<custom_attributes><!-- Custom Attributes Data --></custom_attributes>
<extension_attributes><!-- Here should we add extension attributes data --></extension_attributes>
</item>
</products>
ステップ2:製品リポジトリへのプラグインの追加
Product Reposibilityに拡張機能を追加するには、Afterプラグインを使用する必要があります。プラグインはsave, get, getListなどのメソッドに従う必要があります。単純な属性であるスカラーと、データオブジェクトで表示されるノンスカラーを追加することができます。
public function afterGet
(
\Magento\Catalog\Api\ProductRepositoryInterface $subject,
\Magento\Catalog\Api\Data\ProductInterface $entity
) {
$ourCustomData = $this->customDataRepository->get($entity->getId());
$extensionAttributes = $entity->getExtensionAttributes(); /** get current extension attributes from entity **/
$extensionAttributes->setOurCustomData($ourCustomData);
$entity->setExtensionAttributes($extensionAttributes);
return $entity;
}
AfterGetプラグイン
ここでは、コンフリクトを起こさずに拡張機能を追加する最もシンプルな方法をご紹介します。
- まず、エンティティの拡張属性がすでに設定されている場合は、その属性を取得します。
- 次に、私たちの拡張属性を追加します。
- 最後に、私たちの拡張属性を含めてエンティティの拡張属性を設定します。
Get Listプラグインの後
Get Listの後にも同様のことができます
プラグインを保存した後
afterSaveプラグインは、エンティティデータを返す前に操作する必要があります。
public function afterSave
(
\Magento\Catalog\Api\ProductRepositoryInterface $subject,
\Magento\Catalog\Api\Data\ProductInterface $entity
) {
$extensionAttributes = $entity->getExtensionAttributes(); /** get current extension attributes from entity **/
$ourCustomData = $extensionAttributes->getOurCustomData();
$this->customDataRepository->save($ourCustomData);
return $entity;
}
しかし、エンティティによっては拡張属性を取得するための実装がない場合、常にnullを取得することになります。拡張属性を取得するたびに、それがnullかどうかを判断する必要があります。もしnullであれば、それを作成する必要があります。このようなコードの重複を防ぐために、拡張属性を持つエンティティに対してafterGetプラグインを作成する必要があります。
ここでは、productエンティティに拡張属性の実装がないと仮定して、以下のようなプラグインを作成してみましょう:
use Magento\Catalog\Api\Data\ProductExtensionInterface;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\Data\ProductExtensionFactory;
class ProductAttributesLoad
{
/**
* @var ProductExtensionFactory
*/
private $extensionFactory;
/**
* @param ProductExtensionFactory $extensionFactory
*/
public function __construct(ProductExtensionFactory $extensionFactory)
{
$this->extensionFactory = $extensionFactory;
}
/**
* Loads product entity extension attributes
*
* @param ProductInterface $entity
* @param ProductExtensionInterface|null $extension
* @return ProductExtensionInterface
*/
public function afterGetExtensionAttributes(
ProductInterface $entity,
ProductExtensionInterface $extension = null
) {
if ($extension === null) {
$extension = $this->extensionFactory->create();
}
return $extension;
}
}
次に、プラグインをProductInterfaceにアタッチする必要があります:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Catalog\Api\Data\ProductInterface">
<plugin name="ProductExtensionAttributeOperations" type="Magento\Catalog\Plugin\ProductAttributesLoad"/>
</type>
</config>
ステップ3: 拡張機能の属性設定
スカラー属性の場合
以下のように設定してください:
<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="first_custom_attribute" type="Magento\SomeModule\Api\Data\CustomDataInterface" />
<attribute code="second_custom_attribute" type="Magento\SomeModule\Api\Data\CustomDataInterface" />
</extension_attributes>
</config>
すると、次のような結果が得られます
<product>
<id>1</id>
<sku>some-sku</sku>
<custom_attributes><!-- Custom Attributes Data --></custom_attributes>
<extension_attributes>
<first_custom_attribute>1</first_custom_attribute>
<second_custom_attribute>2</second_custom_attribute>
</extension_attributes>
</product>
スカラーではない属性の場合
以下のように設定してください:
<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="our_custom_data" type="Magento\SomeModule\Api\Data\CustomDataInterface[]" />
</extension_attributes>
</config>
すると、次のような結果が得られます
<product>
<id>1</id>
<sku>some-sku</sku>
<custom_attributes><!-- Custom Attributes Data --></custom_attributes>
<extension_attributes>
<our_custom_data>
<first_custom_attribute>1</first_custom_attribute>
<second_custom_attribute>2</second_custom_attribute>
</our_custom_data>
</extension_attributes>
</product>
結論
以上、Magento 2でエンティティに拡張属性を追加するための詳細な手順をご紹介しました。 この記事が皆様のお役に立てれば幸いです。