In the COOKiES Drupal 8+ module, we had the issue that config entity string translations were not possible with text > 128 characters length: https://www.drupal.org/project/cookies/issues/3327045
The reason for that is in core. Form API Elements of type Textfield have a general #maxlength
of 128 characters:
/**
* {@inheritdoc}
*/
public function getInfo() {
$class = static::class;
return [
'#input' => TRUE,
'#size' => 60,
'#maxlength' => 128,
[...]
This default is "inherited" by Drupal\config_translation\FormElement\Textfield:
/**
* {@inheritdoc}
*/
public function getTranslationElement(LanguageInterface $translation_language, $source_config, $translation_config) {
return [
'#type' => 'textfield',
] + parent::getTranslationElement($translation_language, $source_config, $translation_config);
}
This is hard to understand, as on the other hand there's no technical limit documented for the String (StringData) Schema datatype: https://git.drupalcode.org/project/drupal/blob/8.0.x/core/config/schema/core.data_types.schema.yml
So while this limit may (have) be(en) useful for database values with VARCHAR(128) like in former Drupal 7 times, I don't think this limit still makes a lot of sense.
And if it does, why 128 characters? Wouldn't 256 be a better general value than if there has to be a limit or an assumption to be made?
That's why I created a Drupal core issue to discuss a change here:
https://www.drupal.org/project/drupal/issues/3331028
As I saw, we were not the first Drupal users running into this issue, as this forum post shows:
So finally there are different ways to work around this, until there's a possible Core fix. The one from the forum post, or ours from the COOKiES module. For us, the hook_form_alter() solution wasn't fitting.
Here's our snippet from the related commit:
/**
* Implements hook_preprocess_HOOK().
*/
function cookies_preprocess_config_translation_manage_form_element(&$variables) {
// Temporary fix for: https://www.drupal.org/project/cookies/issues/3327045
// Drupal limits config translation textfields (string) to a maxlength of 128.
// In our forms we set #maxlength to a higher limit so we need to remove
// the limit also for the config translation forms:
// Only for textfields:
if (!empty($variables['element']['translation']['#type']) && $variables['element']['translation']['#type'] == 'textfield') {
// Only for items starting with 'source[config_names][cookies.',
// so we don't affect other items.
if (strpos($variables['element']['source']['#name'], 'source[config_names][cookies.') === 0) {
// Remove the #maxlength limit entirely. There's no technical limit.
unset($variables['element']['translation']['#maxlength']);
}
}
}
I hope this helps some of you.
Please help to improve things and participate in fixing Drupal issues :)