2

I have a button on a admin grid in Magento 2 which need to pass URL parameter coming on the grid page like:

http://127.0.0.1/admin/vendor_module/questionsetflow/index/questionset_id/19/key/8a93789d2ff74e19ee9d8b0aabc1349419582836dea63df961a6146d7454cf38/

enter image description here

I need to pass the dynamic param questionset_id (value = 19) to button 'Add New Question' in my admin grid (ui component):

<?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">
    <argument name="data" xsi:type="array">
        <item name="js_config" xsi:type="array">
            <item name="provider" xsi:type="string">alkemy_chatbot_questionsetflow_listing.alkemy_chatbot_questionsetflow_listing_data_source</item>
            <item name="deps" xsi:type="string">alkemy_chatbot_questionsetflow_listing.alkemy_chatbot_questionsetflow_listing_data_source</item>
        </item>
        <item name="spinner" xsi:type="string">alkemy_chatbot_questionsetflow_columns</item>
        <item name="buttons" xsi:type="array">
            <item name="back" xsi:type="array">
                <item name="name" xsi:type="string">back</item>
                <item name="label" xsi:type="string" translate="true">Back</item>
                <item name="class" xsi:type="string">primary</item>
                <item name="url" xsi:type="string">alkemy_chatbot/questionset/index</item>
            </item>
            <item name="add" xsi:type="array">
                <item name="name" xsi:type="string">add</item>
                <item name="label" xsi:type="string" translate="true">Add New Question</item>
                <item name="class" xsi:type="string">primary</item>
                <item name="url" xsi:type="url" path="alkemy_chatbot/questionsetflow/new">
                    <param name="questionset_id">!!!!!!!!</param>
                </item>
            </item>
        </item>
    </argument>

What value is necessary to put in <param name="questionset_id">

Thanks

3

Use button like here

And then add following line inside buttons tag


<button name="custom_button_name" class="Vendor\Module\Block\Adminhtml\CustomButton"/>

Vendor/Module/Block/Adminhtml/CustomButton.php


namespace Vendor\Module\Block\Adminhtml;

use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;

class CustomButton implements ButtonProviderInterface
{
    /**
     * Url Builder
     *
     * @var \Magento\Framework\UrlInterface
     */
    protected $urlBuilder;

    /**
     * CustomButton constructor.
     *
     * @param \Magento\Backend\Block\Widget\Context $context
     */
    public function __construct(
        \Magento\Backend\Block\Widget\Context $context
    ) {
        $this->urlBuilder = $context->getUrlBuilder();
    }
    /**
     * @return array
     */
    public function getButtonData()
    {
        $data = [
            'label' => __('Custom Button'),
            'class' => 'save primary',
            'id' => 'custom-button',
            'on_click' => sprintf("location.href = '%s';", $this->getUrl('*/*/url', ['id' => 2])),
            'sort_order' => 20,
        ];
        return $data;
    }

    /**
     * Generate url by route and parameters
     *
     * @param   string $route
     * @param   array $params
     * @return  string
     */
    public function getUrl($route = '', $params = [])
    {
        return $this->urlBuilder->getUrl($route, $params);
    }
}

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.