7

I have a custom extension that generate grid using XML, the problem is that I need to add button which will generate link with a get parameter from get request that passed via url, the xml code for button like this look like this:

<item name="buttons" xsi:type="array">
            <item name="sync" xsi:type="array">
                <item name="name" xsi:type="string">sync</item>
                <item name="label" xsi:type="string">Sync All</item>
                <item name="class" xsi:type="string">primary</item>
                <item name="url" xsi:type="string">test_test/test/massSync/post_id/#id</item>
            </item>
        </item>

The button will connect to massSync controller and I need to add the post_id that exist in window current url like this:

http://test.com/admin/test_test/test/index/post_id/1
0

AFAIK you cannot do that directly in the XML, you'll have to use a custom block to do so:

<item name="buttons" xsi:type="array">
    <item name="sync" xsi:type="string">Vendor\Module\Block\Adminhtml\My\Custom\Button</item>
</item>

Then create the corresponding Vendor\Module\Block\Adminhtml\My\Custom\Button.php file with the following content:

<?php

namespace Vendor\Module\Block\Adminhtml\My\Custom;

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

class Button implements ButtonProviderInterface 
{
    /**
     * @var Context
     */
    protected $context;

    /**
     * @param Context $context
     */
    public function __construct(
        Context $context
    ) {
        $this->context = $context;
    }

    /**
     * @return array
     */
    public function getButtonData()
    {
        $data = [];
        if ($this->getPostId()) {
            $data = [
                'id' => 'sync',
                'label' => __('Sync All'),
                'class' => 'primary',
                'url' => $this->getSyncUrl()
            ];
        }
        return $data;
    }

    /**
     * @return string
     */
    public function getSyncUrl()
    {
        return $this->getUrl('test_test/test/massSync', ['post_id' => $this->getPostId()]);
    }

    /**
     * @return int
     */    
    public function getPostId()
    {
        return $this->context->getRequest()->getParam('post_id');
    }
}

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.