When translating text fields 255 is hard coded as the max length which isn't always the case

Created on 2 November 2023, about 1 year ago
Updated 13 November 2023, about 1 year ago

Problem/Motivation

When translating text fields, the function translateTextField has a hard-coded 255 assumed as the maximum length of the text fields. On my installation there's a field with a lower max length configured and that causes a Drupal\Core\Entity\EntityStorageException: SQLSTATE[22001]: String data, right truncated: 1406 Data too long exception in a rare case where the original value of the field is below that max length but the translated is above.

Steps to reproduce

Create a text field with a max length lower than 255. Fill it with English (or whatever the core language is) up to the maximum length. Translate to a language which will have more characters than the maximum length configured. This should produce the above written exception upon triggering automatic translations.

Proposed resolution

Proposed patch attached in the file section. Besides checking for the max length and using that instead of a fixed 255, I've changed the strlen to mb_strlen.

🐛 Bug report
Status

Fixed

Version

2.1

Component

Code

Created by

🇭🇷Croatia abaudoin

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024