3

Can I get help with this issue?

How you can add another action in the product grid in Magento 2?

I tried doing an overwrite but does not show expected, overwrites the block \Magento\Catalog\Block\Adminhtml\Product\Grid in the file di.xml:

<preference for="Magento\Catalog\Block\Adminhtml\Product\Grid" type="Vendor\Module\Block\Adminhtml\Producto\RewriteGrid" />

enter image description here

3

you can create an after plugin for Magento\Catalog\Ui\Component\Listing\Columns\ProductActions::prepareDataSource().

add this in the di.xml of your module:

<type name="Magento\Catalog\Ui\Component\Listing\Columns\ProductActions">
    <plugin name="my-plugin-name" type="[Namespace]\[Module]\Plugin\ProductActions" sortOrder="10" />
</type>

then create the file [Namespace]/[Module]/Plugin/ProductActions.php

<?php
namespace [Namespace]\[Module]\Plugin;
class ProductActions
{
    protected $context;
    protected $urlBuilder;
    public function __construct(
        \Magento\Framework\View\Element\UiComponent\ContextInterface $context,
        \Magento\Framework\UrlInterface $urlBuilder
    )
    {
        $this->context = $context;
        $this->urlBuilder = $urlBuilder;
    }
    public function afterPrepareDataSource(
        \Magento\Catalog\Ui\Component\Listing\Columns\ProductActions $subject,
        array $dataSource
    ) {
        if (isset($dataSource['data']['items'])) {
            $storeId = $this->context->getFilterParam('store_id');

            foreach ($dataSource['data']['items'] as &$item) {
                $item[$subject->getData('name')]['do_something'] = [
                    'href' => $this->urlBuilder->getUrl(
                        'catalog/product/do_something',
                        ['id' => $item['entity_id'], 'store' => $storeId]
                    ),
                    'label' => __('Do Something'),
                    'hidden' => false,
                ];
                $item[$subject->getData('name')]['do_something_else'] = [
                    'href' => $this->urlBuilder->getUrl(
                        'catalog/product/do_something_else',
                        ['id' => $item['entity_id'], 'store' => $storeId]
                    ),
                    'label' => __('Do Something else'),
                    'hidden' => false,
                ];
            }
        }

        return $dataSource;
    }
}
  • Hi @Marius, it works; a question, this is the right way to make this work. – César Feb 9 '16 at 17:53
  • I cannot say for sure but it seams like the way to go. – Marius Feb 9 '16 at 18:53
  • From everything I'm seeing the other answer is correct and this is functional but mistaken. Sorry Marius – LM_Fielding Jul 14 '17 at 8:29
  • @LM_Fielding. The other answer describes how to add a NEW column that contains an action. Using mine you will get the actions as a dropdown in the same column. This is more inline with the other magento grids. I guess it's just a matter of preference. – Marius Jul 14 '17 at 8:46
  • Isn't it best practise to merge in changes via the jsLayout or xml? Apologies, the information is very limited. Can you make any edit to your answer so I can change my vote? – LM_Fielding Jul 14 '17 at 9:58
1

Create ui_component/store_settings_listing.com and add below actionColumn element under columns node

<columns name="store_settings_columns">
        <actionsColumn name="actions" class="MageMastro\Store\Ui\Component\Listing\Column\StoreActions">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="resizeEnabled" xsi:type="boolean">false</item>
                <item name="resizeDefaultWidth" xsi:type="string">107</item>
                <item name="indexField" xsi:type="string">entity_id</item>
                <item name="sortOrder" xsi:type="number">200</item>
            </item>
        </argument>
    </actionsColumn>
</columns>

Create UI Action Class

MageMastro\Store\Ui\Component\Listing\Column\StoreActions.php

/**
 *
 * @package MageMasto Store custom actions
 * @author  Harshad Pandit <magemastro@gmail.com>
 * @created 2016-02-09
 * @copyright Copyright (c) 2016 Harshad Pandit, http://invhub.com.
 */

namespace MageMastro\Store\Ui\Component\Listing\Column;

use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\Column;
use Magento\Cms\Block\Adminhtml\Page\Grid\Renderer\Action\UrlBuilder;
use Magento\Framework\UrlInterface;

class StoreActions extends Column {

    `/** @var UrlBuilder */
    protected $actionUrlBuilder;

    /** @var UrlInterface */
    protected $urlBuilder;

    /**
     * @var string
     */
    private $_viewUrl = 'mm_store/*/view';

    /**
     * @var string
     */
    private $_editUrl = 'mm_store/*/edit';

    /**
     * @var string
     */
    private $_deleteUrl = 'mm_store/*/delete';

    /**
     * @param ContextInterface $context
     * @param UiComponentFactory $uiComponentFactory
     * @param UrlBuilder $actionUrlBuilder
     * @param UrlInterface $urlBuilder
     * @param array $components
     * @param array $data
     * @param string $editUrl
     */
    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        UrlBuilder $actionUrlBuilder,
        UrlInterface $urlBuilder,
        array $components = [],
        array $data = []
    )
    {
        $this->urlBuilder = $urlBuilder;
        $this->actionUrlBuilder = $actionUrlBuilder;
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }

    /**
     * Prepare Data Source
     *
     * @param array $dataSource
     * @return array
     */
    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as & $item) {
                $name = $this->getData('name');
                if (isset($item['entity_id'])) {
                    $item[$name]['view'] = [
                        'href' => $this->urlBuilder->getUrl($this->_viewUrl, ['store_id' => $item['entity_id']]),
                        'label' => __('View')
                    ];

                    $item[$name]['edit'] = [
                        'href' => $this->urlBuilder->getUrl($this->_editUrl, ['store_id' => $item['entity_id']]),
                        'label' => __('Edit')
                    ];
                    $item[$name]['delete'] = [
                        'href' => $this->urlBuilder->getUrl($this->_deleteUrl, ['store_id' => $item['entity_id']]),
                        'label' => __('Delete'),
                        'confirm' => [
                            'title' => __('Delete ${ $.$data.title }'),
                            'message' => __('Are you sure you wan\'t to delete a ${ $.$data.title } record?')
                        ]
                    ];
                }
            }
        }

        return $dataSource;
    }
}
  • Very good answer, well done. – LM_Fielding Jul 14 '17 at 8:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.