1

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,
            
        ];
    }
}

1 Answer 1

0

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.

Your Answer

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

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