0

So, as the title says, I want to add an Admin button in the Edit Product Admin page. That button, when pressed, will call the respective controller of a URL, sending along the product id, the Controller will do something with it, and when the operation is done I want to land back in the specific product's Edit Product page, optionally together with a completion message.

This is the file view\adminhtml\ui_component\product_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="update" xsi:type="array">
                <item name="name" xsi:type="string">update</item>
                <item name="label" xsi:type="string" translate="true">Update Price</item>
                <item name="url" xsi:type="string">mymodule/priceupdate/update</item>
                <item name="class" xsi:type="string">primary</item>
            </item>
        </item>
    </argument>
</form>

This is my etc\adminhtml\routes.xml file:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="admin">
        <route id="mymodule" frontName="mymodule">
            <module name="MyCompany_MyModule" />
        </route>
    </router>
</config>

And this is the Controller\Adminhtml\PriceUpdate\UpdateAction.php file:

<?php declare(strict_types=1);
namespace MyCompany\MyModule\Controller\Adminhtml\PriceUpdate;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\Controller\Result\Json;
use Magento\Framework\Controller\Result\JsonFactory;

class UpdateAction extends Action implements HttpPostActionInterface
{

    /**
     * Undocumented variable
     *
     * @var JsonFactory
     */
    protected $jsonFactory;

    /**
     * Undocumented function
     *
     * @param Context $context
     * @param JsonFactory $jsonFactory
     */
    public function __construct(
        Context $context,
        JsonFactory $jsonFactory)
    {
        parent::__construct($context);
        $this->jsonFactory = $jsonFactory;
    }

    public function execute()
    {
        $json = $this->jsonFactory->create();
        $messages = [];
        $error = false;

        return $json->setData([
            'messages' => $messages,
            'error' => $error
        ]);
    }
}

For start, the file is as it is to have it do nothing (just get called and return to Product Edit Page) and when that goes ok, I will add the functionality. This however fails, redirecting me to the Dashboard with a message saying Invalid security or form key. Please refresh the page.. The structure I used for the UpdateAction came from a tutorial I did on Magento. The Button appears fine.

Any help about how I will achieve the desired functionality, or at least how the non-functional request will work so as to then search further, is welcome.

1 Answer 1

0

My solution, set path back to referrer:

...
use Magento\Framework\App\Response\RedirectInterface;

class...
{ 
   ...
   private $redirect;

   public function __construct(
      ...
      RedirectInterface $redirect
   ) {
      ...
      $this->redirect = $redirect;
   }

   public function execute()
   {
      ...
      
      return $resultRedirect->setPath($this->redirect->getRefererUrl());
   }

   ...
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.