function migrate_ui_edit_mappings_submit in Migrate 7.2
Submit callback for the edit mappings form. Save any choices made in the UI which override mappings made in code.
_state
Parameters
$form:
File
- migrate_ui/
migrate_ui.pages.inc, line 1329 - Pages for managing migration processes.
Code
function migrate_ui_edit_mappings_submit(&$form, &$form_state) {
$machine_name = $form_state['values']['machine_name'];
$row = db_select('migrate_status', 'ms')
->fields('ms', array(
'arguments',
'class_name',
'group_name',
))
->condition('machine_name', $machine_name)
->execute()
->fetchObject();
$class_name = $row->class_name;
$group_name = $row->group_name;
$arguments = unserialize($row->arguments);
$arguments['field_mappings'] = array();
$arguments['group_name'] = $group_name;
$field_mappings = array();
$default_values = array();
$issue_group_values = array();
$xpaths = array();
$migration = Migration::getInstance($machine_name);
if (is_a($migration, 'Migration')) {
$xml = is_a($migration, 'XMLMigration') ? TRUE : FALSE;
$existing_mappings = $migration
->getFieldMappings();
$coded_mappings = $migration
->getCodedFieldMappings();
foreach ($form_state['values']['field_mappings'] as $destination_field => $info) {
// Treat empty strings as NULL.
if ($info['default_value'] === '') {
$info['default_value'] = NULL;
}
if ($xml && $info['xpath'] === '') {
$info['xpath'] = NULL;
}
// If this mapping matches a coded mapping but not a stored mapping, remove
// it entirely (don't store it in the database) so the coded mapping is not
// overwritten.
if (isset($coded_mappings[$destination_field])) {
$coded_source_field = $coded_mappings[$destination_field]
->getSourceField();
$coded_default_value = $coded_mappings[$destination_field]
->getDefaultValue();
$coded_source_migration = $coded_mappings[$destination_field]
->getSourceMigration();
if ($xml) {
$coded_xpath = $coded_mappings[$destination_field]
->getXpath();
}
if ($info['mapping'] == '-1') {
$info['mapping'] = NULL;
}
if ($info['source_migration'] == '-1') {
$info['source_migration'] = NULL;
}
if ($info['issue_group'] == 0 && $coded_mappings[$destination_field]
->getIssueGroup() != t('DNM') || $info['issue_group'] == 1 && $coded_mappings[$destination_field]
->getIssueGroup() == t('DNM')) {
$dnm_matches = TRUE;
}
else {
$dnm_matches = FALSE;
}
if ($info['mapping'] == $coded_source_field && $info['default_value'] == $coded_default_value && $info['source_migration'] == $coded_source_migration && (!$xml || $xml && $info['xpath'] == $coded_xpath) && $dnm_matches) {
continue;
}
}
// This is not a match for a coded mapping, so we will want to save it.
$field_mappings[$destination_field] = $info['mapping'];
$default_values[$destination_field] = $info['default_value'];
$source_migrations[$destination_field] = $info['source_migration'];
$issue_group_values[$destination_field] = $info['issue_group'];
if ($xml) {
$xpaths[$destination_field] = $info['xpath'];
}
}
foreach ($field_mappings as $destination_field => $source_field) {
if ($source_field == -1) {
$source_field = NULL;
}
if ($issue_group_values[$destination_field]) {
$source_field = NULL;
$default = NULL;
}
else {
$default = $default_values[$destination_field];
}
// Until we can provide editing of all the options that go along with
// field mappings, we want to avoid overwriting pre-existing mappings and
// losing important bits.
$mapping = NULL;
if (isset($existing_mappings[$destination_field]) && $issue_group_values[$destination_field] != 0) {
/** @var MigrateFieldMapping $old_mapping */
$old_mapping = $existing_mappings[$destination_field];
if ($source_field == $old_mapping
->getSourceField() && $default_values[$destination_field] == $old_mapping
->getDefaultValue() && $source_migrations[$destination_field] == $old_mapping
->getSourceMigration() && (!$xml || $xml && $xpaths[$destination_field] == $old_mapping
->getXpath())) {
// First, if this mapping matches a previously-stored mapping, we want to
// preserve it as it was originally stored.
if ($old_mapping
->getMappingSource() == MigrateFieldMapping::MAPPING_SOURCE_DB) {
$mapping = $old_mapping;
}
else {
continue;
}
}
}
// We're not skipping this mapping, or preserving an old one, so create the
// new mapping.
if (!$mapping) {
$mapping = _migrate_ui_get_mapping_object($migration, $destination_field, $source_field);
$mapping
->defaultValue($default);
if ($xml && $xpaths[$destination_field]) {
$mapping
->xpath($xpaths[$destination_field]);
}
}
if ($issue_group_values[$destination_field]) {
$mapping
->issueGroup(t('DNM'));
}
else {
$mapping
->issueGroup(NULL);
}
if ($source_migrations[$destination_field] && $source_migrations[$destination_field] != '-1') {
$mapping
->sourceMigration($source_migrations[$destination_field]);
}
$arguments['field_mappings'][] = $mapping;
}
// Handle any source fields marked DNM.
foreach ($form_state['values']['source_fields'] as $source_field => $value) {
// Is this field ignored in the code?
$code_ignored = FALSE;
foreach ($coded_mappings as $destination_field => $mapping) {
if (is_numeric($destination_field) && $mapping
->getSourceField() == $source_field) {
$code_ignored = TRUE;
}
}
// If it is marked DNM in the UI, but is not ignored in the code,
// generate a DNM mapping.
if ($value && !$code_ignored) {
$mapping = _migrate_ui_get_mapping_object($migration, NULL, $source_field);
$mapping
->issueGroup(t('DNM'));
$arguments['field_mappings'][] = $mapping;
}
elseif (!$value && $code_ignored) {
$mapping_found = FALSE;
foreach ($field_mappings as $destination_field => $mapped_source_field) {
if ($source_field == $mapped_source_field) {
$mapping_found = TRUE;
break;
}
}
if (!$mapping_found) {
$mapping = _migrate_ui_get_mapping_object($migration, NULL, $source_field);
$arguments['field_mappings'][] = $mapping;
}
}
}
}
$arguments['dependencies'] = $arguments['soft_dependencies'] = array();
if (isset($form_state['values']['dependencies'])) {
foreach ($form_state['values']['dependencies'] as $dependency => $value) {
if ($value == 1) {
$arguments['dependencies'][] = $dependency;
}
elseif ($value == 2) {
$arguments['soft_dependencies'][] = $dependency;
}
}
}
Migration::registerMigration($class_name, $machine_name, $arguments);
drupal_set_message(t('Migration changes applied.'));
$form_state['redirect'] = "admin/content/migrate/groups/{$group_name}/{$machine_name}";
}