Ticket names with UTF-8 characters can sometimes cause SQL errors

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

Problem/Motivation

A ticket title (name) using UTF-8 characters and the string length truncated by the function substr can cause a SQL error like this one (the ticket title in this case is composed of a date and a longer string):

Drupal\Core\Entity\EntityStorageException: SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect string value: <strong>'\xC3' </strong>for column `qx_pay`.`commerce_ticket`.`name` at row 1: INSERT INTO "commerce_ticket" ("bundle", "uuid", "name", "ticket_number", "order_id", "order_item_id", "state", "data", "uid", "created", "changed") ...

In the case above, '\xC3' is the first part of a two-byte sequence representing a non-ASCII character. Here the whole character 'รก' would have been represented by '\xc3\xa1', but was ultimately truncated by the substr function.

The substr was introduced by patch #2 from this issue: https://www.drupal.org/project/commerce_ticketing/issues/3218870 โ†’ .

Remaining tasks

Since substr is not appropriate to deal with UTF-8 encoding, it should be replaced with mb_strcut.

So this line of code in /commerce_ticketing/src/CommerceTicketCreateTickets.php
'name' => substr($this->t('Ticket') . ' ' . $purchased_entity->label(), 0, 50),
should be replaced with this:
'name' => mb_strcut($this->t('Ticket') . ' ' . $purchased_entity->label(), 0, 50),

The above solution was tested in the same use case, and it's working.

๐Ÿ› Bug report
Status

Needs review

Version

2.0

Component

Code

Created by

๐Ÿ‡ฎ๐Ÿ‡ชIreland marksmith

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

Comments & Activities

Production build 0.71.5 2024