2

I want to overwrite the maximum charcater limit to 320. It is defined in /core/lib/Drupal/Core/Render/Element/Email.php

Can anybody tell me how to overwrite it?

2 Answers 2

3

You can using hook_element_info_alter.

mymodule.module

/*
 * Implements hook_element_info_alter().
 */
function mymodule_element_info_alter(array &$info) {
  if (!empty($info['email'])) {
    $info['email']['#maxlength'] = 320;
  }
}

Flush all caches so hook gets pickedup.

As Neograph734 pointed out, the mail column's VARCHAR length will also have to be increased in the DB. See Resizing Fields in Drupal 8 Without Losing Data to learn how you can accomplish that.

8
  • 2
    Be aware that although this will allow you to enter a value longer than 255 characters. The database definition for the email field (at least on the user) is varchar(254). So you will not be able to store the longer value. Commented Oct 12, 2020 at 12:06
  • 2
    Also worth noting that the RFC which said email addresses are 320 chars long was wrong - 254 is the right number (256 including < and >)
    – Clive
    Commented Oct 12, 2020 at 18:11
  • I had tried hook_element_info_alter ,What i want if any new email field is created for a entity , the Max length is database should also be changed ,If i go manually and update the length for testing /core/lib/Drupal/Core/Render/Element/Email.php. to 320 ,every new email field created in database also has 320 charcater length
    – namit garg
    Commented Oct 15, 2020 at 20:55
  • @namitgarg One should avoid hacking/patching core, but in this particular case since it's a constant, patching the value makes sense. If you use a composer managed site, create a patch and add the patch path to composer so it applies it every time you update Drupal core.
    – No Sssweat
    Commented Oct 16, 2020 at 2:54
  • 1
    @namitgarg its impossible, see this issue and also change record. So the only way is to patch it.
    – No Sssweat
    Commented Oct 20, 2020 at 5:26
0

This can be only changed in patching core , there is a patch contributed here https://www.drupal.org/project/drupal/issues/3177834

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.