symphonycms: Cannot delete content from "optional" fields
Apologies in advance if this has already been addressed, however, the behaviour is still present in the 2.7.x branch and I’ve not located a fix or existing issue. Appears this bug has been around since 18th of May.
Affected Symphony version(s) : Since PR #2664
This relates to code in EntryManager line 119 (https://github.com/symphonycms/symphony-2/blob/2.7.x/symphony/lib/toolkit/class.entrymanager.php#L119)
Expected Behaviour
If there is a field flagged as optional, editing it to have an empty value should cause anything that was there to be deleted.
Current Behaviour
The original value persists.
Steps to reproduce
- Create a new section.
- Add a text input field to that section and set it to optional
- Create a new entry in that section, ensuring to add a value to the text input. e.g. “APPLE”
- Go back to the entry to edit it. Delete the value “APPLE” from the text input field, then save the entry.
- Observe the value “APPLE” persists.
Reason
When an entry is saved, field data is passed through this method. For an optional field with no value, the data looks something like this:
int(113)
array(0) {
}
This code is executed near the top of EntryManager::saveFieldData()
:
// Check if we have field data
if (!is_array($field) || empty($field)) {
return;
}
When this code is executed, it will skip over fields with an empty value. Consider the previous code https://github.com/symphonycms/symphony-2/blob/0dd2c1238a62aa620d7f7e5737843380a31c8b6a/symphony/lib/toolkit/class.entrymanager.php#L186
Note that a call to Symphony::Database()->delete(...)
is triggered first. This allows the desired behaviour to occur. i.e. removing a value from an optional field.
Solution
Delete field data before checking for an empty state.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 15 (14 by maintainers)
Commits related to this issue
- Restore the 2.6.x field behavior for empty arrays Symphony always treated empty arrays as being null values, i.e. deleting old data without doing an insert statement afterwards. The refactor made in... — committed to DeuxHuitHuit/symphonycms by nitriques 7 years ago
- Prevent acting on non existing tables (#2664) This change makes sure that the changes introduced in 4b5067f38 do not cause a bug when the table does not exists. The old code (pre 2.7.0) discarded all... — committed to symphonycms/symphonycms by nitriques 7 years ago
- Restore the 2.6.x field behavior for empty arrays (#2744) Symphony always treated empty arrays as being null values, i.e. deleting old data without doing an insert statement afterwards. The refac... — committed to symphonycms/symphonycms by nitriques 7 years ago
- Fix optional values for upload field *Some* fields, like the upload field, would return false when the data is deleted instead of an empty array. The patch introduced in #2744 did not account for tha... — committed to DeuxHuitHuit/symphonycms by nitriques 7 years ago
- Fix optional values for upload field *Some* fields, like the upload field, return false when the data is deleted instead of an empty array. The patch introduced in #2744 did not account for that: It ... — committed to DeuxHuitHuit/symphonycms by nitriques 7 years ago
- Fix optional values for upload field (#2746) *Some* fields, like the upload field, return false when the data is deleted instead of an empty array. The patch introduced in #2744 did not account for... — committed to symphonycms/symphonycms by nitriques 7 years ago
Hey Alistair ! Great to see you report a bug! I confirm the issue. Thanks!