Plugin that allows you to import data from CSV files.
With the release of Craft 3 on 4-4-2018, this plugin has been deprecated. You can still use this with Craft 2 but you are encouraged to use (and develop) a Craft 3 version. At this moment, I have no plans to do so.
Importing data is a two step process:
Step 1. Select import options
Step 2. Map CSV data
And import!
The plugin's folder should be named "import"
History
Upload (entries)
Upload (users)
Map fields
Manipulate the data as the import is running. You'll be able to tweak your data just before it goes into the database.
This code example is a good starting point:
/**
* Modify data just before importing
*
* @param BaseElementModel $element The current element receiving import data.
* @param array $map Mapping of data between CSV -> Craft fields.
* @param array $data Raw data provided for this row.
*/
public function modifyImportRow($element, $map, $data)
{
// Map data to fields
$fields = array_combine($map, $data);
// Initialize content array
$content = array();
// Arrange your content in a way that makes sense for your plugin
foreach ($fields as $handle => $value) {
$content[$handle] = $value;
}
// Set modified content
$element->setContentFromPost($content);
}
The foreach
loop is probably where you'll do the most customizing. However, feel free to edit any (or all) of this method to achieve your desired results.
Prep the field mapping before the import is run. This allows you to customize the <select>
menu option(s) for your field.
When you are mapping your CSV columns to Craft fields, Import let's you choose which field you want to assign your data to. There is a <select>
menu with a list of Craft fields, and you select the destination for each column.
The purpose of this hook is to give you the opportunity to replace the standard <option>
tag with something custom. In essence, you are providing a template which contains a custom <option>
(or multiple options) to handle the data mapping in a way that is more preferable for your field type.
Put this in your main MyPlugin.php file, and edit accordingly:
/**
* For custom field types, replace default <option> with custom HTML
*
* @return array Mapping between custom field type -> custom <option> template
*/
public function registerImportOptionPaths()
{
return array(
'MyPlugin_MyFieldType' => 'myplugin/customImportOption.html',
);
}
You are being given the opportunity to say "for fields of this type, use my custom template in place of the default <option>
tag". Make sure your customImportOption.html
template outputs one or more <option>
tags... They will be injected into the field select dropdown menu.
In summary:
<option>
If you want to dig in the code a little more and see how it works in even greater detail, check out this chunk of code here...