1

I want to add a new custom button to the sales order grid pageactions, i was trying creating Plugin for "Magento\Backend\Block\Widget\Button\Toolbar" but in the grid containers this block is not used, so thats no work.

Instead the pageactions.phtml is used like the image shows.

Example of the new button

How can I add the button? with a LayoutProcessorPlugin?

Very Important: I want to restrict the button to users with access to a custom Access Role that I have created.

  • 1
    you can start by overriding sales_order_grid – fmsthird Feb 10 at 1:50
4

Try following way:

app/code/SR/MagentoStackExchange/view/adminhtml/ui_component/sales_order_grid.xml


<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <settings>
        <buttons>
            <button name="CustomButton" class="SR\MagentoStackExchange\Block\Adminhtml\Sales\CustomButton"/>
        </buttons>
    </settings>
</listing>

app/code/SR/MagentoStackExchange/Block/Adminhtml/Sales/CustomButton.php

Here I have used Magento_Cms::save for ACL, replace your own.


<?php
namespace SR\MagentoStackExchange\Block\Adminhtml\Sales;

use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;
use Magento\Backend\Block\Widget\Context;
use Magento\Framework\AuthorizationInterface;

class CustomButton implements ButtonProviderInterface
{
    /**
     * @var AuthorizationInterface
     */
    private $authorization;

    /**
     * @var Context
     */
    private $context;

    /**
     * CustomButton constructor.
     *
     * @param AuthorizationInterface $authorization
     * @param Context $context
     */
    public function __construct(
        AuthorizationInterface $authorization,
        Context $context
    ) {
        $this->authorization = $authorization;
        $this->context = $context;
    }

    /**
     * @return array
     */
    public function getButtonData()
    {
        if (!$this->authorization->isAllowed('Magento_Cms::save')) {
            return [];
        }

        return [
            'label' => __('Custom Button'),
            'on_click' => sprintf("location.href = '%s';", $this->getBackUrl()),
            'class' => 'primary',
            'sort_order' => 10
        ];
    }

    /**
     * Get URL for back (reset) button
     *
     * @return string
     */
    public function getBackUrl()
    {
        return $this->context->getUrlBuilder()->getUrl('sales/order_create/start', []);
    }
}

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.