Add custom UI Component (button) to product listing with POST request
-
15-04-2021 - |
Question
how can I add button that make POST request to controller next to "Add Product" button in product listing? So far I do it like this, but it isn't send POST:
Vendor/Module/view/adminhtml/ui_component/product_listing.xml
<?xml version="1.0"?>
<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="synchronize" class="Vendor\Module\Block\Adminhtml\Product\Edit\Button\CustomButton">
</button>
</buttons>
</settings>
</listing>
Vendor/Module/Block/Adminhtml/Product/Edit/Button/CustomButton
<?php
namespace Vendor\Module\Block\Adminhtml\Product\Edit\Button;
use Magento\Backend\Model\UrlInterface;
use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;
class CustomButton implements ButtonProviderInterface
{
private $urlBuilder;
public function __construct(UrlInterface $urlBuilder)
{
$this->urlBuilder = $urlBuilder;
}
public function getButtonData()
{
return [
'label'=>__('Custom label'),
'class' => 'action-secondary',
'on_click'=> "setLocation('" .
$this->urlBuilder->getUrl("path/to/controller", ['key'=>$this->urlBuilder->getSecretKey('front', 'controller', 'action')])
. "')",
'sort_order'=>100,
];
}
}
Solution
Within a post request, you need a request body (data to send along with the POST) and the UI doesn't provide body property, so it's understandable that you cannot send POST within the current implementation.
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange