Importing Custom CSV Files into Magento from Your Custom Module
Hello developers! Today, I’m going to guide you through importing custom CSV files from your own module into a Magento database table. Let’s assume you’ve already created a module for managing region information and want to import multiple records at once.
Understanding the Problem
You’re new to Magento, have set up a custom module for regions, and now need an “import” button on the grid view that allows you to upload a CSV file containing multiple region entries. The challenge is getting the CSV selection menu to appear after clicking the import button.
The Solution
Step 1: Create the Import Function
First, create the function for handling the import in your module’s controller:
namespace YourCompany\YourModule\Controller Adminhtml;
class Region extends \Magento\Backend\App\Action {
// ... Other controllers functions ...
public function execute()
{
$this->_view->loadLayout();
$this->_view->getLayout()->initMessages();
if ($this->getRequest()->isPost()) {
$data = $this->getRequest()->getPost('regions');
// Save the data in your region table
$model->save($data);
}
$this->_addContent(
$this->_view->getLayout()->createBlock('YourCompany_YourModule::grid_region')
->setTemplate('YourCompany_YourModule::region/grid.phtml')
);
return $this->_view->renderLayout();
}
}
Step 2: Add the Import Button
In your module’s etc/adminhtml/menu.xml file, add a new item for the import button:
<menu>
<add id="YourCompany_YourModule::import_region"
title="Import Regions"
url="*/*/import"
sortOrder="10"
parent="Magento_Backend::content_management"
/>
</menu>
Step 3: Create the Import Form
Create a new file app/code/YourCompany/YourModule/view/adminhtml/templates/region/grid.phtml and add the import form:
<form id="regions-import" action="<?= $block->getImportUrl() ?>" method="post">
<input type="file" name="csv_file"/>
<button type="submit">Import</button>
</form>
Step 4: Handle the Imported CSV
In your execute() function, use the Magento\MediaStorage\Model\File\Uploader to handle the uploaded file:
$uploader = $this->_objectManager->create('Magento\MediaStorage\Model\File\Uploader');
$result = $uploader->save($_FILES);
Parse the CSV data using a library like PHPExcel or Fgetcsv and save each entry to your region table.
Best Practices
- Always follow Magento’s coding standards.
- Use object managers sparingly; prefer injectable dependencies when possible.
- Ensure you have proper access checks for admin users in your controller functions.
- Test the import functionality thoroughly before deploying it.
Conclusion
Importing custom CSV files into a Magento database is a common requirement for managing large amounts of data. By following this guide, you should now be able to create an “import” button that allows users to upload and save multiple records from a CSV file within your custom module.
Remember to always refer to the official Magento Developer Documentation for the most accurate information and examples.
Happy coding! Let me know if you have any further questions or need additional clarification on this process.