0

I made a custom button in sales->orders. enter image description here

Which should direct to another external url(www.google.com).

In my app/code/vendor/module/Block/Adminhtml/Sales i have: CustomButton.php

<?php
namespace Vendor\Module\Block\Adminhtml\Sales;

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


class CustomButton implements ButtonProviderInterface
{
    /**
     * @var AuthorizationInterface
     */
    private $authorization;

    /**
     * @var Context
     */
    private $context;

    /**
     * CustomButton constructor.
     *
     * @param AuthorizationInterface $authorization
     * @param Context $context
     */
    public function __construct(
        AuthorizationInterface $authorization,
        Context $context
    ) {
        $this->authorization = $authorization;
        $this->context = $context;
    }

    /**
     * @return array
     */
    public function getButtonData()
    {
 

        return [
            'label' => __('Custom Button'),
            'on_click' => sprintf("location.href = '%s';", $this->getBackUrl()),
            'class' => 'primary',
            'sort_order' => 10
        ];
    }

    /**
     * Get URL for back (reset) button
     *
     * @return string
     */
    public function getBackUrl()
    {
        return $this->context->getUrlBuilder()->getUrl('www.google.com', []);
    }
}

and in view/adminhtml/ui_component i have sales_order_grid.xml

<?xml version="1.0" encoding="UTF-8"?>
<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="CustomButton" class="Vendor\Module\Block\Adminhtml\Sales\CustomButton"/>
        </buttons>
    </settings>
</listing>

When i click the custom button to go www.google.com i get: enter image description here

Could use some pointers on how to achive this.

1 Answer 1

0

Try This

<?php

namespace Vendor\Module\Block\Adminhtml\Sales;

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


class CustomButton implements ButtonProviderInterface
{
    /**
     * @var AuthorizationInterface
     */
    private $authorization;

    /**
     * @var Context
     */
    private $context;

    /**
     * CustomButton constructor.
     *
     * @param AuthorizationInterface $authorization
     * @param Context $context
     */
    public function __construct(
        AuthorizationInterface $authorization,
        Context $context
    ) {
        $this->authorization = $authorization;
        $this->context = $context;
    }

    /**
     * @return array
     */
    public function getButtonData()
    {


        return [
            'label' => __('Custom Button'),
            'on_click' => sprintf("location.href ='https://www.google.com/'"),
            'class' => 'primary',
            'sort_order' => 50
        ];
    }
}
2
  • Oh Thanks alot it works! any idea on how can i make it so it opens in a new tab?
    – RegularGuy
    Dec 17, 2020 at 6:55
  • 1
    i got it myself i used parent.open() and it seems to do the trick. Thanks again tho.
    – RegularGuy
    Dec 17, 2020 at 7:08

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.