Bug 36542

Summary: In C4/AddBiblio, plugin hook after_biblio_action is triggered before the record is actually saved
Product: Koha Reporter: Arthur Suzuki <arthur.suzuki>
Component: Plugin architectureAssignee: Arthur Suzuki <arthur.suzuki>
Status: Needs Signoff --- QA Contact: Tomás Cohen Arazi <tomascohen>
Severity: enhancement    
Priority: P5 - low CC: jonathan.druart, lucas
Version: unspecified   
Hardware: All   
OS: All   
See Also: https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=22709
Change sponsored?: --- Patch complexity: ---
Documentation contact: Documentation submission:
Text to go in the release notes:
Version(s) released in:
Attachments: Plugin which demonstrate the issue
Bug 36542: after_biblio_action plugin hook is now called after the dbx transaction is finished

Description Arthur Suzuki 2024-04-08 09:11:53 UTC
To reproduce :
1) Install a plugin which uses the biblionumber of a created record in the "after_biblio_action".
2) Verify the biblionumber is missing

This is because the call to the hooks is done before the DB transaction is actually done (in AddBiblio function).
Comment 1 Arthur Suzuki 2024-04-10 09:09:17 UTC
Created attachment 164589 [details]
Plugin which demonstrate the issue

I made a plugin which demonstrate the issue.
After installing the plugin in KTD, you'll have to open /var/log/koha/plack-intranet-error.log.

Then, if you create a record, you'll see that when the "after_biblio_action" hook is called, the biblio object is available for some Koha::Biblios namespace thanks to some caching mechanism (first warn in the plugin code is not triggered).
However, when the hook is called, it is actually not yet saved to DB.
This can be verified by doing a call to an API during the call, and see that this api call do not find the record.
This test is implemented in the plugin and you will see in the plack-intranet-error.log that the record is not found.
Comment 2 Arthur Suzuki 2024-04-10 11:40:09 UTC
Created attachment 164607 [details] [review]
Bug 36542: after_biblio_action plugin hook is now called after the dbx transaction is finished
Comment 3 Arthur Suzuki 2024-04-10 11:41:15 UTC
if the patch is applied, the log will contains the record data.
Comment 4 Jonathan Druart 2024-04-17 09:10:33 UTC
We need to log the modified version of the record. We must log after the hook call IMO.