From 630c26dcaac4167e7544f82534233543293f8e5e Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Tue, 22 Oct 2019 14:56:40 +0100 Subject: [PATCH] Bug 22445: Allow marc fields as pattern %tag$field% There is a limitation here, only one replacement will work. Let see if it is needed later to support several patterns Sponsored-by: Orex Digital Signed-off-by: Hayley Mapley Signed-off-by: Hugo Agud Signed-off-by: Owen Leonard Signed-off-by: Michal Denar Signed-off-by: Kyle Hall Signed-off-by: Katrin Fischer Signed-off-by: Martin Renvoize --- Koha/Biblio.pm | 10 ++++++++++ t/db_dependent/Koha/Biblios.t | 25 +++++++++++++++++-------- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index 427005d523..625352f135 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -679,6 +679,16 @@ sub custom_cover_image_url { my $issn = $self->biblioitem->issn; $url =~ s|%issn%|$issn|g; } + + 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); + $url =~ s|$re|$value|; + } + return $url; } diff --git a/t/db_dependent/Koha/Biblios.t b/t/db_dependent/Koha/Biblios.t index 09765228fd..2e7ce189e2 100644 --- a/t/db_dependent/Koha/Biblios.t +++ b/t/db_dependent/Koha/Biblios.t @@ -21,6 +21,7 @@ use Modern::Perl; use Test::More tests => 6; use Test::Exception; +use MARC::Field; use C4::Items; use C4::Biblio; @@ -187,18 +188,26 @@ subtest 'can_be_transferred' => sub { }; subtest 'custom_cover_image_url' => sub { - plan tests => 1; + plan tests => 2; + t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/%isbn%_%issn%.png' ); + my $isbn = 'my_isbn'; my $issn = 'my_issn'; - my $biblioitem = $builder->build_object( - { - class => 'Koha::Biblioitems', - value => { isbn => $isbn, issn => $issn } - } - ); - my $biblio = Koha::Biblios->find( $biblioitem->biblionumber ); + my $marc_record = MARC::Record->new; + my ( $biblionumber, undef ) = C4::Biblio::AddBiblio($marc_record, ''); + + my $biblio = Koha::Biblios->find( $biblionumber ); + my $biblioitem = $biblio->biblioitem->set( + { isbn => $isbn, issn => $issn }); is( $biblio->custom_cover_image_url, "https://my_url/${isbn}_${issn}.png" ); + + my $marc_024a = '710347104926'; + $marc_record->append_fields( MARC::Field->new( '024', '', '', a => $marc_024a ) ); + C4::Biblio::ModBiblio( $marc_record, $biblio->biblionumber ); + + t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/%024$a%.png' ); + is( $biblio->custom_cover_image_url, "https://my_url/$marc_024a.png" ); }; $schema->storage->txn_rollback; -- 2.20.1