Problem/Motivation
Sometimes we need to set larger labels on field instances. One example might be on boolean fields where we can use the label as string to display text at the checkbox.
Right now, by default the textfield form element \Drupal\Core\Render\Element\Textfield sets the maxlength to 128
/**
* {@inheritdoc}
*/
public function getInfo() {
$class = get_class($this);
return [
'#input' => TRUE,
'#size' => 60,
'#maxlength' => 128,
...
...
and the label (\Drupal\field_ui\Form\FieldConfigEditForm::form) element doesn't provide any alternative \Drupal\field_ui\Form\FieldConfigEditForm::form
// Build the configurable field values.
$form['label'] = [
'#type' => 'textfield',
'#title' => $this->t('Label'),
'#default_value' => $this->entity->getLabel() ?: $field_storage->getName(),
'#required' => TRUE,
'#weight' => -20,
];
So the maxlength is set to the defaults (128)
Proposed resolution
The proposed solution is to set the #maxlength to a higher value, for instance 255. Since it is a config entity, we should not have conflicts with the database model.
Release notes snippet
Field labels can now be up to 255 characters long. This has no impact on the API, but require attention in admin themes to account for the longer possible label length.
| Comment | File | Size | Author |
|---|---|---|---|
| #12 | 3184542-increase-textfield-maxlength-to-255.patch | 535 bytes | olha_rybak |
| #5 | After Patch.png | 110.01 KB | mitthukumawat |
| #5 | Before Patch.png | 98.24 KB | mitthukumawat |
Issue fork drupal-3184542
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:
Comments
Comment #3
jlbellidoComment #4
jlbellidoComment #5
mitthukumawat commentedI have applied the patch and the field label maxlength is 255 now.
I have checked the maxlength in inspect element.
Adding screenshots for reference.
Comment #6
mitthukumawat commentedComment #10
catchDiscussed this with @alexpott briefly. Long field labels cause some minor UI issues (very long page title when configuring etc.), however these already exist with 128 characters. Apart from this we couldn't really see a reason not to commit as-is.
Committed 5dccbed and pushed to 9.3.x. Thanks!
Comment #12
olha_rybak commentedThe same issue is still actual in Drupal 11.3. Here is a patch that fixed it for me