validateMachineName() throws uncaught exception when machine_name contains non-ASCII characters

Created on 19 September 2024, 4 months ago

Problem/Motivation

Drupal\Core\Render\Element\MachineName::validateMachineName() throws uncaught exception when machine_name contains non-ASCII characters.

validateMachineName() doesn't handle errors well:
- !isset($element['#machine_name']['error']) is always TRUE
- even if validation fails, function will lookup machine name in the database

Steps to reproduce

- navigate to /admin/structure/types/add, update machine name to something non-ASCII, save
on any machine_name form element, type some non-ASCII characters, validate form (click Save), get white screen of death and exception in the logs:

Drupal\Core\Database\DatabaseExceptionWrapper: SQLSTATE[HY000]: General error: 1267 Illegal mix of collations (ascii_general_ci,IMPLICIT) and (utf8mb4_0900_ai_ci,COERCIBLE) for operation '=': SELECT "name", "data" FROM "config" WHERE "collection" = :collection AND "name" IN ( :names__0 ); Array ( [:collection] => [:names__0] => node.type.тест ) in Drupal\Core\Config\DatabaseStorage->readMultiple() (line 114 of /Users/jannakha/work/d11.local/web/core/lib/Drupal/Core/Config/DatabaseStorage.php).

Stack trace:

#0 /web/core/lib/Drupal/Core/Database/Connection.php(661): Drupal\mysql\Driver\Database\mysql\ExceptionHandler->handleExecutionException(Object(PDOException), Object(Drupal\Core\Database\StatementWrapperIterator), Array, Array)
#1 /web/core/lib/Drupal/Core/Config/DatabaseStorage.php(114): Drupal\Core\Database\Connection->query('SELECT [name], ...', Array, Array)
#2 /web/core/lib/Drupal/Core/Config/CachedStorage.php(95): Drupal\Core\Config\DatabaseStorage->readMultiple(Array)
#3 /web/core/lib/Drupal/Core/Config/ConfigFactory.php(165): Drupal\Core\Config\CachedStorage->readMultiple(Array)
#4 /web/core/lib/Drupal/Core/Config/ConfigFactory.php(136): Drupal\Core\Config\ConfigFactory->doLoadMultiple(Array)
#5 /web/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php(163): Drupal\Core\Config\ConfigFactory->loadMultiple(Array)
#6 /web/core/lib/Drupal/Core/Entity/EntityStorageBase.php(312): Drupal\Core\Config\Entity\ConfigEntityStorage->doLoadMultiple(Array)
#7 /web/core/lib/Drupal/Core/Entity/EntityStorageBase.php(262): Drupal\Core\Entity\EntityStorageBase->loadMultiple(Array)
#8 /web/core/lib/Drupal/Core/Entity/EntityBase.php(503): Drupal\Core\Entity\EntityStorageBase->load('\xD1\x82\xD0\xB5\xD1\x81\xD1\x82')
#9 [internal function]: Drupal\Core\Entity\EntityBase::load('\xD1\x82\xD0\xB5\xD1\x81\xD1\x82', Array, Object(Drupal\Core\Form\FormState))
#10 /web/core/lib/Drupal/Core/Render/Element/MachineName.php(276): call_user_func(Array, '\xD1\x82\xD0\xB5\xD1\x81\xD1\x82', Array, Object(Drupal\Core\Form\FormState))
#11 [internal function]: Drupal\Core\Render\Element\MachineName::validateMachineName(Array, Object(Drupal\Core\Form\FormState), Array)
#12 /web/core/lib/Drupal/Core/Form/FormValidator.php(281): call_user_func_array(Array, Array)
#13 /web/core/lib/Drupal/Core/Form/FormValidator.php(238): Drupal\Core\Form\FormValidator->doValidateForm(Array, Object(Drupal\Core\Form\FormState))

Proposed resolution

if validation of machine_name fails, don't query database

Remaining tasks

- patch
- testing

User interface changes

- none

Introduced terminology

- none

API changes

- none

Data model changes

- none

Release notes snippet

- validateMachineName() throws uncaught exception when machine_name contains non-ASCII characters

🐛 Bug report
Status

Needs review

Version

11.0 🔥

Component
Render 

Last updated 3 days ago

Created by

🇦🇺Australia jannakha Brisbane!

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024