Problem/Motivation
The save which runs in hook_entity_insert causes a WSOD when using auto entity labels with the core book module. In that case if you try to create a new book on a completely new node you get an SQL duplicate primary key error on save (adding a book outline to an existing node doesn't trigger this issue).
Digging around the issue queue it looks like the hook_entity_insert was added to add support for the [node:nid] token: https://www.drupal.org/project/auto_entitylabel/issues/2920695.
Steps to reproduce
- Install both the core book and auto_entitylabel modules.
- Configure an entity to use both books and auto_entitylabel (I triggered this using 'hide the title field' option, but I suspect that the other options will trigger this too.
- Create a new node of that type. Select to create a new book from this node.
- Save the node
Expected behaviour:
- The node saves without error. Both the book and auto entity label information is set.
Actual result:
- A WSDO appears and the node fails to save. With errors turned on the error is a duplicate primary key error on the book table is shown.
Proposed resolution
While the second save in hook_entity_insert is not forbidden it is not recommended (according to the hook_entity_insert documentation at api.drupal.org). This stackexchange post explains the issue https://drupal.stackexchange.com/questions/225612/update-title-field-in-... (it talks about node_save but I think this still applies to entity->save()). The entity insert hooks run after the entity runs in the middle of the database transaction to save the entity, therefore the node id is available but not everything in saved.
The problem with the core book module is that it uses the node id as the primary key for its own book entities. I'm not quite sure why but book still thinks that there is no book entity associated with the node when the save in hook_entity_insert is called, therefore it tries to create a second outline, causing the duplicate primary key error.
According to the stackexhange post the way to avoid this is to run the second save after the initial database transaction has finished, and some code to do this. I've created a patch with this code in it.
I've only tested on my site. This removes the WSOD for book nodes. I don't use the [node:id] token so I haven't tested if this resolves that issue. Since this code doesn't remove the second save, only delays it, I don't think this will fix the double email issue.
Remaining tasks
This patch is a hack that removes the WSOD by deferring the second save until the end of the first transaction. Suggest that a more permanent solution be found.
User interface changes
API changes
Data model changes
| Comment | File | Size | Author |
|---|---|---|---|
| 3076302-save-after-transaction-24.patch | 756 bytes | demma10 |
Comments