From f40865cb4d108adeaf77e56b4ac006251c27b180 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Tue, 4 Sep 2012 15:26:51 +0200 Subject: [PATCH] =?utf8?q?GetModels-Controller-Helper:=20Weitere=20URL-Par?= =?utf8?q?ameter=20f=C3=BCr=20Callbacks=20=C3=BCbergeben=20k=C3=B6nnen?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- SL/Controller/Helper/GetModels.pm | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/SL/Controller/Helper/GetModels.pm b/SL/Controller/Helper/GetModels.pm index 00ef31017..e35365717 100644 --- a/SL/Controller/Helper/GetModels.pm +++ b/SL/Controller/Helper/GetModels.pm @@ -3,7 +3,7 @@ package SL::Controller::Helper::GetModels; use strict; use Exporter qw(import); -our @EXPORT = qw(get_callback get_models); +our @EXPORT = qw(get_models_url_params get_callback get_models); use constant PRIV => '__getmodelshelperpriv'; @@ -21,6 +21,22 @@ sub register_get_models_handlers { map { push @{ $registered_handlers{$_} }, $additional_handlers{$_} if $additional_handlers{$_} } keys %registered_handlers; } +sub get_models_url_params { + my ($class, $sub_name_or_code) = @_; + + my $code = (ref($sub_name_or_code) || '') eq 'CODE' ? $sub_name_or_code : sub { shift->$sub_name_or_code(@_) }; + my $callback = sub { + my ($self, %params) = @_; + my @additional_params = $code->($self); + return ( + %params, + (scalar(@additional_params) == 1) && (ref($additional_params[0]) eq 'HASH') ? %{ $additional_params[0] } : @additional_params, + ); + }; + + push @{ $registered_handlers{callback} }, $callback; +} + sub get_callback { my ($self, %override_params) = @_; @@ -102,6 +118,19 @@ in L. =over 4 +=item C + +Register one of the controller's subs to be called whenever an URL has +to be generated (e.g. for sort and pagination links). This is a way +for the controller to add additional parameters to the URL (e.g. for +filter parameters). + +The C<$sub> parameter can be either a code reference or the name of +one of the controller's functions. + +The value returned by this C<$sub> must be either a single hash +reference or a hash of key/value pairs to add to the URL. + =item C This function should only be called from other controller helpers like -- 2.20.1