Ok the new generation of ui grid in magento 2 give you the ability to create or change grid with xml file, this xml use external php file for getting all part needed .
Our example here is the grid action like edit in catalog product grid or delete preview in page cms grid ,
ok we want add custom action .
Ok the xml responsible for ui grid of product list in backend is vendor/magento/module-catalog/view/adminhtml/ui_component/product_listing.xml , and for action you will inside this file :
<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>
it's tell to grid : the current row get the url from edit action but the important part is in the last of file :
<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>
the ui grid use the class Magento\Catalog\Ui\Component\Listing\Columns\ProductActions for adding all action related to this ui grid we go to class in path vendor/magento/module-catalog/Ui/Component/Listing/Columns/ProductActions.php , we find:
{ $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; }
this method is responsible to adding action ,
Ok now we want add our action , it's simple you need grid plugin in your module and add event 'after' , and push your action , inside Ibnab/CustomAction/etc/adminhtml/di.xml (Ibnab = YouVendor , CustomAction= YourModule) :
<?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>
Ok now create your class in :
Ibnab/CustomAction/Plugin/Adminhtml/ProductActions .php and push :
<?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) { $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; } }
thats is all .
Comments