From 98e48a892c875ec5e4a6195ec1b7b449be4bb383 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 2 Jan 2020 12:09:27 +0100 Subject: [PATCH] Bug 24294: Add default value support for control fields in ACQ framework When trying to add an order using the ACQ framework with a 008@ tag, Koha explodes: Control fields (generally, just tags below 010) do not have subfields, use data() at /home/vagrant/kohaclone/C4/Acquisition.pm line 3272. Test plan: Set a default value for a control field in the ACQ framework Turn on UseACQFrameworkForBiblioRecords Create a new order from a new record The default value should be displayed Save => No crash Signed-off-by: Katrin Fischer Signed-off-by: Marcel de Rooy Signed-off-by: Martin Renvoize Signed-off-by: Joy Nelson (cherry picked from commit dec127df2ab986ed555f7a2cadc94491abecfbfe) Signed-off-by: Lucas Gass --- C4/Acquisition.pm | 24 ++++++++++++++----- .../Acquisition/FillWithDefaultValues.t | 9 +++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/C4/Acquisition.pm b/C4/Acquisition.pm index 4158e93cb4..1b5ec2f7a0 100644 --- a/C4/Acquisition.pm +++ b/C4/Acquisition.pm @@ -3260,18 +3260,30 @@ sub FillWithDefaultValues { my @fields = $record->field($tag); if (@fields) { for my $field (@fields) { - unless ( defined $field->subfield($subfield) ) { + if ( $field->is_control_field ) { + $field->update($defaultvalue) if not defined $field->data; + } + elsif ( not defined $field->subfield($subfield) ) { $field->add_subfields( $subfield => $defaultvalue ); } } } else { - $record->insert_fields_ordered( - MARC::Field->new( - $tag, '', '', $subfield => $defaultvalue - ) - ); + if ( $tag < 10 ) { # is_control_field + $record->insert_fields_ordered( + MARC::Field->new( + $tag, $defaultvalue + ) + ); + } + else { + $record->insert_fields_ordered( + MARC::Field->new( + $tag, '', '', $subfield => $defaultvalue + ) + ); + } } } } diff --git a/t/db_dependent/Acquisition/FillWithDefaultValues.t b/t/db_dependent/Acquisition/FillWithDefaultValues.t index f70ae1e0b6..8a09fdaf66 100755 --- a/t/db_dependent/Acquisition/FillWithDefaultValues.t +++ b/t/db_dependent/Acquisition/FillWithDefaultValues.t @@ -19,6 +19,11 @@ $biblio_module->mock( 'GetMarcStructure', sub { { + # default for a control field + '008' => { + x => { defaultvalue => $default_x }, + }, + # default value for an existing field '245' => { c => { defaultvalue => $default_author }, @@ -39,6 +44,10 @@ $biblio_module->mock( my $record = MARC::Record->new; $record->leader('03174nam a2200445 a 4500'); my @fields = ( + MARC::Field->new( + '008', '1', ' ', + '@' => '120829t20132012nyu bk 001 0ceng', + ), MARC::Field->new( 100, '1', ' ', a => 'Knuth, Donald Ervin', -- 2.39.5