Drag & drop addition adds meaningless numbers to table data, could break integrating code

Created on 15 February 2023, almost 2 years ago
Updated 3 May 2024, 7 months ago

Problem/Motivation

In this commit, drag and drop functionality was added.

The new functionality saves the weight values from the frontend into the backend, which is problematic. This potentially introduces a new property 'weight' into any/all rows. The base TableField formatter was updated to be aware of this, but the problem extends to any and all code that deals with raw tablefield values. This includes the module's own Drupal\tablefield\TableValue typed data API class, which was not updated to exclude 'weight' values.

Proposed resolution

I do not think saving the weight is a good approach. Nor do I think it would be best to try to simply work around its presence in every instance within Tablefield. Even if everything within the Tablefield module itself is updated that way, this data format change will also pose a problem to any custom code dealing with raw TableField data. (If a release was tagged today, I think that would warrant flagging this as a breaking change in the release notes.) And that seems unnecessary - I don't think there's a reason to record the weight value.

It should be feasible to use an #element_validate callback in the TableField element class to:
- Sort the rows in a working copy of the data.
- Unset the weight values in that working copy, and then set the weight-less data back using $form_state->setValueForElement().
setValueForElement() sets the data that will be used for form submission, but it does not overwrite user input. So this should not be a problem for AJAX operations or other submissions that result in the form getting returned back, so long as the weight logic uses the #value attribute that passes in user input. Which the drag-and-drop patch is correctly doing.

Remaining tasks

Implement the solution to remove weight data in a element_validate callback.

User interface changes

None.

Data model changes

This will return the data model to what it was before the drag-and-drop commit introduced this new wrinkle, so I think that should be fine.

I don't know if this seems worth an update hook. The drag and drog commit was added to the dev branch months ago, but has yet to be released in a tagged version.

πŸ› Bug report
Status

Needs work

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States bvoynick

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

Comments & Activities

Production build 0.71.5 2024