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.

Issue fork drupal-3184542

Command icon 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

jlbellido created an issue. See original summary.

jlbellido’s picture

Issue summary: View changes
jlbellido’s picture

Status: Active » Needs review
mitthukumawat’s picture

StatusFileSize
new98.24 KB
new110.01 KB

I have applied the patch and the field label maxlength is 255 now.
I have checked the maxlength in inspect element.
Adding screenshots for reference.

mitthukumawat’s picture

Status: Needs review » Reviewed & tested by the community

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

  • catch committed 5dccbed on 9.3.x
    Issue #3184542 by jlbellido, mitthukumawat, alexpott: Increase field...

catch credited alexpott.

catch’s picture

Issue summary: View changes
Status: Reviewed & tested by the community » Fixed
Issue tags: +9.3.0 release notes

Discussed 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!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

olha_rybak’s picture

The same issue is still actual in Drupal 11.3. Here is a patch that fixed it for me