/   /   /  Magento 2 backend add custom grid action (target _self or _blank)

Note:

For more extensions and themes visit our store

Magento 2 backend add custom grid action (target _self or _blank)


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 :

  1.  
  2. <item name="fieldAction" xsi:type="array">
  3.      <item name="provider" xsi:type="string">product_listing.product_listing.product_columns.actions</item>
  4.              <item name="target" xsi:type="string">applyAction</item>
  5.                    <item name="params" xsi:type="array">
  6.                    <item name="0" xsi:type="string">edit</item>
  7.               <item name="1" xsi:type="string">${ $.$data.rowIndex }</item>
  8.      </item>
  9.  </item>
  10.  
  11.  

it's tell to grid : the current row  get the url from edit action but the important part is in the last of file :

  1.  
  2.         <actionsColumn name="actions" class="Magento\Catalog\Ui\Component\Listing\Columns\ProductActions">
  3.             <argument name="data" xsi:type="array">
  4.                 <item name="config" xsi:type="array">
  5.                     <item name="indexField" xsi:type="string">entity_id</item>
  6.                     <item name="sortOrder" xsi:type="number">200</item>
  7.                 </item>
  8.             </argument>
  9.         </actionsColumn>
  10.  
  11.  

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:

  1.  
  2.     public function prepareDataSource(array $dataSource)
  3.     {
  4.         if (isset($dataSource['data']['items'])) {
  5.             $storeId = $this->context->getFilterParam('store_id');
  6.             foreach ($dataSource['data']['items'] as &$item) {
  7.                 $item[$this->getData('name')]['edit'] = [
  8.                     'href' => $this->urlBuilder->getUrl(
  9.                         'catalog/product/edit',
  10.                         ['id' => $item['entity_id'], 'store' => $storeId]
  11.                     ),
  12.                     'label' => __('Edit'),
  13.                     'hidden' => false,
  14.                 ];
  15.             }
  16.         }
  17.       return $dataSource;
  18.     }
  19.  
  20.  

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) :

  1.  
  2. <?xml version="1.0"?>
  3. <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
  4.     <type name="Magento\Catalog\Ui\Component\Listing\Columns\ProductActions">
  5.         <plugin name="prepare_data_source_after" type="Ibnab\CustomAction\Plugin\Adminhtml\ProductActions"/>
  6.     </type>
  7. </config>
  8.  
  9.  

ok now create your class in :
Ibnab/CustomAction/Plugin/Adminhtml/ProductActions .php and push :

  1.  
  2. <?php
  3. namespace Ibnab\CustomAction\Plugin\Adminhtml;
  4. use Magento\Framework\View\Element\UiComponent\ContextInterface;
  5. use Magento\Framework\UrlInterface;
  6. class ProductActions {
  7.     protected $urlBuilder;
  8.     protected $context;
  9.     protected $dataHelper;
  10.     public function __construct(
  11.     ContextInterface $context, UrlInterface $urlBuilder, \Ibnab\QuickPreview\Helper\Data $dataHelper
  12.     ) {
  13.         $this->urlBuilder = $urlBuilder;
  14.         $this->context = $context;
  15.         $this->dataHelper = $dataHelper;
  16.     }
  17.     public function afterPrepareDataSource($productActions, $result) {
  18.         if ($this->dataHelper->allowExtension()) {
  19.             if (isset($result['data']['items'])) {
  20.                 $storeId = $this->context->getFilterParam('store_id');
  21.                foreach ($result['data']['items'] as &$item) {
  22.                 $item[$productActions->getData('name')]['delete] = [
  23.                     'href' => $this->urlBuilder->getUrl(
  24.                         'catalog/product/delete',
  25.                         ['id' => $item['entity_id'], 'store' => $storeId]
  26.                     ),
  27.                     'label' => __('Delete'),
  28.                     'hidden' => false,
  29.                 ];
  30.                 }
  31.             }
  32.         }
  33.         return $result;
  34.     }
  35. }
  36.  
  37.  

that give you link opened in the same page not target _blank if you want open on target _black you need use class Magento\Cms\Block\Adminhtml\Page\Grid\Renderer\Action\UrlBuilder the code it will be :

  1.  
  2. <?php
  3. namespace Ibnab\CustomAction\Plugin\Adminhtml;
  4. use Magento\Framework\View\Element\UiComponent\ContextInterface;
  5. use Magento\Cms\Block\Adminhtml\Page\Grid\Renderer\Action\UrlBuilder;
  6. use Magento\Framework\UrlInterface;
  7. class ProductActions {
  8.     protected $urlBuilder;
  9.     protected $context;
  10.     protected $dataHelper;
  11.     protected $actionUrlBuilder;
  12.     public function __construct(
  13.     ContextInterface $context, UrlInterface $urlBuilder, UrlBuilder $actionUrlBuilder, \Ibnab\QuickPreview\Helper\Data $dataHelper
  14.     ) {
  15.         $this->urlBuilder = $urlBuilder;
  16.         $this->actionUrlBuilder = $actionUrlBuilder;
  17.         $this->context = $context;
  18.         $this->dataHelper = $dataHelper;
  19.     }
  20.     public function afterPrepareDataSource($productActions, $result) {
  21.         if ($this->dataHelper->allowExtension()) {
  22.             if (isset($result['data']['items'])) {
  23.                 $storeId = $this->context->getFilterParam('store_id');
  24.                  foreach ($result['data']['items'] as &$item) {
  25.                     $item[$productActions->getData('name')]['view'] = [
  26.                         'href' => $this->actionUrlBuilder->getUrl(
  27.                                 $this->urlBuilder->getUrl(
  28.                                         'catalog/product/view', ['product_id' => $item['entity_id'], 'store' => $storeId]
  29.                                 ), isset($item['_first_store_id']) ? $item['_first_store_id'] : null, isset($item['store_code']) ? $item['store_code'] : null
  30.                         ),
  31.                         'label' => __('View')
  32.                     ];
  33.                 }
  34.             }
  35.         }
  36.         return $result;
  37.     }
  38. }
  39.  
  40.  

thats is all .

Comments

IBNAB is a company made of a group of professionals whose work is providing secure open source solutions. Our company strives for reaching magnificent results with each experience and provides professional open source solutions that cover every part of the business process.

Newsletter