Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
How to reproduce it:
- Enable the config_translation
- Add a new language to the site
- Create a list field with at least 3 options and set cardinality to 2 on any content type
- Use the checkboxes widget for that field
- Translate the field label
- Translate the core validation contraint '%name: this field cannot hold more than @count values.'
- Submit the node form of the content type with 3 options
The resulting error
The problem is apparently caused due to a nested string translation object of the field label within the translation string object of the constraint itself. Here the details:
The website encountered an unexpected error. Please try again later.
User error: InvalidArgumentException thrown while calling __toString on a Drupal\Core\StringTranslation\TranslatableMarkup object in /core/lib/Drupal/Core/Database/Connection.php on line 719: Placeholders must have a trailing [] if they are to be expanded with an array of values. in Drupal\Core\StringTranslation\TranslatableMarkup->__toString() (line 25 of core/lib/Drupal/Component/Utility/ToStringTrait.php).
Drupal\Core\StringTranslation\TranslatableMarkup->__toString()
drupal_set_message(Object, 'error', )
Drupal\Core\Form\FormErrorHandler->drupalSetMessage(Object, 'error')
Drupal\Core\Form\FormErrorHandler->displayErrorMessages(Array, Object)
Drupal\Core\Form\FormErrorHandler->handleFormErrors(Array, Object)
Drupal\Core\Form\FormValidator->finalizeValidation(Array, Object, 'user_form')
Drupal\Core\Form\FormValidator->validateForm('user_form', Array, Object)
Drupal\Core\Form\FormBuilder->processForm('user_form', Array, Object)
Drupal\Core\Form\FormBuilder->buildForm(Object, Object)
Drupal\Core\Controller\FormController->getContentResult(Object, Object)
call_user_func_array(Array, Array)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
call_user_func_array(Object, Array)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1)
Stack\StackedHttpKernel->handle(Object, 1, 1)
Drupal\Core\DrupalKernel->handle(Object)
The expected result
The validation constraint should be displayed translated (including the field's label)
Proposed resolution
Case the maxMessage to string in FieldItemList::getConstraints(). A UI test for this wouldn't hurt either.
Comments
Comment #2
s_leu CreditAttribution: s_leu at MD Systems GmbH commentedSeems to be a duplicate of #2570285: Make sure TranslatableMarkup accepts string values only
Comment #3
alexpottWe're going to put a fix in before RC. This is critical bug because it WSOD's and presents the user with no chance of recovery.
Comment #4
alexpottDiscussed some more with @catch we decided to go for the full fix in #2570285: Make sure TranslatableMarkup accepts string values only