TranslatableMarkup passed to t() function when config property has no label set.

Created on 14 June 2023, over 1 year ago
Updated 11 July 2023, over 1 year ago

Problem/Motivation

Error on config translation page, when there is no label set for the config property in the schema definition.

Steps to reproduce

enable config translation
Provide a form_element_class for the boolean type using hook_config_schema_info_alter():

function my_module_config_schema_info_alter(&$definitions) {
  $map = [
    'boolean' => Drupal\config_translation\FormElement\Textfield::class,
  ];

  // Add a form element for some field types in config schema, that are not
  // available in core, or that we want to extend.
  foreach ($definitions as $type => &$definition) {
    if (isset($map[$type]) && !isset($definition['form_element_class'])) {
      $definition['form_element_class'] = $map[$type];
    }
  }
}

Visit the translation page for a menu
--> following error message appears:

InvalidArgumentException: $string ("n/a") must be a string. in Drupal\Core\StringTranslation\TranslatableMarkup->__construct() (line 132 of core/lib/Drupal/Core/StringTranslation/TranslatableMarkup.php).

The path to follow:
The "locked" property has no label set in the schema:
https://git.drupalcode.org/project/drupal/-/blob/9.5.x/core/modules/syst...

system.menu.*:
  type: config_entity
  label: 'Menu'
  mapping:
    id:
      type: string
      label: 'ID'
    label:
      type: label
      label: 'Label'
    description:
      type: label
      label: 'Menu description'
    locked:
      type: boolean
      label: ''

ConfigTranslationFormBase::createFormElement checks if there is no label set and sets the label as a translation of n/a:
https://git.drupalcode.org/project/drupal/-/blob/10.1.x/core/modules/con...

      if (!$definition->getLabel()) {
        $definition->setLabel(new TranslatableMarkup('n/a'));
      }

FormElementBase gets that label and tries to translate again:
https://git.drupalcode.org/project/drupal/-/blob/10.1.x/core/modules/con...

'@label' => $this->t($this->definition->getLabel()),

Proposed resolution

We either should set a label to the config schema for the locked property.
Or should set a string only version of n/a as label for definitions without label.
Or check if the label is already a translation, so do not try to translate again. Since the DataDefinitionInterface (https://git.drupalcode.org/project/drupal/-/blob/9.5.x/core/lib/Drupal/C...) defines that getLabel() might return a TranslatableMarkup.

🐛 Bug report
Status

Active

Version

9.5

Component
Typed data 

Last updated 3 days ago

  • Maintained by
  • 🇦🇹Austria @fago
Created by

🇩🇪Germany eyilmaz Berlin, Germany

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

Comments & Activities

Production build 0.71.5 2024