1

In my custom module to displaying the button on customer edit page (adminhtml), when hit the custom button it will call my custom controller after executed again I am redirecting to customer edit page. Here My custom controller is called, but when it redirecting Error is throwing.

Error: Fatal error: Call to undefined method Magento\Backend\Model\View\Result\Page\Interceptor::setUrl() in C:\xampp\htdocs\magentoee21\app\code\SR\StackExchange\Controller\Adminhtml\Stackexchange\Customer.php on line 22 Call Stack

and my code is:

root/app/code/SR/StackExchange/view/base/ui_component/customer_form.xml

<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <argument name="data" xsi:type="array">
        <item name="buttons" xsi:type="array">
            <item name="custom_button" xsi:type="string">SR\StackExchange\Block\Adminhtml\Edit\CustomButton</item>
        </item>
    </argument>
</form>

root/app/code/SR/StackExchange/Block/Adminhtml/Edit/CustomButton.php

<?php

namespace SR\StackExchange\Block\Adminhtml\Edit;
use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;

class CustomButton extends \Magento\Customer\Block\Adminhtml\Edit\GenericButton implements ButtonProviderInterface
{

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

        if($this->getCustomerId()){
            return [
                'label' => __('Custom Button'),
                'on_click' => sprintf("location.href = '%s';", $this->getCustomerUrl()),
                'class' => 'action-secondary',
                'sort_order' => 10
            ];
        }
    }

    /**
     * Get URL for back (reset) button
     *
     * @return string
     */
    public function getCustomerUrl()
    {
        $params = [
            'id' => $this->getCustomerId(),
            'store' => 0
        ];

        return $this->getUrl('stackexchange/stackexchange/customer',$params);
    }

}

//Admin controller

 <?php

    namespace SR\StackExchange\Controller\Adminhtml\Stackexchange;

    use \Magento\Customer\Controller\Adminhtml\Index\Edit;
    use Magento\Framework\Controller\ResultFactory;

    class Customer extends \Magento\Customer\Controller\Adminhtml\Index\Edit
    {

        public function execute()
        {
            $resultRedirect = $this->resultPageFactory->create(ResultFactory::TYPE_REDIRECT);
            $customer_Id = (int) $this->getRequest()->getParam('id');
            $store = $this->getRequest()->getParam('store');
            $customer = $this->_customerRepository->getById($customer_Id);

            //file_put_contents('Customer.txt', print_r($customer_Id,true), FILE_APPEND);

//Method 1
            //$resultRedirect->setPath('customer/index/edit/',['id' => $customer_Id, 'store' => $store,'_current' => true]);
            //return $resultRedirect;

//Method 2
            $resultRedirect->setUrl($this->_redirect->getRefererUrl());
            return $resultRedirect;

        }

    }

I have tried 2 different methods, but it's not working for me.

Could you please throw me right direction.

1 Answer 1

0

I have resolved my self to changing the controller method

public function execute()
        {
            $resultRedirect = $this->resultPageFactory->create(ResultFactory::TYPE_REDIRECT);
            $customer_Id = (int) $this->getRequest()->getParam('id');
            $store = $this->getRequest()->getParam('store');
            $customer = $this->_customerRepository->getById($customer_Id);

            //file_put_contents('Customer.txt', print_r($customer_Id,true), FILE_APPEND);

//Method 1
            //$resultRedirect->setPath('customer/index/edit/',['id' => $customer_Id, 'store' => $store,'_current' => true]);
            //return $resultRedirect;

//Method 2
            $resultRedirect->setUrl($this->_redirect->getRefererUrl());
            return $resultRedirect;

        }

to

public function execute()
    {
        $resultRedirect = $this->resultRedirectFactory->create();
        $customer_Id = (int) $this->getRequest()->getParam('id');
        $store = $this->getRequest()->getParam('store');
        $customer = $this->_customerRepository->getById($customer_Id);
        $resultRedirect->setPath('customer/index/edit/',['id' => $customer_Id, 'store' => $store,'_current' => true]);
        return $resultRedirect;
    }

Now it's working fine. If some one have better answer feel free to update this.

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.