Issue #3086611: Allow CSV imports to reference assets by name

This commit is contained in:
Michael Stenta 2019-10-08 16:15:18 -04:00
parent 700786444a
commit 148eda32b0
3 changed files with 45 additions and 3 deletions

View File

@ -214,6 +214,9 @@ function farm_import_add_importer_fields($type, $bundle, $importer) {
// Add fields, if they exist on the bundle.
foreach ($mappings as $field => $mapping) {
if (!empty($mapping['real_field'])) {
$field = $mapping['real_field'];
}
if (!empty(field_info_instance($type, $field, $bundle))) {
$importer->config['processor']['config']['mappings'][] = $mapping;
}
@ -271,6 +274,13 @@ function farm_import_asset_field_mappings() {
'unique' => FALSE,
'language' => 'und',
),
'field_farm_parent_name' => array(
'source' => 'Parent names',
'target' => 'field_farm_parent:label',
'unique' => FALSE,
'language' => 'und',
'real_field' => 'field_farm_parent',
),
);
}
@ -289,6 +299,13 @@ function farm_import_log_field_mappings() {
'unique' => FALSE,
'language' => 'und',
),
'field_farm_asset_name' => array(
'source' => 'Asset names',
'target' => 'field_farm_asset:label',
'unique' => FALSE,
'language' => 'und',
'real_field' => 'field_farm_asset',
),
'field_farm_area' => array(
'source' => 'Areas',
'target' => 'field_farm_area',

View File

@ -24,10 +24,13 @@ function farm_import_feeds_tamper_default() {
$feeds_tamper = farm_import_feeds_tamper_plugin('farm_asset', $bundle, 'Archived', 'boolean_default_false');
$export[$feeds_tamper->id] = $feeds_tamper;
// If a "Parent IDs" field exists, explode it into an array of IDs.
// If a parent asset field exists, explode "Parent IDs" and "Parent names"
// into arrays of IDs and names.
if (!empty(field_info_instance('farm_asset', 'field_farm_parent', $bundle))) {
$feeds_tamper = farm_import_feeds_tamper_plugin('farm_asset', $bundle, 'Parent IDs', 'explode');
$export[$feeds_tamper->id] = $feeds_tamper;
$feeds_tamper = farm_import_feeds_tamper_plugin('farm_asset', $bundle, 'Parent names', 'explode');
$export[$feeds_tamper->id] = $feeds_tamper;
}
}
@ -50,10 +53,13 @@ function farm_import_feeds_tamper_default() {
$feeds_tamper = farm_import_feeds_tamper_plugin('log', $bundle, 'Done', 'boolean_default_true');
$export[$feeds_tamper->id] = $feeds_tamper;
// If an "Asset IDs" field exists, explode it into an array of IDs.
// If an asset reference field exists, explode the "Asset IDs" and "Asset
// names" targets into arrays of IDs and names.
if (!empty(field_info_instance('log', 'field_farm_asset', $bundle))) {
$feeds_tamper = farm_import_feeds_tamper_plugin('log', $bundle, 'Asset IDs', 'explode');
$export[$feeds_tamper->id] = $feeds_tamper;
$feeds_tamper = farm_import_feeds_tamper_plugin('log', $bundle, 'Asset names', 'explode');
$export[$feeds_tamper->id] = $feeds_tamper;
}
// If an "Areas" field exists, explode it into an array of area names.

View File

@ -200,7 +200,7 @@ function farm_livestock_feeds_importer_default_alter($importers) {
$name = 'log_farm_birth';
if (!empty($importers[$name])) {
// Add Mother ID mapping.
// Add Mother ID and name mappings.
$mappings = array(
array(
'source' => 'Mother ID',
@ -208,6 +208,12 @@ function farm_livestock_feeds_importer_default_alter($importers) {
'unique' => FALSE,
'language' => 'und',
),
array(
'source' => 'Mother name',
'target' => 'field_farm_mother:label',
'unique' => FALSE,
'language' => 'und',
),
);
$importer_mappings =& $importers[$name]->config['processor']['config']['mappings'];
$importer_mappings = array_merge($importer_mappings, $mappings);
@ -218,6 +224,13 @@ function farm_livestock_feeds_importer_default_alter($importers) {
$mapping['source'] = 'Children IDs';
}
}
// Change "Asset names" to "Children names".
foreach ($importer_mappings as &$mapping) {
if ($mapping['source'] == 'Asset names') {
$mapping['source'] = 'Children names';
}
}
}
}
@ -258,6 +271,12 @@ function farm_livestock_feeds_tamper_default_alter(&$feeds_tampers) {
// Explode nicknames to allow multiple values.
$feeds_tamper = farm_import_feeds_tamper_plugin('farm_asset', 'animal', 'Nicknames', 'explode');
$feeds_tampers[$feeds_tamper->id] = $feeds_tamper;
// Explode birth children IDs and names to allow multiple values.
$feeds_tamper = farm_import_feeds_tamper_plugin('log', 'farm_birth', 'Children IDs', 'explode');
$feeds_tampers[$feeds_tamper->id] = $feeds_tamper;
$feeds_tamper = farm_import_feeds_tamper_plugin('log', 'farm_birth', 'Children names', 'explode');
$feeds_tampers[$feeds_tamper->id] = $feeds_tamper;
}
/**