Magento 2 Custom MassAction redirects to dashboard with “Invalid Security Token” but works?
-
14-04-2021 - |
Question
I created a custom mass action, and it shows up and seems to function fine except rather than redirecting back to the order page, it goes to the admin dashboard and says the typical "Invalid security or form key. Please refresh the page."
I've removed most of the code for the actual mass action part of it and narrowed it down to something I must be doing wrong with the redirect.
I've done the usual setup:di:compile, setup:upgrade, cache:flush to attempt to fix it but I am stuck with this for now.
Here's my MassAction.php
<?php
namespace Vendor\Module\Controller\Adminhtml\Export;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\Controller\ResultFactory;
class Pdf extends \Magento\Sales\Controller\Adminhtml\Order\AbstractMassAction
{
protected $fileFactory;
protected $csvProcessor;
protected $directoryList;
protected $timezone;
protected $request;
protected $scopeConfig;
protected $collectionFactory;
protected $_orderHelper;
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Ui\Component\MassAction\Filter $filter,
\Magento\Sales\Model\ResourceModel\Order\CollectionFactory $collectionFactory,
\Magento\Framework\App\Response\Http\FileFactory $fileFactory,
\Magento\Framework\File\Csv $csvProcessor,
\Magento\Framework\App\Filesystem\DirectoryList $directoryList,
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
){
parent::__construct($context, $filter);
$this->fileFactory = $fileFactory;
$this->csvProcessor = $csvProcessor;
$this->directoryList = $directoryList;
$this->collectionFactory = $collectionFactory;
$this->timezone = $timezone;
$this->scopeConfig = $scopeConfig;
}
protected function massAction(AbstractCollection $collection)
{
$orderIds = $collection->getAllIds();
file_put_contents("/report.txt","Works"); // this is fired and written to disk
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$resultRedirect->setPath($this->getComponentRefererUrl()); // rather than returning to the order panel, it does to the admin dashboard?
return $resultRedirect;
}
}
Solution
- the ADMIN_RESOURCE was not set
- the redirect url needed to be somewhat different to the core as your massaction url is in another module that sale/order
The code below works better
<?php
namespace Egghead\LabelMaker\Controller\Adminhtml\Export;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Ui\Component\MassAction\Filter;
class Labels extends \Magento\Sales\Controller\Adminhtml\Order\AbstractMassAction
implements \Magento\Framework\App\Action\HttpPostActionInterface
{
/**
* Authorization level of a basic admin session
*/
const ADMIN_RESOURCE = 'Egghead_LabelMaker::labelmaker';
protected $fileFactory;
protected $csvProcessor;
protected $directoryList;
protected $timezone;
protected $request;
protected $scopeConfig;
protected $collectionFactory;
protected $_orderHelper;
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Ui\Component\MassAction\Filter $filter,
\Magento\Sales\Model\ResourceModel\Order\CollectionFactory $collectionFactory,
\Magento\Framework\App\Response\Http\FileFactory $fileFactory,
\Magento\Framework\File\Csv $csvProcessor,
\Magento\Framework\App\Filesystem\DirectoryList $directoryList,
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
){
parent::__construct($context, $filter);
$this->fileFactory = $fileFactory;
$this->csvProcessor = $csvProcessor;
$this->directoryList = $directoryList;
$this->collectionFactory = $collectionFactory;
$this->timezone = $timezone;
$this->scopeConfig = $scopeConfig;
}
protected function massAction(AbstractCollection $collection)
{
$orderIds = $collection->getAllIds();
file_put_contents("/report.txt","Works"); // this is fired and written to disk
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('sales/order');
return $resultRedirect;
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange