From f48cb7a0f06c3be2527fdfb7d14a00f267fc2c63 Mon Sep 17 00:00:00 2001 From: Janusz Kaczmarek Date: Sat, 14 Jun 2025 21:45:32 +0000 Subject: [PATCH] Bug 40128: StripWhitespaceChars can create empty subfields When StripWhitespaceChars is enabled and the user by chance enters some white spaces in the input field, Koha generates empty subfield (or even entirely empty field). Test plan: ========== 1. Enable StripWhitespaceChars system preference. 2. Edit an existing record, putting just some spaces into an empty subfield. Save the record. 3. Go to bibliographic record Normal view and in modal "MARC preview" notice that the subfield you entered spaces in exists as an empty subfield. If it was the only subfield in a field, the field exists with no data at all. This is wrong. 4. Apply the patch ; restart_all. 5. Repeat 2. and 3. You should not see any empty subfield now. If the empty subbfield was the only subfield in a field, the field should have been removed from the record. Signed-off-by: Roman Dolny Signed-off-by: Marcel de Rooy Signed-off-by: Lucas Gass (cherry picked from commit d0ce815600c903e285700e9b615db848669d26ad) Signed-off-by: Paul Derscheid --- Koha/Filter/MARC/TrimFields.pm | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Koha/Filter/MARC/TrimFields.pm b/Koha/Filter/MARC/TrimFields.pm index 99a5f0dfa3..00244cab29 100644 --- a/Koha/Filter/MARC/TrimFields.pm +++ b/Koha/Filter/MARC/TrimFields.pm @@ -60,9 +60,15 @@ sub filter { my $value = $subfield->[1]; $value =~ s/[\n\r]+/ /g; $value =~ s/^\s+|\s+$//g; - $field->add_subfields( $key => $value ); # add subfield to the end of the subfield list - $field->delete_subfield( pos => 0 ); # delete the subfield at the top of the subfield list + $field->add_subfields( $key => $value ) + if $value ne q{} + ; # add subfield to the end of the subfield list, but only if there is still a non empty value there + $field->delete_subfield( pos => 0 ); # delete the subfield at the top of the subfield list } + + # if it happed that all existing subfields had whitespaces only, + # the field would be empty now and should be removed from the record + $record->delete_fields($field) unless scalar( $field->subfields ); } } return $record;