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.
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 Answer 1
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', []);
}
}
-
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 RanaDivya Sekar– Divya Sekar2020年11月11日 06:43:43 +00:00Commented Nov 11, 2020 at 6:43
-
How to add two buttons any idea?Himanshu– Himanshu2021年04月20日 09:53:51 +00:00Commented Apr 20, 2021 at 9:53
sales_order_grid