View | Details | Raw Unified | Return to bug 40984
Collapse All | Expand All

(-)a/C4/AuthoritiesMarc.pm (-3 / +58 lines)
Lines 39-44 use Koha::Authority; Link Here
39
use Koha::Database;
39
use Koha::Database;
40
use Koha::Libraries;
40
use Koha::Libraries;
41
use Koha::RecordProcessor;
41
use Koha::RecordProcessor;
42
use Koha::Logger;
43
use Koha::Plugins;
42
use Koha::SearchEngine;
44
use Koha::SearchEngine;
43
use Koha::SearchEngine::Indexer;
45
use Koha::SearchEngine::Indexer;
44
use Koha::SearchEngine::Search;
46
use Koha::SearchEngine::Search;
Lines 673-678 sub AddAuthority { Link Here
673
        $p->process($record);
675
        $p->process($record);
674
    }
676
    }
675
677
678
    # Call before hook
679
    _before_authority_action_hooks( { action => 'save', authority_id => $authid, record => $record } );
680
676
    # Save record into auth_header, update 001
681
    # Save record into auth_header, update 001
677
    my $action;
682
    my $action;
678
    my $authority;
683
    my $authority;
Lines 1885-1890 sub compare_fields { Link Here
1885
    return 1;
1890
    return 1;
1886
}
1891
}
1887
1892
1893
=head2 _before_authority_action_hooks
1894
1895
Helper method that takes care of calling all plugin hooks before an authority action
1896
1897
=cut
1898
1899
sub _before_authority_action_hooks {
1900
    my ($args) = @_;
1901
1902
    my $authority_id = $args->{authority_id};
1903
    my $record       = $args->{record};
1904
    my $action       = $args->{action} // q{};
1905
1906
    Koha::Plugins->call(
1907
        'before_authority_action',
1908
        { action => $action, payload => { authority_id => $authority_id, record => $record } }
1909
    );
1910
}
1911
1888
=head2 _after_authority_action_hooks
1912
=head2 _after_authority_action_hooks
1889
1913
1890
Helper method that takes care of calling all plugin hooks
1914
Helper method that takes care of calling all plugin hooks
Lines 1892-1902 Helper method that takes care of calling all plugin hooks Link Here
1892
=cut
1916
=cut
1893
1917
1894
sub _after_authority_action_hooks {
1918
sub _after_authority_action_hooks {
1895
    my ($args) = @_;    # hash keys: action, authority_id
1919
    my ($args) = @_;
1896
    return Koha::Plugins->call( 'after_authority_action', $args );
1920
1921
    my $authority_id = $args->{authority_id};
1922
    my $action       = $args->{action} // q{};
1923
1924
    my $authority = $action ne 'delete' ? Koha::Authorities->find($authority_id) : undef;
1925
    my $record    = $authority          ? $authority->record                     : undef;
1926
1927
    Koha::Plugins->call(
1928
        'after_authority_action',
1929
        {
1930
            action  => $action,
1931
            payload => {
1932
                authority    => $authority,
1933
                authority_id => $authority_id,
1934
                record       => $record,
1935
            },
1936
1937
            # NOTE: Deprecated parameters, will be removed in a future release
1938
            authority    => $authority,
1939
            authority_id => $authority_id,
1940
        }
1941
    );
1942
1943
    # Log deprecation warning if any plugin is still using the old signature
1944
    my @plugins = Koha::Plugins->get_enabled_plugins( { verbose => 0 } );
1945
    @plugins = grep { $_->can('after_authority_action') } @plugins;
1946
    if (@plugins) {
1947
        Koha::Logger->get->warn(
1948
            "after_authority_action hook: The old signature (with authority and authority_id as direct parameters) "
1949
                . "is deprecated and will be removed in a future release. "
1950
                . "Please update your plugin to use the 'payload' parameter instead." );
1951
    }
1897
}
1952
}
1898
1953
1899
END { }                 # module clean-up code here (global destructor)
1954
END { }    # module clean-up code here (global destructor)
1900
1955
1901
1;
1956
1;
1902
__END__
1957
__END__
(-)a/t/db_dependent/Koha/Plugins/authority_hooks.t (-1 / +22 lines)
Lines 18-24 use Modern::Perl; Link Here
18
18
19
use MARC::Record;
19
use MARC::Record;
20
use Test::NoWarnings;
20
use Test::NoWarnings;
21
use Test::More tests => 5;
21
use Test::More tests => 6;
22
use Test::Warn;
22
use Test::Warn;
23
23
24
use File::Basename;
24
use File::Basename;
Lines 44-49 my $builder = t::lib::TestBuilder->new; Link Here
44
44
45
t::lib::Mocks::mock_config( 'enable_plugins', 1 );
45
t::lib::Mocks::mock_config( 'enable_plugins', 1 );
46
46
47
subtest 'before_authority_action hook' => sub {
48
    plan tests => 1;
49
50
    $schema->storage->txn_begin;
51
52
    my $plugins = Koha::Plugins->new;
53
    $plugins->InstallPlugins;
54
    my $plugin = Koha::Plugin::Test->new->enable;
55
56
    my $record = MARC::Record->new;
57
    $record->append_fields( MARC::Field->new( '100', '1', '2', a => 'Name' ) );
58
    my $type = $builder->build( { source => 'AuthType', value => { auth_tag_to_report => '100' } } );
59
60
    warnings_exist { C4::AuthoritiesMarc::AddAuthority( $record, undef, $type->{authtypecode} ); }
61
    qr/before_authority_action called with action: save, authority_id: /,
62
        'AddAuthority calls the before hook with action=save';
63
64
    Koha::Plugins->RemovePlugins;
65
    $schema->storage->txn_rollback;
66
};
67
47
subtest 'after_authority_action hook' => sub {
68
subtest 'after_authority_action hook' => sub {
48
    plan tests => 3;
69
    plan tests => 3;
49
70
(-)a/t/lib/plugins/Koha/Plugin/Test.pm (-3 / +17 lines)
Lines 215-224 sub after_item_action { Link Here
215
    }
215
    }
216
}
216
}
217
217
218
sub before_authority_action {
219
    my ( $self, $params ) = @_;
220
221
    my $action       = $params->{action} // '';
222
    my $payload      = $params->{payload};
223
    my $authority_id = $payload->{authority_id} // '';
224
    my $record       = $payload->{record};
225
226
    Koha::Exception->throw("before_authority_action called with action: $action, authority_id: $authority_id");
227
}
228
218
sub after_authority_action {
229
sub after_authority_action {
219
    my ( $self, $params ) = @_;
230
    my ( $self, $params ) = @_;
220
    my $action = $params->{action}       // q{};
231
    my $action    = $params->{action} // q{};
221
    my $id     = $params->{authority_id} // 0;
232
    my $payload   = $params->{payload};
233
    my $authority = $payload->{authority};
234
    my $id        = $payload->{authority_id} // 0;
235
    my $record    = $payload->{record};
236
222
    Koha::Exception->throw("after_authority_action called with action: $action, id: $id");
237
    Koha::Exception->throw("after_authority_action called with action: $action, id: $id");
223
}
238
}
224
239
225
- 

Return to bug 40984