Problem/Motivation

The current token replacement implementation for autogeneration of the name field does no sanity checking or sanitization of the token inputs to ensure the resulting name is suitable for the field. One major problem is that some tokens can generate names that are too long for the name field, resulting in SQL errors that prevent the entity from being saved: "Drupal\Core\Entity\EntityStorageException: SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'name' at row 1: INSERT INTO "storage_field_data" (..."

Also, if a token value has not been set, the token name [in brackets] is used instead, which is different from the expected behavior from Auto Entity Label, which instead displays nothing in such cases. HTML and special characters will also be included in this field, with unexpected and undesirable results.

At a minimum, the auto-generated names should not be allowed to generate Drupal-breaking SQL errors. Beyond that, some basic cleanup of inputs would also be helpful.

Steps to reproduce

  1. Create a storage entity type with a long text field
  2. Configure 'Pattern for automatic name generation' with a token based off the long text field
  3. Attempt to create a storage entity with content in the long text field that is > 255 characters. (The 'name' field is VARCHAR 255.)

Proposed resolution

Currently, Drupal\storage\Entity\Storage::applyNamePattern() simply performs a \Drupal::token()->replace()
https://git.drupalcode.org/project/storage/-/blob/1.1.x/src/Entity/Stora...

This should probably include additional processing after token replacement similar to what Auto Entity Label performs. See Drupal\auto_entitylabel\AutoEntityLabelManager::generateLabel()
https://git.drupalcode.org/project/auto_entitylabel/-/blob/8.x-3.x/src/A...

(However, note that the Auto Entity Label approach doesn't seem to be doing any max-length truncation. That may be getting applied elsewhere when setting the label field.)

I'm kind of surprised there isn't a generic Drupal service that sanitizes inputs for standard Drupal entity labels. If there is, that might be even cleaner to implement.

(If anyone else runs into this issue, you can still use Auto Entity Label to override the Storage Entity automatic name field generation to avoid the issues outlined above while waiting for a fix to this module.)

CommentFileSizeAuthor
#2 3276657-truncate-clear.patch1.01 KBmxh

Comments

DaleTrexel created an issue. See original summary.

mxh’s picture

Version: 1.2.0 » 1.1.x-dev
Status: Active » Needs review
StatusFileSize
new1.01 KB

Please try whether the attached patch fixes the length problem.

mxh’s picture

Title: auto-generated names should be sanitized » Name pattern is not properly respecting maximum allowed length of the Storage name field

This issue won't get fixed unless someone (not me) manually reviews the patch.

  • mxh committed c01749b on 1.1.x
    Issue #3276657 by mxh, DaleTrexel: Name pattern is not properly...
mxh’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

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