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.

asked Dec 16, 2020 at 14:47

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
 ];
 }
}
answered Dec 16, 2020 at 17:18
2
  • Oh Thanks alot it works! any idea on how can i make it so it opens in a new tab? Commented 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. Commented Dec 17, 2020 at 7:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.