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

Command icon 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

damienmckenna created an issue. See original summary.

giaczug made their first commit to this issue’s fork.

giaczug’s picture

Assigned: Unassigned » giaczug
Status: Active » Needs review

giaczug’s picture

Solution: 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.module to defer library item creation:

  1. paragraphs_library_make_library_item_submit() - Changed to set a pending_library_promotion flag instead of creating the library item immediately. The paragraph remains editable.
  2. paragraphs_library_cancel_library_promotion_submit() - Added new submit handler to allow users to cancel a pending promotion before saving.
  3. 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.
  4. 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

  • Promote to library → Sets flag, paragraph stays editable
  • Cancel library promotion → Clears flag (optional)
  • Save form → Creates library item and converts to from_library reference
  • Promote → Unlink → Promote → Save → Only one library item is created
giaczug’s picture

Sorry, the change I proposed works, but it breaks some tests. Does anyone have a solution to this problem?

damienmckenna’s picture

Status: Needs review » Needs work

Thank 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.

giaczug’s picture

Status: Needs work » Needs review

OK! I fixed all the tests!

damienmckenna’s picture

Status: Needs review » Reviewed & tested by the community

I've done a little bit of testing with this patch and so far it seems to work great! Thank you!