I was just creating some nodes programmatically and was running into an error that the value I was setting for my list field wasn't an allowed option.
It turns out, when I had input my allowed values, I had used the following form:
key | value
So when I was trying to set the field value to 'key', the system was expecting 'key '.
While this isn't a bug, this might be an issue that others would run into too. I don't think that trimming the values would cause problems for anyone, and would possibly avert trouble for the small number of people who put spaces on either side of the separator.
Comments
Comment #1
dddave commentedKicking upwards...
Comment #2
Anonymous (not verified) commentedThis should be a simple fix. Adding Novice tag.
Comment #3
kars-t commentedAdding some trims is really easy. But I am not sure if the description of the textarea should be altered as well and if we need updated tests for this?
And maybe we should only trim the key but not the value. Doesn't makes much sense to me to use spaces in the label but we would probably change an expected behavior?
Comment #4
eloivaqueI try the patch of #3 comment, and its working for me.
Comment #5
swentel commentedMakes sense to me, patch is fine enough.
Comment #6
xjmLooks great!
We should add some simple automated test coverage for this fix, and perhaps an inline comment explaining why we're trimming.
Comment #7
plopescHello
Patch re-rolled including basic test and inline comment explaining why we're trimming.
Regards
Comment #8
yesct commentedThis could also use a tests only patch (to show the bot will fail without the fix, that the fix works and the tests will work to show the fail).
Comment #9
plopescOf course, re-rolling only test and combined patches.
Regards.
Comment #10
swentel commentedFine for me.
Comment #11
catchMaybe we need an actual widget for this. Until that point this looks good.
Committed/pushed to 8.x, might be backportable?
Comment #12
plopescHello
Attaching basic backport patch, including tests.
However, I suppose that there will be problems for people who actually uses values that includes spaces.
One option could be use hook_update_N to trim all stored values in the DB.
What do you think?
Comment #13
dcam commentedSetting status...
Comment #15
star-szr@plopesc - If you upload the test-only patch first, then the combined patch (as you did in #9), the system comment and setting to "needs work" (as in #14) won't happen. Provided the combined patch passes testing, of course :)
Comment #16
parthipanramesh commentedThe latest patch does work fine for me. In my opinion it's a good solution to use trim. Thanks!
Comment #17
David_Rothstein commentedI tested this patch on a D7 site with a field that already had spaces at the end of some of the list options. Regardless of whether the site admin put the spaces there intentionally or not, this causes big problems if the site already has data in the field. If you go to the Field UI settings page for the field and try to save it for any reason, you get a validation error - "Allowed values list: some values are being removed while currently in use" - and then you're kind of stuck.
I believe this would affect a site that upgraded to Drupal 8 too, and therefore probably needs some kind of upgrade/migration path to fix.
Even then, I'm not sure we can fix it in Drupal 7. People use machine names in code all the time (in addition to the database), so switching the machine names from underneath them sounds like a bad idea in a stable release.
Comment #18
penyaskitoDo we need to massage the data in D6-D7 => D8 migration then?
Comment #19
penyaskito\Drupal\migrate_drupal\Plugin\migrate\process\d6\FieldSettings::getSettingsalready trims the allowed values setting. I cannot find where we do the same with saved field values (if we do at all).Comment #32
quietone commentedThis issue was committed to 8.0 and was reopened for backport to Drupal 7. According to the testing in #17 the patch causes problems in Drupal 7 and recommends that it not be applied to Drupal 7. The remaining question was should field values be trimmed when migrating. From a migrate perspective changing the source value is not what the use expects and there needs to be strong evidence that it is needed. The Migrate API has been in use for a while now and I don't recall an issue to trim field values. If someone thinks that should be done then open an issue in the 'migration system' component.
Changing the status to Fixed and changing the version to when the commit was made.
Thanks all!