Bug 24294: Add default value support for control fields in ACQ framework
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Thu, 2 Jan 2020 11:09:27 +0000 (12:09 +0100)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Wed, 26 Feb 2020 20:37:00 +0000 (20:37 +0000)
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 <katrin.fischer.83@web.de>
Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
C4/Acquisition.pm
t/db_dependent/Acquisition/FillWithDefaultValues.t

index 4a84ddc..e7f489f 100644 (file)
@@ -3282,18 +3282,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
+                                )
+                            );
+                        }
                     }
                 }
             }
index f70ae1e..8a09fda 100755 (executable)
@@ -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',