TransWikia.com

Magento 2 Custom MassAction redirects to dashboard with "Invalid Security Token" but works?

Magento Asked on December 2, 2021

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

use MagentoFrameworkModelResourceModelDbCollectionAbstractCollection;
use MagentoStoreModelScopeInterface;
use MagentoFrameworkControllerResultFactory;
 
class Pdf extends MagentoSalesControllerAdminhtmlOrderAbstractMassAction
{
    protected $fileFactory;
    protected $csvProcessor;
    protected $directoryList;
    protected $timezone;
    protected $request;
    protected $scopeConfig;
    protected $collectionFactory;
    protected $_orderHelper;
    
    public function __construct(
        MagentoBackendAppActionContext $context,
        MagentoUiComponentMassActionFilter $filter,
        MagentoSalesModelResourceModelOrderCollectionFactory $collectionFactory,
        MagentoFrameworkAppResponseHttpFileFactory $fileFactory,
        MagentoFrameworkFileCsv $csvProcessor,
        MagentoFrameworkAppFilesystemDirectoryList $directoryList,
        MagentoFrameworkStdlibDateTimeTimezoneInterface $timezone,
        MagentoFrameworkAppConfigScopeConfigInterface $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;
    }
}

One Answer

  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 EggheadLabelMakerControllerAdminhtmlExport;
            
            use MagentoBackendAppActionContext;
            use MagentoFrameworkModelResourceModelDbCollectionAbstractCollection;
            use MagentoSalesModelResourceModelOrderCollectionFactory;
            use MagentoStoreModelScopeInterface;
            use MagentoFrameworkControllerResultFactory;
            use MagentoUiComponentMassActionFilter;
            
            class Labels extends MagentoSalesControllerAdminhtmlOrderAbstractMassAction
                implements MagentoFrameworkAppActionHttpPostActionInterface
            {
                /**
                 * 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(
                    MagentoBackendAppActionContext $context,
                    MagentoUiComponentMassActionFilter $filter,
                    MagentoSalesModelResourceModelOrderCollectionFactory $collectionFactory,
                    MagentoFrameworkAppResponseHttpFileFactory $fileFactory,
                    MagentoFrameworkFileCsv $csvProcessor,
                    MagentoFrameworkAppFilesystemDirectoryList $directoryList,
                    MagentoFrameworkStdlibDateTimeTimezoneInterface $timezone,
                    MagentoFrameworkAppConfigScopeConfigInterface $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;
                }
            }

Answered by Herve Tribouilloy on December 2, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP