Problem/Motivation
A problem with the library UI / architecture can lead to duplicate library items being created by accident.
Steps to reproduce
Set up a content type with a paragraph field and a paragraph type that is library-enabled.
Create a node of this content type, add a paragraph to the node, promote the item to the library, then unlink it from the library, make additional changes, then promote it to the library again.
Expected results:
* Only one library item is created.
Actual results:
* Two library items are created.
Proposed resolution
Library items are only created once the node is saved, until that point they remain in the edit form and are editable the same as any other content.
Remaining tasks
Work out how to do this with the current UI.
Make the necessary changes.
User interface changes
During initial creation of a pice of content it is possible to continue editing a paragraph that is selected to be added to the library.
API changes
TBD
Data model changes
n/a
Issue fork paragraphs-3578895
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
giaczug commentedComment #5
giaczug commentedSolution: Defer library item creation until form save
The issue was caused by library items being created immediately when the "Promote to library" button is clicked, rather than when the parent entity is saved. This meant multiple clicks (promote → unlink → promote) would create duplicate library items in the database.
Changes Made
Modified
paragraphs_library.moduleto defer library item creation:paragraphs_library_make_library_item_submit()- Changed to set apending_library_promotionflag instead of creating the library item immediately. The paragraph remains editable.paragraphs_library_cancel_library_promotion_submit()- Added new submit handler to allow users to cancel a pending promotion before saving.paragraphs_library_paragraphs_widget_actions_alter()- Updated to show "Cancel library promotion" button when a paragraph is flagged for promotion, keeping the paragraph editable during this state.paragraphs_library_form_alter()+paragraphs_library_process_pending_promotions()- Added form alter hook and custom submit handler that intercepts form submission and creates library items for flagged paragraphs just before the entity is saved.How It Works Now
from_libraryreferenceComment #6
giaczug commentedSorry, the change I proposed works, but it breaks some tests. Does anyone have a solution to this problem?
Comment #7
damienmckennaThank you for working through that!
I ran the tests locally and it's failing on the translation page; it's expecting to see the text field to translate but instead it indicates that the text field is not translatable.
Comment #8
giaczug commentedOK! I fixed all the tests!
Comment #9
damienmckennaI've done a little bit of testing with this patch and so far it seems to work great! Thank you!