2

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.

Teja Bhagavan Kollepara
3,8275 gold badges33 silver badges69 bronze badges
asked Feb 9, 2019 at 22:02
1
  • 1
    you can start by overriding sales_order_grid Commented Feb 10, 2019 at 1:50

1 Answer 1

6

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', []);
 }
}
answered Feb 10, 2019 at 6:09
2
  • Here I want to create a modal when this button was clicked. So i have to pass this URL in this function 'onclick' => "",... Is there an idea from your side? @Sohel Rana Commented Nov 11, 2020 at 6:43
  • How to add two buttons any idea? Commented Apr 20, 2021 at 9:53

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.