Draggable table still used but not necessarily rendered correctly when cardinality is 1

Created on 2 March 2024, 10 months ago
Updated 4 March 2024, 10 months ago

Problem/Motivation

There seem to be many issues around drag-and-drop and some others may be related or need to be resolved before a final fix can be implemented for this one in particular.

When a field has a cardinality of 1, it still gets displayed in a draggable table via field_multiple_value_form. This has been discussed in the very old issue #2272221 already, but not quite properly fixed in the current version for D8+. It may have been fixed for a while and perhaps a change to Drupal core broke it again. The paragraphs_preprocess_field_multiple_value_form() function sort of addresses it by trying to remove the table columns that shouldn't be displayed if the cardinality is 1, but it's no longer removing the right table columns. With the latest version of D10 (possibly earlier, can't go back and test/verify at this point), column 2 in the header and 3 of the rows needs to be removed for this to work properly.

Furthermore, it still displays the header actions button with a "Drag & drop" option to select, which shouldn't be there for a cardinality of 1.

Steps to reproduce

  • Use the latest version of Paragraphs in Drupal 10.2.3
  • Create a paragraphs field on a node (or other entity) with a cardinality of 1
  • Create/edit that entity and see it still shows the "Order" header and there's a gap to the right of the paragraph item under the "Order" column heading

Proposed resolution

One solution is to update the paragraphs_preprocess_field_multiple_value_form() function to remove the correct columns from the table header and each of the rows. However, that seems fragile, as it would have to be updated again if there's another change to multiple value tables in core at some point in future.

Instead, my suggestion is to go into the formMultipleElements function of the ParagraphsWidget class and around line 1397 where it sets the #theme to field_multiple_value_form, fix the cardinality options as follows:

REMOVE the '#cardinality' key since it's already set a few lines above, then set the '#cardinality_multiple' key like this:

'#cardinality_multiple' => ($cardinality > 1)

This simply won't render the table.

Then the header actions need to be updated to not show a drag and drop option based on cardinality, but I couldn't see where to fix that. As a short term solution, I created my own field widget plugin that extends the ParagraphsWidget class. It overrides formMultipleElements to first call the parent method and then alter the #cardinality_multiple key based on actual cardinality. I then had the buildHeaderActions method overridden to return an empty array, since in my case I'm only ever going to use my field widget for paragraph fields with a cardinality of 1 anyway.

🐛 Bug report
Status

Active

Version

1.17

Component

Code

Created by

🇨🇦Canada teknocat

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @teknocat
  • 🇫🇮Finland vermario

    hi! I saw the same issue you are seeing, but with translations (another situation in which the tabledrag functionality should not be there).

    I read your issue only after I found a possible fix, so I made a patch that actually does the same that you mentioned:

    One solution is to update the paragraphs_preprocess_field_multiple_value_form() function to remove the correct columns from the table header and each of the rows. However, that seems fragile, as it would have to be updated again if there's another change to multiple value tables in core at some point in future.
    

    Available in 🐛 Weight fields appear when editing a translation even if the order can't be changed Needs review

    Your other solution would work with fields with cardinality 1, but for translations I think we still need the other fix. although, I wonder if this works only for drupal 10.2...

Production build 0.71.5 2024