From 17e3e50bca71859222e0c125a50c83ce9842c30d Mon Sep 17 00:00:00 2001 From: Boriana Ditcheva Date: Thu, 1 Aug 2013 13:01:11 -0400 Subject: [PATCH] Adding options in UI for how to handle empty text values in during import --- feeds_ui/feeds_ui.admin.inc | 29 +++++++++++++++++++++++++++++ mappers/text.inc | 8 ++++++-- tests/feeds/content.csv | 7 ++++--- tests/feeds/content_changed.csv | 3 +++ tests/feeds_mapper_field.test | 30 ++++++++++++++++++++++++++++-- 5 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 tests/feeds/content_changed.csv diff --git a/feeds_ui/feeds_ui.admin.inc b/feeds_ui/feeds_ui.admin.inc index beb1930..7e7d202 100644 --- a/feeds_ui/feeds_ui.admin.inc +++ b/feeds_ui/feeds_ui.admin.inc @@ -847,6 +847,18 @@ function feeds_ui_mapping_settings_optional_unique_summary($mapping, $target, $f } } + /** + * Per mapping settings summary callback. Shows if a mapping can be saved as null. + */ +function feeds_ui_mapping_settings_allow_empty_summary($mapping, $target, $form, $form_state) { + if (empty($mapping['allow_empty'])) { + return t('Empty new value leaves current value untouched.'); + } + else { + return t('Empty new value clears current value.'); + } +} + /** * Per mapping settings form callback. Lets the user choose if a target is as * unique or not. @@ -865,6 +877,23 @@ function feeds_ui_mapping_settings_optional_unique_form($mapping, $target, $form return $settings_form; } + /** + * Per mapping settings form callback. Lets the user choose if a target's null + * values are ignored or not. + */ +function feeds_ui_mapping_settings_allow_empty_form($mapping, $target, $form, $form_state) { + $empty_value_options = array(0 => t('Empty new value leaves current value untouched.'), 1 => t('Empty new value clears current value.')); + $settings_form = array(); + $settings_form['allow_empty'] = array( + '#type' => 'radios', + '#title' => t('How to treat new empty values'), + '#default_value' => isset($mapping['allow_empty']) ? $mapping['allow_empty'] : 0, + '#options' => $empty_value_options, + ); + + return $settings_form; +} + /** * Theme feeds_ui_overview_form(). */ diff --git a/mappers/text.inc b/mappers/text.inc index 48447d7..3f36ecc 100644 --- a/mappers/text.inc +++ b/mappers/text.inc @@ -20,11 +20,15 @@ function text_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_nam foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) { $info = field_info_field($name); + $allow_empty = isset($targets[$name]['allow_empty']) ? $targets[$name]['allow_empty'] : 0; if (in_array($info['type'], $text_types)) { $targets[$name] = array( 'name' => check_plain($instance['label']), 'callback' => 'text_feeds_set_target', 'description' => t('The @label field of the entity.', array('@label' => $instance['label'])), + 'allow_empty' => $allow_empty, + 'form_callback' => 'feeds_ui_mapping_settings_allow_empty_form', + 'summary_callback' => 'feeds_ui_mapping_settings_allow_empty_summary', ); } } @@ -33,8 +37,8 @@ function text_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_nam /** * Callback for mapping text fields. */ -function text_feeds_set_target($source, $entity, $target, $value) { - if (empty($value)) { +function text_feeds_set_target($source, $entity, $target, $value, $mapping) { + if (empty($value) && empty($mapping['allow_empty'])) { return; } diff --git a/tests/feeds/content.csv b/tests/feeds/content.csv index 1e68bdf..bd53784 100644 --- a/tests/feeds/content.csv +++ b/tests/feeds/content.csv @@ -1,3 +1,4 @@ -"guid","title","created","alpha","beta","gamma","delta","body" -1,"Lorem ipsum",1251936720,"Lorem",42,"4.2",3.14159265,"Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat." -2,"Ut wisi enim ad minim veniam",1251932360,"Ut wisi",32,"1.2",5.62951413,"Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat." +"guid","title","created","alpha","beta","gamma","delta","body","epsilon" +1,"Lorem ipsum",1251936720,"Lorem",42,"4.2",3.14159265,"Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.","" +2,"Ut wisi enim ad minim veniam",1251932360,"Ut wisi",32,"1.2",5.62951413,"Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.","" +3,"Lorem",1251936720,"Ipsum",42,"4.2",3.14159265,"Ut wisien","dolor" diff --git a/tests/feeds/content_changed.csv b/tests/feeds/content_changed.csv new file mode 100644 index 0000000..0bfa125 --- /dev/null +++ b/tests/feeds/content_changed.csv @@ -0,0 +1,3 @@ +"guid","title","created","alpha","beta","gamma","delta","body","epsilon" +2,"Ut wisi enim ad minim veniam",1251932360,"",32,"1.2",5.62951413,"Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.","" +3,"Lorem",1251936720,"Ipsum",42,"4.2",3.14159265,"Ut wisien","" diff --git a/tests/feeds_mapper_field.test b/tests/feeds_mapper_field.test index 2506a29..a133672 100644 --- a/tests/feeds_mapper_field.test +++ b/tests/feeds_mapper_field.test @@ -31,6 +31,7 @@ class FeedsMapperFieldTestCase extends FeedsMapperTestCase { 'beta' => 'number_integer', 'gamma' => 'number_decimal', 'delta' => 'number_float', + 'epsilon' => 'text' )); // Create and configure importer. @@ -55,6 +56,7 @@ class FeedsMapperFieldTestCase extends FeedsMapperTestCase { 3 => array( 'source' => 'alpha', 'target' => 'field_alpha', + 'allow_empty' => TRUE, ), 4 => array( 'source' => 'beta', @@ -68,13 +70,23 @@ class FeedsMapperFieldTestCase extends FeedsMapperTestCase { 'source' => 'delta', 'target' => 'field_delta', ), + 7 => array( + 'source' => 'guid', + 'target' => 'guid', + 'unique' => TRUE, + ), + 8 => array( + 'source' => 'epsilon', + 'target' => 'field_epsilon', + 'allow_empty' => FALSE, + ), )); // Import CSV file. $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content.csv'); - $this->assertText('Created 2 nodes'); + $this->assertText('Created 3 nodes.'); - // Check the two imported files. + // Check the three imported nodes. $this->drupalGet('node/1/edit'); $this->assertNodeFieldValue('alpha', 'Lorem'); $this->assertNodeFieldValue('beta', '42'); @@ -86,5 +98,19 @@ class FeedsMapperFieldTestCase extends FeedsMapperTestCase { $this->assertNodeFieldValue('beta', '32'); $this->assertNodeFieldValue('gamma', '1.20'); $this->assertNodeFieldValue('delta', '5.62951'); + + $this->drupalGet('node/3/edit'); + $this->assertNodeFieldValue('epsilon', 'dolor'); + + $this->importFile('csv', $this->absolutePath() . '/tests/feeds/content_changed.csv'); + + $this->drupalGet('node/2/edit'); + $this->assertNodeFieldValue('alpha', ''); + $this->assertNodeFieldValue('beta', '32'); + $this->assertNodeFieldValue('gamma', '1.20'); + $this->assertNodeFieldValue('delta', '5.62951'); + + $this->drupalGet('node/3/edit'); + $this->assertNodeFieldValue('epsilon', 'dolor'); } } -- 1.7.7.msysgit.1