From 3d543f43526dcc866d6800633c6d75e76e1ea596 Mon Sep 17 00:00:00 2001 From: Boriana Ditcheva Date: Thu, 1 Aug 2013 11:10:33 -0400 Subject: [PATCH] Adding options for how to handle empty text fields in an import --- feeds_ui/feeds_ui.admin.inc | 30 ++++++++++++++++++++++++++++++ mappers/text.inc | 8 ++++++-- tests/feeds/content.csv | 7 ++++--- tests/feeds/content_changed.csv | 3 +++ 4 files changed, 43 insertions(+), 5 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..ed1995b 100644 --- a/feeds_ui/feeds_ui.admin.inc +++ b/feeds_ui/feeds_ui.admin.inc @@ -847,6 +847,19 @@ 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 +878,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","" -- 1.7.7.msysgit.1