0

I created a button via following code:

<?php

namespace UV\Bestellung\Plugin;

class Buttons
{
public function beforePushButtons(
    \Magento\Backend\Block\Widget\Button\Toolbar\Interceptor $subject,
    \Magento\Framework\View\Element\AbstractBlock $context,
    \Magento\Backend\Block\Widget\Button\ButtonList $buttonList
) {

    $this->_request = $context->getRequest();
    if($this->_request->getFullActionName() == 'sales_order_view'){
        $buttonList->add(
            'bestellschein',
            ['label' => __('Bestellschein'), 'onclick' => 'setLocation(window.location.href)', 'class' => 'reset'],
            -1
        );
    }
}
}

?>

It appears and it is clickable but obviously it has no logic and is doing nothing. How can I give it a logic and implement it?

Thanks!:-)

2

You can create controller and implement the logic there (check $url). When you click on the button, you will reach to your controller and there you can implement your logic.

namespace UV\Bestellung\Plugin;

class Buttons
{
    public function beforePushButtons(
        \Magento\Backend\Block\Widget\Button\Toolbar\Interceptor $subject,
        \Magento\Framework\View\Element\AbstractBlock $context,
        \Magento\Backend\Block\Widget\Button\ButtonList $buttonList
    ) {

        $this->_request = $context->getRequest();
        if($this->_request->getFullActionName() == 'sales_order_view'){
            $url = $subject->getUrl('custom_module/order/custom/id/'.$SOMEID);
            $buttonList->add(
                'bestellschein',
                ['label' => __('Bestellschein'), 'onclick' => 'setLocation(\'{$url}\')', 'class' => 'reset'],
                -1
            );
        }
    }
}

In Vendor/Module/etc/adminhtml/routers.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="admin">
        <route frontName="custom_module" id="custom_module">
            <module before="Magento_Backend" name="Vendor_Module"/>
        </route>
    </router>
</config>

In Vendor/Module/Controller/Adminhtml/Order/Custom.php

namespace Vendor\Module\Controller\Adminhtml\Order;

class Custom  extends \Magento\Sales\Controller\Adminhtml\Order
{


    public function execute()
    {
        // your logic here 
    }
}
  • Ok it doesn't work yet. My Custom.php is located here now: UV/Bestellung/Controller/Adminhtml/Order/Custom.php. " $url = $subject->getUrl('UV/Bestellung/Controller/Order/Custom');" doesn't work. – k33n Mar 15 '18 at 11:54
  • Did you create routers.xml file? you have to create controller and url properly. – Prakash Thapa Mar 15 '18 at 12:03
  • yes I created it. That is it: '<config xmlns:xsi="w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd"> <router id="admin"> <route frontName="UV_Bestellung" id="UV_Bestellung"> <module before="Magento_Backend" name="Vendor_Module"/> </route> </router> </config>' – k33n Mar 15 '18 at 12:08
  • 1
    if you created router.xml why is the $url is still : $url = $subject->getUrl('UV/Bestellung/Controller/Order/Custom'); ? The url should be: $subject->getUrl('UV_Bestellung/order/custom'); (all lower case) check first how to controller magento controller and URL – Prakash Thapa Mar 15 '18 at 12:16
  • see in my answer what I have done. I dont know why it is not working :-( – k33n Mar 15 '18 at 12:33
0

I have done that now:

UV/Bestellung/Plugin/Buttons.php

<?php

namespace UV\Bestellung\Plugin;

class Buttons
{
public function beforePushButtons(
    \Magento\Backend\Block\Widget\Button\Toolbar\Interceptor $subject,
    \Magento\Framework\View\Element\AbstractBlock $context,
    \Magento\Backend\Block\Widget\Button\ButtonList $buttonList
) {

    $this->_request = $context->getRequest();
    if($this->_request->getFullActionName() == 'sales_order_view'){
        $url = $subject->getUrl('UV_Bestellung/order/custom');
        $buttonList->add(
            'bestellschein',
            ['label' => __('Bestellschein'), 'onclick' => 'setLocation(\'{$url}\')', 'class' => 'reset'],
            -1
        );
    }
}
}

?>

UV/Bestellung/etc/adminhtml/routes.xml

<config>
    <router id="admin">
        <route id="bestellschein" frontName="Bestellschein">
            <module name="UV_Bestellung" before="UV_Bestellung"/>
        </route>
    </router>
</config>

UV/Bestellung/Controller/Adminhtml/Order/Custom.php

<?php
namespace UV\Bestellung\Controller\Adminhtml\Order;
class Custom extends \Magento\Sales\Controller\Adminhtml\Order
{
    public function execute()
    {
        // your logic here 
    }
}
?>

When I call the page where the button is located, it is just blank. Error logs says that this line is the problem:

$url = $subject->getUrl('UV_Bestellung/order/custom');
  • 1
    $url = $subject->getUrl('UV_Bestellung/order/custom'); Should be $url = $subject->getUrl('Bestellschein/order/custom'); In routers.xml, the frontName='...' is the URL of magento magento please check URL => MAGENTO_URL/ADMINPATH/bestellschein/order/custom is accessible? – Prakash Thapa Mar 15 '18 at 12:42
  • 404 Site not found. What do you mean by frontName="..." is the URL of Magento? – k33n Mar 15 '18 at 12:48
  • check routers.xml <route id="bestellschein" frontName="Bestellschein"> frontName should be the part of URL. in your example: Bestellschein not UV_Bestellung – Prakash Thapa Mar 15 '18 at 12:55

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.