Problem/Motivation
At https://www.drupal.org/docs/drupal-apis/configuration-api/configuration-... there's no information at all about possible length limits for the several data types.
So when creating a config entity settings form (or anything else like that), you might assume that you can use data types like
- string
- label
- ...
without having to care for a limit. For config entities, I guess that assumption is also absolutely correct (if it is, it would still be nice to write that down on the documentation page). For content entities that are stored into the database, of course limits are important. Still 128 isn't a good limit anymore?
Now, when creating the settings forms for the config entity, we were wondering that a text with > 128 characters could not be entered and was cut off in the UI.
The reason was the maxlength="128" attribute on the form elements. Okay so far, but strange default behavior.
Modifying this limit were unsure, if there's a technical limit, so that when changing this FAPI textfield element #maxlength and saving it, might lead to any kind of errors or just cut off text. As I couldn't find any documentation, we tried and couldn't find issues with that.
Later on, we used config translation to allow translating the values and run into the same issue again, now for the config translation form. To increase / remove the limit, we used a hook, until this is solved.
The issue came up with #3327045: Cookies Service Input fields limit text length too short at the COOKiES module.
Steps to reproduce
Follow the steps at https://www.drupal.org/docs/drupal-apis/configuration-api/creating-a-con... and try to enter a label with > 128 characters.
Even if there's no storage reason for the limit, the input is limited to 128 characters and changing this is hard.
Proposed resolution
There are several levels, where this might be solved, but I'm not experienced enough to decide the long term strategy.
These are the options from most global and BC-dangerous to BC-safe solutions:
a) Remove the #maxlength default from \Drupal\Core\Render\Element\Textfield entirely
b) Increase the #maxlength default at \Drupal\Core\Render\Element\Textfield to 256 to match at least 99% of the cases when making such an assumption
c) Unset the #maxlength limit at Drupal\config_translation\FormElement\Textfield
d) Increase the #maxlength limit at Drupal\config_translation\FormElement\Textfield to 256 to match at least 99% of the cases when making such an assumption
e) Introduce a maxlength or size similar property for config schema (abc.schema.yml) which is used to define the limits centrally and provide helpers to determine the limit.
Based on the results of the discussion, we are going with option B, increasing the default limit to 256.
Remaining tasks
- [X] Decide on an option: B for BC, A in a follow-up
- [X] Update issue summary
- [X] Create CR
- [ ] Framework Manager Review
- [ ] Release Manager Review
- [ ] Decide if this is to be done in a major release, see #15.
- [ ] Document the limits or that there is no technical limit at the Configuration schema/metadata docs page after that has been confirmed
User interface changes
None
API changes
None
Data model changes
None
Release notes snippet
Increased the default textfield #maxlength from 128 to 256
| Comment | File | Size | Author |
|---|---|---|---|
| #44 | 3331028-nr-bot.txt | 124 bytes | needs-review-queue-bot |
| #42 | remove_default_textfield_maxlength_128-3331028-42.patch | 1.48 KB | nord102 |
| #40 | 3331028-nr-bot.txt | 90 bytes | needs-review-queue-bot |
Issue fork drupal-3331028
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:
- 3331028-increase-or-remove-maxlength-remove-11.x
changes, plain diff MR !11024
- 3331028-increase-or-remove-maxlength-remove
changes, plain diff MR !4008
- 3331028-increase-or-remove-maxlength-increase-formelement
changes, plain diff MR !4005
- 3331028-increase-or-remove-maxlength-unset
changes, plain diff MR !4006
- 3331028-increase-or-remove-maxlength-increase
changes, plain diff MR !4007
Comments
Comment #2
anybodyComment #3
anybodyComment #8
anybodyImplemented the 4 options a - d as example MR's. I'd tend to choose a or b and I think the limit of 128 characters never should have been introduced after Drupal 7 with configuration.
Comment #9
anybodyAs next step it should be discussed which way to go with which risks, eventually BC's (= core target version) etc. The MR's are just examples to show possible ways. Would be nice to have core maintainers feedback here.
Comment #10
grevil commentedI'd agree with either solution a) or b), where setting the limit to 256 would be more explicit, as you wouldn't usually need a higher char count for labels etc. anyway. But removing the maxlength entirely could help with the remaining 1% needing a higher char count.
So if it wouldn't have any negative side effects, I'd vote for solution a), otherwise b).
EDIT: The failing tests seem unrelated.
Comment #12
longwaveOut of interest I looked through git history and found that we set maxlength to 128 for textfields in #35644: Change defaults on form elements to most common values - previously it was an arbitrary value of 70 instead.
I am not sure what the best option is here. I don't really like any of them except the simplest one: remove the default maxlength entirely - but I also share the BC concerns. There is definitely a risk of introducing bugs if we just drop the length, because of things like database column sizes being overflowed where previously this was not possible.
We could (in theory) deprecate it by detecting when it's not set, warning users that #maxlength default will change in the next major, etc, but is it worth it? I am not entirely sure of the benefit of changing it now given that form API elements can just override this value if they need to.
Adding framework manager review tag to get more visibility on this.
Comment #13
anybodyThanks for the feedback @longwave. I shouldn't repeat myself, as I already wrote those points into the issue summary, but as short reply to your comment
Comment #14
duaelfrI faced that issue in an views exposed filter on an entity reference field using the autocomplete widget.
One of my users tried to search for a referenced entity which title is 138 chars long (+ double quotes and ID between parenthesis). The autocomplete works well and the field is filled when selecting the right result but after submitting the form, they gets an error and no results.
Comment #15
catchThinking about the deprecation, I'm not sure we can do that, unless we want to force everyone to set a maxlength, otherwise there's no way to remove the deprecation message for modules that don't want it set.
This is true but I feel like for entities this would already be a problem due to JSON:API. The database will either truncate the value or throw an exception, the exception could be jarring, but it would only be for someone who previously would not have successfully submitted that form with the same values.
I think this is probably OK to do in a minor release, but with quite a loud change record/release note.
It's also the sort of thing we could make 'major version only', generally we've avoided that as much as possible, but this might be a case where that would work.
Comment #16
anybodyOk we shouldn't try to change the MR to 11.x ;D
This should be just fine and patchable ...
Comment #17
quietone commentedI am doing triage on the core RTBC queue.
I read the issue summary and the most importantly the proposed resolution is a set of options. I then read the comments to figure that out. The comment that set this to RTBC supported 2 of the options. Since that is not clear I am tagging for an IS update and setting to NW.
In #15 catch asked for a CR. I am adding that tag. Of course, that shouldn't be made until this has a definite resolution. He also considered that this might be done in a major release. Therefor, I am tagging for RM review as well.
I am setting to NR for discussing and selecting the option to implement.
Comment #18
smustgrave commentedFor the issue summary update and change record.
Comment #19
grevil commentedLet's simply elevate the max length to 256. This should be enough. Removing it entirely might have further consequences.EDIT: Sorry, a bit too fast on the submit button here.
Comment #20
grevil commentedAccording to the discussion here, we should proceed with MR !4008 as @catch confirmed the approach in #15.
I'll create the change record.
Comment #21
anybodyMarked the approaches we probably won't take as "Draft". Maybe useful for a follow-up or discussions. Feel free to close otherwise.
Comment #22
grevil commentedOk, I created a Change Record! Back to "RTBC".
Comment #23
anybodyIssue summary updated. Waiting for Needs framework manager review, Needs release manager review now!
Comment #24
grevil commented1 test fails.
Comment #25
grevil commentedOK, the last failing test is simply an unrelated racing condition problem. I don't think, this should be fixed in this issue, as it is completely unrelated.
Comment #26
grevil commentedComment #27
grevil commentedSetting this back to "Needs work" as I don't think any core maintainer would commit an MR containing failed tests. Maybe someone can help with #3396907: testCreateViewWizard fails, when removing the "#maxlength" property of the textfield form element. I have no idea, what makes the test fail and can not reproduce it through manual testing.
Comment #28
grevil commentedAlright, back to needs review! Special thanks to @lendude, who provided the "_populate" fix on Slack! Thanks again! 👍
Please credit him manually!
Comment #29
grevil commentedComment #30
grevil commentedPipeline is green! https://git.drupalcode.org/issue/drupal-3331028/-/pipelines/39305
Comment #31
anybodyBack to RTBC! :)
Comment #35
catchAre there any existing forms in core (entity labels? Machine names?) where we might be relying on the default 128 #maxlength and need to add an explicit value in those forms?
Comment #36
anybody@catch: Any ideas how we can be sure about that?
Looking at this: https://git.drupalcode.org/search?search=%23maxlength&nav_source=navbar&...
it seems there are at least many cases where peolple added the explicit
#maxlengthvalue, where it was needed!An important example you made: Machine names:
https://git.drupalcode.org/issue/drupal-3331028/-/blob/3331028-increase-...
So I think we should ensure this is safe wherever possible, but on the other hand things won't get better, if we wait longer to "fix" this. Happy to help if anyone has further ideas how to be even more safe!
Comment #37
catch@Anybody can we look at core usages of #type 'textfield' where it doesn't have #maxlength?
Comment #38
smustgrave commentedFollowing up @Anybody if you had a chance to look at #37.
Comment #39
anybody@smustgrave thanks for the ping, I didn't forget this, but currently can't take the time. Open for anyone, sorry.
Comment #40
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #41
mile23I did a search in core (granted, it was 10.2), for
'#type' => 'textfield', and stopped counting at 10 when I found files that rely on the default value.Also, it seems like relying on the default value is kind of OK, which is why it's there.
Config offers a schema constraint of
Length.max, so it seems like a config form would use that value if it's present, only using the form element default if there was no such constraint. Ideally this would happen in some automatic or semi-automatic way to make it easy to build config forms. Maybe as a method onConfigFormBasethat does magic for you.It seems like there would be some other issue where this is happening, but I don't see it, other than in initiatives to make sure all config has constraints: #2869792: [meta] Add constraints to all config entity types It also seems like it would slot right into this CR's purpose: https://www.drupal.org/node/3373502
Comment #42
nord102Re-rolling the MR as a patch to apply to
10.4.0Comment #43
colanComment #44
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #47
grevil commentedCreated a new branch targeting 11.x (3331028-increase-or-remove-maxlength-remove-11.x).
@mile23, meaning at least 10 form elements without
#maxlengthset? Should we manually set the `maxlength` attribute there?Couldn't we just proceed and merge this? I understand, that this could potentially lead to problems, but we are using this patch in production for a year now and never had any issues with it.
Comment #48
catchI think it would be good to at least investigate whether there are high-usage forms (anything visitor/editor facing mainly) that might be relying on the default max length that need updating to have it set explicitly before merging this. Untagging for RM/FM review since I've looked at this twice now.
Comment #49
samlerner commentedI did a search through core 10.4.3 and came up with 475 different
'#type' => 'textfield'instances. Some that I saw had maxlength set, but a lot did not, including fields for things like database names and URL paths in Views forms.I don't know how many problems it would cause by removing the default limit, or by having the limit at 256 instead of 128. But it feels like having some default limit is a good idea. I can update the MR to increase the default limit to 256, instead of removing it, if folks are on board.
Comment #50
anybody@samlerner thanks for your effort on this. My personal opinion is that the form layer should not assume any limit. This is just logically wrong. The limit (where it exists) comes from the database layer and should only be set, where it's needed.
Core maintainers should decide, if increasing the limit mitigates the problem for now, but I think it also has risks and doesn't solve the real problem. Anyway, we need someone to decide how to fix this flaw finally. :) Thanks!
Comment #51
andypostMost of issues last years (starting with node title length) are all about 128 to 256 transition
Database fields mostly also have 256 for text so the remains is easy to check
Comment #52
samlerner commentedI updated the MR to restore the
#maxlengthand increase it to 256. I think this is the best we can do, given the large number of existing fields without any limit set.If the text input is too long, I think it's a better user experience to have it identified at the form, rather than the database layer. I agree with @anybody that the limit really exists at the database layer, so it could be handled there. But DB limit errors are harder for end users to understand.
Comment #53
smustgrave commentedCould the summary be cleaned up about what solution was decided please
Comment #54
samlerner commentedComment #55
samlerner commentedI updated the summary to note we're going with option B, increasing the default limit to 256.
Comment #56
smustgrave commentedLooking at the MR increasing to 256 makes sense but that doesn't line up with the change to core/modules/views_ui/js/views-admin.js. What's the scenario when maxlength isn't there? Shouldn't we have test coverage for that scenario?
Comment #57
robbt commentedI'm just running into this issue and it seems like increasing the max-length to 256 would be an improvement over setting the default max-length to 128 as it currently is.
In terms of the views-admin.js change it may make sense to just remove that change if it makes merging this easier unless that is specifically one of the goals of this MR.
It seems like that was likely created in-line with the idea of removing the default max-length altogether and since we are simply changing the default we shouldn't need to touch this check.
Another reason to go ahead with this change is the default field length of a text field created using the UI is 255. So setting the default max-length to 128 can create some issues for instance I ran into an issue in the conditional_fields module where they just create a text-field and it has the default max-length of 128 and so I was unable to input the required value.