Make it easier for config target to treat empty strings as null

Created on 16 August 2024, 5 months ago

Problem/Motivation

With config validation we can declare a config key to be nullable. When submitting a config form with an empty string in a textfield, or an empty select element, the config is saved as an empty string. It is possible to use a callback (as below) to convert empty strings to null, but it's a bit awkward.

$form['foo'] = [
  '#title' => $this->t('Foo'),
  '#type' => 'textfield',
  '#config_target' => new ConfigTarget(
    'my_module.config,
    'foo',
    toConfig: static fn (string $value) => $value ?: NULL,
  ),
];

Steps to reproduce

Proposed resolution

Provide a way for ConfigTarget to treat empty values as null. Maybe it's another param in the constructor, or maybe a fluent setter, like new ConfigTarget('my_module.config', 'foo')->convertEmptyToNull()

Remaining tasks

Figure out the best DX
Do it

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

✨ Feature request
Status

Active

Version

11.0 πŸ”₯

Component
ConfigurationΒ  β†’

Last updated about 17 hours ago

Created by

πŸ‡¦πŸ‡ΊAustralia mstrelan

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

Comments & Activities

  • Issue created by @mstrelan
  • πŸ‡¬πŸ‡§United Kingdom longwave UK

    Not sure if it was in an issue or in Slack but somewhere @alexpott wondered whether NULL is really the correct thing to do for config string values and whether we should just use empty string instead to avoid having to handle conversion from UI input.

Production build 0.71.5 2024