Sending Data From Magento Admin Grid to Controller: A Comprehensive Guide
If you’re building a custom admin grid in Magento and need to pass data from a column to your controller, you’ve come to the right place. In this guide, we’ll walk through the process step-by-step, providing clear examples and explaining the key concepts involved.
Understanding Magento Admin Grid Configuration
Before diving into sending data, let’s first understand how an admin grid is configured in Magento.
An admin grid is defined by a block class that extends Magento\Backend\Block\Widget\Grid. This block contains settings for the column definitions, which are arrays of key-value pairs. Each array corresponds to a single column and includes properties like ‘header’, ‘index’, ‘column_name’, etc.
The ‘getter’ property is particularly important for our discussion today. It determines how the value for this column is obtained from the data collection (usually an array of objects).
Passing Data From Grid Column to Controller
Now, let’s address the main question: How can we pass a value from a grid column to the controller?
Method 1: Using Form Fields and GET Parameters
One straightforward approach is to use form fields for your grid columns. When you configure a field using the ‘index’ property, Magento automatically includes it as a URL parameter when the row is clicked.
Here’s an example:
protected function _prepareColumns()
{
$this->addColumn(
'my_field',
[
'header' => __('My Field'),
'index' => 'my_field',
'type' => 'number'
]
);
}
In this case, when you click a row, Magento will generate a URL like your-grid-url?my_field=123. The value of the column is passed as a GET parameter called “my_field”.
Method 2: Using Custom Actions and POST Parameters
If you need to send multiple values or complex data structures, it’s better to use a custom action.
First, define your action in the controller:
public function myCustomAction()
{
$parameters = $this->getRequest()->getPost();
// Process the parameters here...
}
Then, in your grid configuration, set up a URL for this action under ‘actions’:
protected function _prepareColumns()
{
$this->addColumn(
'my_field',
[
'header' => __('My Field'),
'index' => 'my_field',
'type' => 'number',
'actions' => [
[
'url' => $this->getUrl('adminhtml/mymodule/myCustomAction'),
'label' => __('Process')
]
]
]
);
}
When you click the “Process” link, Magento will send a POST request to myCustomAction with all the grid data in the request body. This is perfect for sending complex data structures.
Conclusion
In this post, we’ve explored how to pass data from an admin grid column to a controller in Magento. We covered two main methods: using form fields and GET parameters, and using custom actions and POST parameters.
Remember that the choice between these methods depends on your specific use case and requirements. For simple values like IDs or integers, GET can be enough. But for more complex data, POST via a custom action is usually the best approach.
If you’re looking to dive deeper into Magento development, I recommend checking out our comprehensive Magento tutorials and guides at Laravel Company. We have plenty of resources on admin grid configuration, controller actions, and more.
Happy coding!