Bug 40095: Add context to intranet_js plugin hook

The get_plugins_intranet_js method now passes the current script name
(controller) to plugins via a 'page' parameter, allowing plugins to
determine which page/controller is being displayed.

This will allow plugin authors to only return JS that's relevant to the
current page when they wish to do so.

Test plan:
1. Create a plugin with an intranet_js method that logs the received parameters
2. Navigate to different pages in the staff interface
3. Verify that the plugin receives the page parameter containing the script name
4. Confirm that the script name reflects the actual controller (e.g.,
   '/cgi-bin/koha/admin/admin-home.pl' when on the admin home page)
5. Run the test: prove t/db_dependent/Koha/Template/Plugin/KohaPlugins.t
6. Verify that the new test passes, confirming plugins receive the page parameter

Signed-off-by: Roman Dolny <roman.dolny@jezuici.pl>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
This commit is contained in:
Martin Renvoize 2025-06-09 15:03:54 +01:00 committed by Lucas Gass
parent c2b7f2ddb2
commit aee1c8f7a5
No known key found for this signature in database
GPG Key ID: 60B447398B0D36CA
2 changed files with 36 additions and 2 deletions

View File

@ -24,6 +24,7 @@ use base qw( Template::Plugin );
use Try::Tiny qw( catch try );
use Koha::Plugins;
use Koha::AuthUtils qw( get_script_name );
=head1 NAME
@ -151,9 +152,13 @@ sub get_plugins_intranet_head {
This method collects the output of all plugins with an intranet_js method
to output to the javascript section of at the bottom of intranet pages.
The method now passes the current script name (controller) to the plugin's intranet_js
method as a parameter: { page => 'script_name' }
=cut
sub get_plugins_intranet_js {
my ($self) = @_;
return q{} unless C4::Context->config("enable_plugins");
my $p = Koha::Plugins->new();
@ -166,10 +171,13 @@ sub get_plugins_intranet_js {
}
);
# Get script name (controller) instead of template name
my $script_name = get_script_name() || '';
my @data = ();
foreach my $plugin (@plugins) {
try {
my $datum = $plugin->intranet_js || q{};
my $datum = $plugin->intranet_js( { page => $script_name } ) || q{};
push( @data, $datum );
} catch {
warn "Error calling 'intranet_js' on the " . $plugin->{class} . "plugin ($_)";

View File

@ -3,7 +3,7 @@
use Modern::Perl;
use Test::NoWarnings;
use Test::More tests => 19;
use Test::More tests => 20;
use Test::Warn;
use CGI;
use File::Basename;
@ -92,4 +92,30 @@ is( $plugin->get_plugins_opac_head, q{}, 'Test plugin opac_head return value
is( $plugin->get_plugins_intranet_js, q{}, 'Test plugin intranet_js return value is empty' );
is( $plugin->get_plugins_intranet_head, q{}, 'Test plugin intranet_head return value is empty' );
# Test intranet_js page parameter passing
t::lib::Mocks::mock_config( 'enable_plugins', 1 );
# Reset the mock to capture arguments
my $captured_args;
$mock_plugin->mock(
'intranet_js',
sub {
my ( $self, $args ) = @_;
$captured_args = $args;
return "test";
}
);
# Create plugin with context (minimal)
my $plugin_with_context = bless {}, 'Koha::Template::Plugin::KohaPlugins';
# Call get_plugins_intranet_js
my $result = $plugin_with_context->get_plugins_intranet_js();
# Verify that the plugin was called with page parameter containing script name
ok(
defined $captured_args && ref($captured_args) eq 'HASH' && exists $captured_args->{page},
'Plugin intranet_js method receives page parameter with script name'
);
$schema->storage->txn_rollback;