Problem/Motivation

Using nested paragraphs we ran into the the issue that the weights were calculated wrong, after drag and dropping a paragraph that contained other paragraphs. What was wrong about the weights was, that they had the same values.
This occurred because of
first, how the updateField behavior in tabledrag.js is handling ordering, assigning the max value when running out of values

        case 'order':
          {
            var siblings = this.rowObject.findSiblings(rowSettings);
            if ($(targetElement).is('select')) {
              var values = [];
              $(targetElement).find('option').each(function () {
                values.push(this.value);
              });
              // only takes into account the values count on one level
              var maxVal = values[values.length - 1];
              
              // targetClass is field_name-delta-order and this function takes into account all values on every level
              $(siblings).find(targetClass).each(function () {
                if (values.length > 0) {
                  this.value = values.shift();
                } else {
                  this.value = maxVal;
                }
              });

and second, of how the order class is set in theme.inc inside the template_preprocess_field_multiple_value_form function

function template_preprocess_field_multiple_value_form(&$variables) {
  $element = $variables['element'];
  $variables['multiple'] = $element['#cardinality_multiple'];
  $variables['attributes'] = $element['#attributes'];

  if ($variables['multiple']) {
    $table_id = Html::getUniqueId($element['#field_name'] . '_values');
    // setting it up like doesn't take into account that multiple values on the same content could have the same field name
    $order_class = $element['#field_name'] . '-delta-order';

this leaves room for same name order errors, because we choose a popular name like 'field_body' for a paragraph field that contained other paragraphs.

Proposed resolution

Assigning a unique name to the order class using the unique table id instead of the field_name only, like

  if ($variables['multiple']) {
    $table_id = Html::getUniqueId($element['#field_name'] . '_values');
    $order_class = table_id . '-delta-order';

The reason behind this approach is that the table id already contains the field_name and also adds a unique part if the content is already on the view.

Remaining tasks

Is this a good approach or have I missed something similar in the issue queue?

Issue fork drupal-3098960

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

schaefdi created an issue. See original summary.

schaefdi’s picture

schaefdi’s picture

Assigned: schaefdi » Unassigned
candalt’s picture

Can confirm this behaviour. Have tested on a fresh installation:

Drupal: 8.8.5
Profile: Standard
Paragraphs: 1.11

Add paragraph "Child" with field "Title"
Add paragraph "Parent" with field "Title" and "Modules (field_modules)", relation to "Child" (many)
Add field "Modules (field_modules)" to "Article" nodetype, relation to paragraph "Parent"

Try to add multiple paragraphs of type "Parent" to "Article". After adding, drag and drop one paragraph to change order.

Result: Weight for all paragraphs are set to "0,1,1" and so on.

abalevik’s picture

Can confirm that patch #2 fixes the problem mentioned in the issue, which @candalt mentions.

Great if anyone else can confirm / test as well.

candalt’s picture

Have tested patch #2 and it is working fine.

Version: 8.7.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Branches prior to 8.8.x are not supported, and Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

kevinquillen’s picture

The patch applies, but I see the same error. The console has several "Error: Cannot find valid paragraph weight to adjust!" within a Layout Builder edit interface for a block with nested paragraphs.

avpaderno’s picture

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8.9.x is now open only to security issues.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.1.10 (June 4, 2021) and Drupal 9.2.10 (November 24, 2021) were the last bugfix releases of those minor version series. Drupal 9 bug reports should be targeted for the 9.3.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

fabsgugu’s picture

Patch tested, Work on version 9.3.3 for me.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Category: Task » Bug report
Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative, +Needs tests

This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.

This seems more like a bug potentially

Either way will need a test case

Did not test.

raphael apard’s picture

Patch tested, work on version 9.3.2 for me.

mediabounds’s picture

Patch in #2 resolved the issue for me!

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

pbonnefoi’s picture

Patch worked for me on Drupal 10.3 and Drupal 11.

pbonnefoi’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work

Previously tagged for tests which still appear to be needed. thanks

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

arunkumark’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests

As per the comment, I added a test case to check the unique class for the multi-field value. Moving to NR now.

godotislate’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new217.61 KB
new149.12 KB
new217.58 KB
new148.78 KB

I did some manually testing. On latest 11.x, after following the reproduction steps in #4 to create the paragraphs and fields, I created Article content with 3 Parent Paragraphs (Parent 1, Parent 2, and Parent 3), and each Parent Paragraph with 2 children (Parent 1 => Child 1, Child 2; Parent 2 => Child 3, Child4; Parent 3 => Child 5, Child 6). I then dragged Parent 3 to go above Parent 2 (See screenshot). After saving, Parent 2 incorrectly remained above Parent 3 (See screenshot).

I then applied the changes from MR 9897. Re-edited the page and dragged Parent 3 above Parent 2 again (see screenshot). After save, Parent 3 stays correctly above Parent 2 (See screenshot).

I tried to reproduce the issue mentioned in #8 with block content and nested paragraphs in Layout Builder, but I did not see the console errors.

The automated test that's needed is a functional test that does similar to steps to the manual test just described. But I'm not sure an automated test can be written using core alone, at least not easily. Given that and the fact that the fix is essentially a one-line diff, I wonder if it qualifies under #2972776: [policy, no patch] Better scoping for bug fix test coverage as something that can be committed without this test. Moving to RTBC for confirmation.

pbonnefoi’s picture

Thanks for your help ! Indeed, it seems rather difficult to write a functional test since this bug is a combination of core and paragraphs.

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

nod_’s picture

saving credits

nod_’s picture

Status: Reviewed & tested by the community » Needs work

the test can be removed here. it's testing that something we removed was removed

godotislate’s picture

Status: Needs work » Needs review

Unnecessary test removed and rebased MR.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

With test coverage removed seems pretty straight forward fix then.

  • nod_ committed 94d0636c on 11.1.x
    Issue #3098960 by pbonnefoi, godotislate, schaefdi, nod_, arunkumark,...

  • nod_ committed 6265e217 on 11.x
    Issue #3098960 by pbonnefoi, godotislate, schaefdi, nod_, arunkumark,...
nod_’s picture

Version: 11.x-dev » 11.1.x-dev
Status: Reviewed & tested by the community » Fixed

Committed and pushed 6265e217854 to 11.x and 94d0636ceaf to 11.1.x. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.