0

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;
    }
}

1 Answer 1

1
  1. the ADMIN_RESOURCE was not set
  2. 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;
                }
            }
8
  • I believe that may have worked, it now says Fatal error: Uncaught Error: Call to a member function create() on null in /srv/public_html/vendor/magento/module-sales/Controller/Adminhtml/Order/AbstractMassAction.php:61 which I traced to being something about it not liking the collection
    – joeybab3
    Commented Jul 23, 2020 at 15:29
  • Hmm ok so that still didn't solve the issue, regardless is it better practice to use */*/ vs the line I used previously?
    – joeybab3
    Commented Jul 23, 2020 at 16:38
  • No worries, thanks for the responses, what other files would help? This is the entirety of the Pdf.php file, is there something else that could help?
    – joeybab3
    Commented Jul 23, 2020 at 16:47
  • Ideally the module that does make up the screen but not mandatory.. if I can have your issue on my local of course it speed the solution and make it more reliable Commented Jul 23, 2020 at 16:49
  • No worries, thanks for all your help
    – joeybab3
    Commented Jul 23, 2020 at 17:05

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.