
Magento 2の新世代UIグリッドでは、xmlファイルを使ってグリッドを作成・変更することができ、必要な部分は外部のPHPファイルを使って取得することができます。この記事では、Magento 2で編集や削除のアクションを追加する方法を紹介します。 具体的には、カタログ製品グリッドでの編集や、ページCMグリッドでのプレビュー削除などのグリッドアクションを例にします。

ステップ1:
vendor/magento/module-catalog/view/adminhtml/ui_component/product_listing.xmlファイルは、バックエンドの商品リストのUIグリッドと、このファイル内のアクションを担当します。
<item name="fieldAction" xsi:type="array">
<item name="provider" xsi:type="string">product_listing.product_listing.product_columns.actions</item>
<item name="target" xsi:type="string">applyAction</item>
<item name="params" xsi:type="array">
<item name="0" xsi:type="string">edit</item>
<item name="1" xsi:type="string">${ $.$data.rowIndex }</item>
</item>
</item>
これは、現在の行がURLフォームの編集アクションを取得している間、重要な部分はファイルの最後の部分であることをグリッドに伝えます。
<actionsColumn name="actions" class="Magento\Catalog\Ui\Component\Listing\Columns\ProductActions">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="indexField" xsi:type="string">entity_id</item>
<item name="sortOrder" xsi:type="number">200</item>
</item>
</argument>
</actionsColumn>
Magento\Catalog\Ui\Component\Listing\Columns\ProductActionsというクラスを使うことで、UIグリッドはパス vendor/magento/module-catalog/Ui/Component/Listing/Columns/ProductActions.phpのクラスに、それに関連するすべてのアクションを追加します。
public function prepareDataSource(array $dataSource)
{
if (isset($dataSource['data']['items'])) {
$storeId = $this->context->getFilterParam('store_id');
foreach ($dataSource['data']['items'] as &$item) {
$item[$this->getData('name')]['edit'] = [
'href' => $this->urlBuilder->getUrl(
'catalog/product/edit',
['id' => $item['entity_id'], 'store' => $storeId]
),
'label' => __('Edit'),
'hidden' => false,
];
}
}
return $dataSource;
}
ステップ2:
まず、モジュールにグリッドプラグインを導入し、イベントの後にイベントを追加し、アクションをIbnab/CustomAction/etc/adminhtml/di.xml(IbnabはYouVendor、CustomActionはYourMode)にプッシュします。
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Catalog\Ui\Component\Listing\Columns\ProductActions">
<plugin name="prepare_data_source_after" type="Ibnab\CustomAction\Plugin\Adminhtml\ProductActions"/>
</type>
</config>
ステップ3:
そして、Ibnab/CustomAction/Plugin/Adminhtml/ProductActions .phpにクラスを作成し、プッシュする必要があります。
<?php
namespace Ibnab\CustomAction\Plugin\Adminhtml;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\Url;
class ProductActions {
protected $urlBuilder;
protected $context;
public function __construct(
ContextInterface $context, Url $urlBuilder
) {
$this->urlBuilder = $urlBuilder;
$this->context = $context;
}
public function afterPrepareDataSource($productActions, $result) {
if (isset($result['data']['items'])) {
$storeId = $this->context->getFilterParam('store_id');
foreach ($result['data']['items'] as &$item) {
$item[$productActions->getData('name')]['preview'] = [
'href' => $this->urlBuilder->getUrl('catalog/product/view', ['id' => $item['entity_id'], '_nosid' => true]),
'target' => '_blank',
'label' => __('ُPreview'),
];
}
}
return $result;
}
}
まとめ
以上、Magento 2で編集や削除のマスアクションを追加するための詳細な手順をご紹介しました。 この記事が、あなたのオンラインストアをより効果的に管理するために役立つことを願っています。