diff --git a/Koha/Plugins.pm b/Koha/Plugins.pm index a188d7c815..da336adec6 100644 --- a/Koha/Plugins.pm +++ b/Koha/Plugins.pm @@ -46,18 +46,26 @@ sub new { return bless( $args, $class ); } -=head2 GetPlugins() +=head2 GetPlugins -This will return a list of all the available plugins of the passed type. +This will return a list of all available plugins, optionally limited by +method or metadata value. -Usage: my @plugins = C4::Plugins::GetPlugins( $method ); + my @plugins = C4::Plugins::GetPlugins({ + method => 'some_method', + metadata => { some_key => 'some_value' }, + }); + +The method and metadata parameters are optional. +Available methods currently are: 'report', 'tool', 'to_marc', 'edifact'. +If you pass multiple keys in the metadata hash, all keys must match. -At the moment, the available types are 'report', 'tool' and 'to_marc'. =cut sub GetPlugins { - my $self = shift; - my $method = shift; + my ( $self, $params ) = @_; + my $method = $params->{method}; + my $req_metadata = $params->{metadata} // {}; my @plugin_classes = $self->plugins(); my @plugins; @@ -68,13 +76,18 @@ sub GetPlugins { my $plugin = $plugin_class->new({ enable_plugins => $self->{'enable_plugins'} }); - if ($method) { - if ( $plugin->can($method) ) { - push( @plugins, $plugin ); + # Limit results by method or metadata + my $ok = 1; + next if $method && !$plugin->can($method); + my $plugin_metadata = $plugin->get_metadata; + foreach my $key ( keys %$req_metadata ) { + if( !$plugin_metadata->{$key} || + $plugin_metadata->{$key} ne $req_metadata->{$key} ) { + $ok = 0; + last; } - } else { - push( @plugins, $plugin ); } + push( @plugins, $plugin ) if $ok; } } return @plugins; diff --git a/admin/edi_accounts.pl b/admin/edi_accounts.pl index 5f963bd4c0..e5b069463e 100755 --- a/admin/edi_accounts.pl +++ b/admin/edi_accounts.pl @@ -57,7 +57,9 @@ if ( $op eq 'acct_form' ) { $template->param( plugins_enabled => $plugins_enabled ); if ( $plugins_enabled ) { - my @plugins = Koha::Plugins->new()->GetPlugins('edifact'); + my @plugins = Koha::Plugins->new()->GetPlugins({ + method => 'edifact', + }); $template->param( plugins => \@plugins ); } } diff --git a/plugins/plugins-home.pl b/plugins/plugins-home.pl index 225a560031..5efd9bd94c 100755 --- a/plugins/plugins-home.pl +++ b/plugins/plugins-home.pl @@ -50,7 +50,9 @@ if ($plugins_enabled) { method => $method, ); - my @plugins = Koha::Plugins->new()->GetPlugins($method); + my @plugins = Koha::Plugins->new()->GetPlugins({ + method => $method, + }); $template->param( plugins => \@plugins, ); diff --git a/t/Koha/Plugin/Test.pm b/t/Koha/Plugin/Test.pm index 6a8ca7862f..401ce3df23 100644 --- a/t/Koha/Plugin/Test.pm +++ b/t/Koha/Plugin/Test.pm @@ -1,6 +1,6 @@ package Koha::Plugin::Test; -## It's good practive to use Modern::Perl +## It's good practice to use Modern::Perl use Modern::Perl; ## Required for all plugins @@ -16,6 +16,7 @@ our $metadata = { minimum_version => '3.11', maximum_version => undef, version => $VERSION, + my_example_tag => 'find_me', }; ## This is the minimum code required for a plugin's 'new' method diff --git a/t/db_dependent/Plugins.t b/t/db_dependent/Plugins.t index 403d7c4328..f918693c47 100755 --- a/t/db_dependent/Plugins.t +++ b/t/db_dependent/Plugins.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 22; +use Test::More tests => 23; use File::Basename; use FindBin qw($Bin); use Archive::Extract; @@ -42,9 +42,17 @@ is( $metadata->{'name'}, 'Test Plugin', 'Test $plugin->get_metadata()' ); is( $plugin->get_qualified_table_name('mytable'), 'koha_plugin_test_mytable', 'Test $plugin->get_qualified_table_name()' ); is( $plugin->get_plugin_http_path(), '/plugin/Koha/Plugin/Test', 'Test $plugin->get_plugin_http_path()' ); -my @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins( 'report' ); +# testing GetPlugins +my @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({ + method => 'report' +}); my @names = map { $_->get_metadata()->{'name'} } @plugins; is( scalar grep( /^Test Plugin$/, @names), 1, "Koha::Plugins::GetPlugins functions correctly" ); +@plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({ + metadata => { my_example_tag => 'find_me' }, +}); +@names = map { $_->get_metadata()->{'name'} } @plugins; +is( scalar grep( /^Test Plugin$/, @names), 1, "GetPlugins also found Test Plugin via a metadata tag" ); SKIP: { my $plugins_dir = C4::Context->config("pluginsdir"); diff --git a/tools/stage-marc-import.pl b/tools/stage-marc-import.pl index a5f60bd1a2..99506fc562 100755 --- a/tools/stage-marc-import.pl +++ b/tools/stage-marc-import.pl @@ -210,7 +210,9 @@ if ($completedJobID) { if ( C4::Context->preference('UseKohaPlugins') && C4::Context->config('enable_plugins') ) { - my @plugins = Koha::Plugins->new()->GetPlugins('to_marc'); + my @plugins = Koha::Plugins->new()->GetPlugins({ + method => 'to_marc', + }); $template->param( plugins => \@plugins ); } }