Mass action in the grid in tab redirects to dashboard in magento2
-
29-09-2020 - |
Question
I've a form with multiple tabs and in one tab there is a grid with mass actions.
But when i click on Submit button, after selecting the massaction, the control is redirected to dashboard.
this is the code to get the mass action
protected function _prepareMassaction()
{
$this->setMassactionIdField('sku_id');
$this->getMassactionBlock()->setTemplate('Namespace_Module::massaction_extended.phtml');
//$this->setMassactionIdFieldOnlyIndexValue(true);
$this->getMassactionBlock()->setFormFieldName('universalProductsGrid');
$this->getMassactionBlock()->addItem(
'delete',
[
'label' => __('Remove Global SKUs'),
'url' => $this->getUrl('namespace_module/*/massDeleteSku',['id'=>$this->getRequest()->getParam('id')]),
'confirm' => __('Are you sure?')
]
);
return $this;
}
EDIT I have found the problem, the form_key was not rendered in the massaction form, so temporary i added the below function in the file
magento-root/vendor/magento/module-backend/Block/Widget/Grid/Massaction/Extended.php
public function getFormKey()
{
return $this->formKey->getFormKey();
}
and then in the file Namespace_Module::massaction_extended.phtml
replaced <?php echo $block->getBlockHtml('formkey')?>
with
<input name="form_key" type="hidden" value="<?php //echo $block->getFormKey() ?>" />
But now my problem is how do i change this in my module block, instead of changing the core block.
Thanks.
Solution
You can add formkey using <update handle="formkey"/>
inside your all grid related layout xml files.
ex.module_index_grid.xml
code for test,
<?xml version="1.0"?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
<update handle="formkey"/>
<body>
//code for body
</body>
</layout>
Try using this.