From c0a0f64c67a88b4f6f26cca28dc4585755cfed4f Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Fri, 15 Sep 2023 16:09:56 +0100 Subject: [PATCH] Bug 31503: (follow-up) Add 'feature_enabled' to Koha::Plugins This patch adds a 'feature_enabled' accessor to the Koha::Plugins class. Passing the plugin method name for the plugin feature you're looking for will return a boolean denoting whether any enabled plugins carry said method. Incuded in the patch is an addition to the TT KohaPlugins plugin to enable quick access to this function. --- Koha/Plugins.pm | 24 ++++++++++++++++++++++++ Koha/Template/Plugin/KohaPlugins.pm | 17 +++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/Koha/Plugins.pm b/Koha/Plugins.pm index a3c47006b5..520f2febf2 100644 --- a/Koha/Plugins.pm +++ b/Koha/Plugins.pm @@ -139,6 +139,30 @@ sub get_enabled_plugins { return @$enabled_plugins; } + +=head2 feature_enabled + +Returns a boolean denoting whether a plugin based feature is enabled or not. + + $enabled = Koha::Plugins->feature_enabled('method_name'); + +=cut + +sub feature_enabled { + my ( $class, $method ) = @_; + + return 0 unless C4::Context->config('enable_plugins'); + + my $key = "ENABLED_PLUGIN_FEATURE_" . $method; + my $feature = Koha::Cache::Memory::Lite->get_from_cache($key); + unless ( defined($feature) ) { + my @plugins = $class->get_enabled_plugins(); + my $enabled = any { $_->can($method) } @plugins; + Koha::Cache::Memory::Lite->set_in_cache( $key, $enabled ); + } + return $feature; +} + =head2 GetPlugins This will return a list of all available plugins, optionally limited by diff --git a/Koha/Template/Plugin/KohaPlugins.pm b/Koha/Template/Plugin/KohaPlugins.pm index 7d527b34e3..35ac34bcf3 100644 --- a/Koha/Template/Plugin/KohaPlugins.pm +++ b/Koha/Template/Plugin/KohaPlugins.pm @@ -282,4 +282,21 @@ sub get_plugins_opac_cover_images { return join( "\n", @data ); } +=head3 feature_enabled + + [% KohaPlugins.feature_enabled('method_name') %] + +This method returns true if the passed plugin hook method name is found to be installed and enabled as part of a plugin. + +=cut + +sub feature_enabled { + my ($self, $method) = @_; + + my $p = Koha::Plugins->new(); + return 0 unless $p; + + return $p->feature_enabled($method); +} + 1; -- 2.41.0