From 991516a2fa0d32cbb559bee1bff878a5feb59c78 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 5 Oct 2021 13:57:04 +0200 Subject: [PATCH] Bug 29172: Make CustomCoverImagesURL compatible with control fields Bug 22445 added CustomCoverImagesURL that deals with MARC fields, but control fields were not supported. Test plan: Turn CustomCoverImages on Fill CustomCoverImagesURL with https://covers.openlibrary.org/b/isbn/{006}-M.jpg Add an isbn in 006 (0596001738 will work) Go to the detail page of the bibliographic record and confirm that you see a cover image. Signed-off-by: David Nind Signed-off-by: Katrin Fischer --- Koha/Biblio.pm | 10 ++++++++-- t/db_dependent/Koha/Biblios.t | 9 ++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index f1d84b23f6..e3618dc80f 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -753,12 +753,18 @@ sub custom_cover_image_url { $url =~ s|{issn}|$issn|g; } - my $re = qr|{(?\d{3})\$(?.)}|; + my $re = qr|{(?\d{3})(\$(?.))?}|; if ( $url =~ $re ) { my $field = $+{field}; my $subfield = $+{subfield}; my $marc_record = $self->metadata->record; - my $value = $marc_record->subfield($field, $subfield); + my $value; + if ( $subfield ) { + $value = $marc_record->subfield( $field, $subfield ); + } else { + my $controlfield = $marc_record->field($field); + $value = $controlfield->data() if $controlfield; + } return unless $value; $url =~ s|$re|$value|; } diff --git a/t/db_dependent/Koha/Biblios.t b/t/db_dependent/Koha/Biblios.t index d19e8f3c0d..b93efebb21 100755 --- a/t/db_dependent/Koha/Biblios.t +++ b/t/db_dependent/Koha/Biblios.t @@ -193,12 +193,13 @@ subtest 'can_be_transferred' => sub { }; subtest 'custom_cover_image_url' => sub { - plan tests => 4; + plan tests => 6; t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/{isbn}_{issn}.png' ); my $isbn = '0553573403 | 9780553573404 (pbk.).png'; my $issn = 'my_issn'; + my $cf_value = 'from_control_field'; my $marc_record = MARC::Record->new; my ( $biblionumber, undef ) = C4::Biblio::AddBiblio($marc_record, ''); @@ -221,6 +222,12 @@ subtest 'custom_cover_image_url' => sub { $biblio->biblioitem->isbn('')->store; is( $biblio->custom_cover_image_url, undef, "Don't generate the url if the biblio does not have the value needed to generate it" ); + t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/{001}.png' ); + is( $biblio->custom_cover_image_url, undef, 'Record does not have 001' ); + $marc_record->append_fields(MARC::Field->new('001', $cf_value)); + C4::Biblio::ModBiblio( $marc_record, $biblio->biblionumber ); + $biblio = Koha::Biblios->find( $biblionumber ); + is( $biblio->get_from_storage->custom_cover_image_url, "https://my_url/$cf_value.png", 'URL generated using 001' ); }; $schema->storage->txn_rollback; -- 2.11.0