2

I have a custom module with an preview of orders with custom functionality. But on that preview page i want a 'Go Back' button to go back to the main overview of orders. Is there a way to add such a button on the preview.phtml file? I am fairly new to magento 2 so code examples would be awesome.

4 Answers 4

2

In administration, these types of buttons are part of the 'Container'. Buttons like Save,Reset, Delete etc...

To add another button you need to overload _construct method in your block that extends

\Magento\Backend\Block\Widget\Form\Container

This will be usually located in something like:

\Namespace\Module\Block\Adminhtml\Edit.php

Or whatever your structure is. Look for the \Magento\Backend\Block\Widget\Form\Container

In this container add following method:

protected function _construct()
{
    $this->_objectId   = 'xxxxx';
    $this->_blockGroup = 'Namespace_Module';
    $this->_controller = 'adminhtml_xxxx';

    parent::_construct();

    $this->addButton(
        'my_back_button',
        [
            'label' => __('My Back Button'),
            'onclick' => 'setLocation(\'' . $this->getUrl('router/controller/action') . '\')',
            'class' => 'back'
        ],
        -1
    );
}

If you really want to add this link in .phtml file, you can add something like this:

<a href="<?php echo $block->getUrl('router/controller/action') ?>"><?php echo __('Go Back') ?></a>
3

In your _construct function of your container block call the parent::_construct() and then update its values if needed:

class Edit extends Container
{
    public function __construct(
        Context $context,
        Registry $registry,
        array $data = []
    ) {
        $this->_coreRegistry = $registry;
        parent::__construct($context, $data);
    }

    protected function _construct()
    {
        $this->_objectId = 'V_M';
        $this->_blockGroup = 'V_M';
        $this->_controller = 'adminhtml_promo_vm';
        parent::_construct();//It sets back, reset, delete, and save buttons with default values
        $this->buttonList->update('save', 'label', __('Save M'));
        $this->buttonList->update('back', 'onclick', "setLocation('" . $this->getUrl('sales_rule/promo_quote/index') . "')");
    }
}

Update:

Sorry did not notice that you wanted it the .phtml file, so add a simple button with a setLocation and use $block->getUrl() (I think).

Note: getUrl() is Magento\Framework\View\Element\AbstractBlock::getUrl())

0

I know this has already been accepted, but there is a more elegant way to do this than the accepted answer.

In your block class (Vendor\Module\Block\...\Edit), add

public function getBackUrl() {
    return $this->getUrl('<Desired URL>');
}

This works by overriding the getBackUrl() method in Magento\Backend\Block\Widget\Form\Container where the back button URL is generated in the constructor:

protected function _construct()
{
    parent::_construct();

    $this->addButton(
        'back',
        [
            'label' => __('Back'),
            'onclick' => 'setLocation(\'' . $this->getBackUrl() . '\')',
            'class' => 'back'
        ],
        -1
    );

    ...
}

This can also be done with the Delete and Save buttons.

0

You can extend your Block class form \Magento\Backend\Block\Widget\Form\Container and you'll get Save, Reset, Delete, Back buttons automatically. If you want completely implement your own logic then better extend for one level lower from \Magento\Backend\Block\Widget\Container.

For example:

    <?php
    
    namespace Vendor\Module\Block\Adminhtml\Edit\Buttons;
    
    use Magento\Backend\Block\Widget\Container;
    
    class BackButton extends Container
    {
        /**
         * @return void
         */
        protected function _construct()
        {
            parent::_construct();
    
            $this->addButton(
                'back',
                [
                    'label' => __('Back'),
                    'onclick' => 'setLocation(\'' . $this->getBackUrl() . '\')',
                    'class' => 'back',
                ],
                -1
            );
        }
    
        /**
         * Get URL for back button.
         *
         * @return string
         */
        protected function getBackUrl(): string
        {
            return $this->getUrl('<any path>', [<any params>]);
        }
    }

And you should add to your layout this code:

     <referenceContainer name="content">
        <block class="Customization\OrderEdit\Block\Adminhtml\Order\Edit\BackButton" name="edit_order_back_button"/>
     </referenceContainer>

That's enough for proper work. enter image description here

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.