From 64acc660e110a60d42a1736ec1a060f6f0ae7eb4 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. --- 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; -- 2.39.5