From 3cb2f83782440dcafeef70d63367cffd6049adb0 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 --- C4/Acquisition.pm | 24 ++++++++++++++++------ t/db_dependent/Acquisition/FillWithDefaultValues.t | 9 ++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/C4/Acquisition.pm b/C4/Acquisition.pm index fcd590df79..fb644edc1d 100644 --- a/C4/Acquisition.pm +++ b/C4/Acquisition.pm @@ -3269,18 +3269,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 }, @@ -40,6 +45,10 @@ 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', d => '1938', -- 2.11.0