Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX Ensure files can be removed from elemental blocks #1267

Conversation

GuySartorelli
Copy link
Member

@GuySartorelli GuySartorelli commented Nov 6, 2024

In CMS 5.2 and earlier, all fields in the form are included in the POST data, including empty upload fields.

In CMS 5.3 the formschema form submission logic is used instead, which omits data for empty upload fields (and possibly for other empty fields as well).

Normally this would be handled just fine with $form->saveInto($record), but elemental doesn't use that so missing data just gets ignored instead of being set to null.

CI note

The new behat test relies on a test extension added in silverstripe/silverstripe-frameworktest#210
That PR needs to be merged before this can go green.

Issue

@GuySartorelli GuySartorelli marked this pull request as draft November 6, 2024 00:00
Comment on lines +142 to 144
// Would usually be handled by $form->saveInto($element) but since the field names
// in the form have been namespaced, we need to handle it ourselves.
$element->updateFromFormData($dataWithoutNamespaces);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really we should just rename the fields in the form, and then call $form->saveInto($element) - but doing that now would be an API break because someone may have customised the updateFromFormData() method in their custom elements.

continue;
}

$cmsFields = $this->getCMSFields()->saveableFields();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting the saveableFields() explicitly - things like LiteralField don't need to be touched here.

Comment on lines +660 to +661
foreach ($cmsFields as $fieldName => $field) {
$datum = $data[$fieldName] ?? null;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there's data, set to the value in the data - otherwise set to null.

This is still skipping a bunch of stuff that $form->saveInto() would do, but fully replicating that logic isn't in scope here. This change is the least disruptive change required to fix the bug.

Copy link
Member

@emteknetnz emteknetnz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code change looks good

I've merged the frameworktest PR and re-rerun behat and it all works

One tiny change

tests/Behat/features/file-upload.feature Outdated Show resolved Hide resolved
@GuySartorelli GuySartorelli force-pushed the pulls/5.3/allow-removing-files branch from 87a4e42 to 527b221 Compare November 6, 2024 02:26
@emteknetnz emteknetnz merged commit 23af9c0 into silverstripe:5.3 Nov 6, 2024
24 checks passed
@emteknetnz emteknetnz deleted the pulls/5.3/allow-removing-files branch November 6, 2024 02:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants