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

Created on 2 November 2023, 8 months ago
Updated 13 November 2023, 7 months 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.69.0 2024