3

In Magento 2.2.5, I am creating admin grid by following this tutorial(Using layout Block method. Not UI Component).

In that I have a add button. I can't remove it. I need to remove it and add a new button with custom url. How to achieve this.

Screenshot:

Screenshot

1
  • Can you share your code? Oct 22 '18 at 9:21
6

You can use update

parent::_construct();
$this->buttonList->update('save', 'onclick', 'setLocation(\'' . $this->getUrl('*/*/<youraction>') . '\')');
2

Try using this code

    parent::_construct();
    $this->removeButton('add');

    $this->buttonList->add(
            '<your name>',
            [
        'label' => __('<your label>'),
        'class' => 'save',
        'onclick' => 'setLocation(\'' . $this->getUrl('*/*/<youraction>') . '\')',
        'style' => '    background-color: #ba4000; border-color: #b84002; box-shadow: 0 0 0 1px #007bdb;color: #fff;text-decoration: none;'
            ]
    );

Hope that will help you.

1
  • Yes, It is working. I have tried this but before parent::_construct(); i added this remove button code. So It is not worked. Oct 16 '18 at 7:42
2

Try using this code

     protected function _construct()
        {
            $this->_objectId = 'row_id';
            $this->_blockGroup = '[Vendor_name]_[module_name]';
            $this->_controller = 'adminhtml_[Controller_name]';
            parent::_construct();

// for remove btn

            $this->buttonList->remove('save');
            $this->buttonList->remove('back');
            $this->buttonList->remove('reset');
            
/////  for update btn name

            $this->buttonList->update('save', 'label', __('[Updateed_btn_name]'));

/// for add new custome btn
    
            $this->addButton(
                        '<custome_btn_name>',
                        [
                            'label' => __('Btn_name_to_show'),
                            'on_click' => sprintf("location.href = '%s';", $this->getUrl('[Route_name]/[controller_name]/[file_name]')),
                            'class' => 'primary',
                            'level' => 1
                        ]
                    );
        }

[Update]

Another way to add button into Ui-Component

Step-1: Create Generic.php at app/code/VendoreName/ModuleName/Block/Adminhtml/Edit/Button

<?php

namespace VendoreName\ModuleName\Block\Adminhtml\Edit\Button;

use Magento\Backend\Block\Widget\Context;
use Magento\Cms\Api\PageRepositoryInterface;

class Generic
{
    protected $context;
    protected $pageRepository;

    public function __construct(
        Context $context,
        PageRepositoryInterface $pageRepository
    ) {
        $this->context = $context;
        $this->pageRepository = $pageRepository;
    }

    public function getUrl($route = '', $params = [])
    {
        return $this->context->getUrlBuilder()->getUrl($route, $params);
    }
}

Step-2: Create Back.php at app/code/VendoreName/ModuleName/Block/Adminhtml/Edit/Button

<?php

namespace VendoreName\ModuleName\Block\Adminhtml\Edit\Button;

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

class Back extends Generic implements ButtonProviderInterface
{
    public function getButtonData()
    {
        return [
            'label' => __('Back'),
            'on_click' => sprintf("location.href = '%s';", $this->getBackUrl()),
            'class' => 'back',
            'sort_order' => 10,
        ];
    }

    public function getBackUrl()
    {
        return $this->getUrl('*/*/');
    }
}

Step-3: Create Reset.php at app/code/VendoreName/ModuleName/Block/Adminhtml/Edit/Button

<?php

namespace VendoreName\ModuleName\Block\Adminhtml\Edit\Button;

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

class Reset implements ButtonProviderInterface
{
    public function getButtonData()
    {
        return [
            'label' => __('Reset'),
            'class' => 'reset',
            'on_click' => 'location.reload();',
            'sort_order' => 30,
        ];
    }
}

Step-4: Create Delete.php at app/code/VendoreName/ModuleName/Block/Adminhtml/Edit/Buttons

<?php

namespace VendoreName\ModuleName\Block\Adminhtml\Edit\Button;

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

class Delete extends Generic implements ButtonProviderInterface
{
    protected $context;

    public function __construct(
        Context $context
    ) {
        $this->context = $context;
    }

    public function getButtonData()
    {
        $data = [];
        $id = $this->context->getRequest()->getParam('id');
        if ($id) {
            $data = [
                'label' => __('Delete'),
                'class' => 'delete',
                'on_click' => 'deleteConfirm(\'' . __(
                    'Are you sure you want to delete this?'
                ) . '\', \'' . $this->getDeleteUrl() . '\')',
                'sort_order' => 20,
            ];
        }
        return $data;
    }

    public function getDeleteUrl()
    {
        $id = $this->context->getRequest()->getParam('id');
        return $this->getUrl('*/*/delete', ['id' => $id]);
    }
}

Step-5: Create Save.php at app/code/VendoreName/ModuleName/Block/Adminhtml/Edit/Button

<?php

namespace VendoreName\ModuleName\Block\Adminhtml\Edit\Button;

use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;
use Magento\Ui\Component\Control\Container;

class Save extends Generic implements ButtonProviderInterface
{
    public function getButtonData()
    {
        return [
            'label' => __('Save'),
            'class' => 'save primary',
            'data_attribute' => [
                'mage-init' => [
                    'buttonAdapter' => [
                        'actions' => [
                            [
                                'targetName' => 'your_form_ui_component_name.your_form_ui_component_name',
                                'actionName' => 'save',
                                'params' => [
                                    false,
                                ],
                            ],
                        ],
                    ],
                ],
            ],
        ];
    }
}

Step-6: In your Form Ui-Component file at app/code/VendoreName/ModuleName/view/adminhtml/ui_component add below code:

................................................................................................................
<argument name="data" xsi:type="array">
    ............................................................................................................
    <item name="buttons" xsi:type="array">
        <item name="back" xsi:type="string">VendoreName\ModuleName\Block\Adminhtml\Edit\Button\Back</item>
        <item name="delete" xsi:type="string">VendoreName\ModuleName\Block\Adminhtml\Edit\Button\Delete</item>
        <item name="reset" xsi:type="string">VendoreName\ModuleName\Block\Adminhtml\Edit\Button\Reset</item>
        <item name="save" xsi:type="string">VendoreName\ModuleName\Block\Adminhtml\Edit\Button\Save</item>
    </item>
    ............................................................................................................
</argument>
................................................................................................................

I Hope This Helps You.

0
0

use this if you are using ui component

   <item name="buttons" xsi:type="array">
        <item name="complete" xsi:type="array">
            <item name="name" xsi:type="string">add</item>
            <item name="label" xsi:type="string">View Complete Orders</item>
            <item name="class" xsi:type="string">primary</item>
            <item name="url" xsi:type="string">afrax/create/complete</item>
        </item>
         <item name="pending" xsi:type="array">
            <item name="processing" xsi:type="string">add</item>
            <item name="label" xsi:type="string">View Pending Orders</item>
            <item name="class" xsi:type="string">primary</item>
            <item name="url" xsi:type="string">afrax/create/index</item>
        </item>
        <item name="cancel" xsi:type="array">
            <item name="processing" xsi:type="string">add</item>
            <item name="label" xsi:type="string">View Cancel Orders</item>
            <item name="class" xsi:type="string">primary</item>
            <item name="url" xsi:type="string">afrax/create/canceled</item>
        </item>
    </item>

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.