Skip to content

Use option labels instead of values in summary tokens#101

Open
gmpf wants to merge 1 commit into
terminal42:mainfrom
gmpf:opt-labels
Open

Use option labels instead of values in summary tokens#101
gmpf wants to merge 1 commit into
terminal42:mainfrom
gmpf:opt-labels

Conversation

@gmpf

@gmpf gmpf commented May 23, 2026

Copy link
Copy Markdown

At the moment, the summary tokens for any field type that uses the options field (natively: select, radio, checkbox) display the values of those options, not the labels. E. g., if you have a checkbox field with name "accessibility", label "Barrierefreit", and the following options:

value label
seniorFriendly altersgerechte Wohnung
elevator Personenaufzug

and you select those options in the form, then the output in the summary will be something like:

Barrierefreit
seniorFriendly, elevator

This is technically consistent with other fields, because what is shown is always the field value, but I can't think of a use case in which someone would actually want the output to be like this for options fields. It's quite the opposite: The question of how one can display the option labels instead has been asked many times for many years. The user can technically work around the problem field by field, but it cannot be solved when the token ##mp_forms_summary## is used.

This PR makes it so that, for fields that use options, the option labels are assembled and used as the token values.

I was initially concerned about multiple form fields that have the same name, but this is actually irrelevant because the Core only outputs the last one of these fields anyway. So all we have to do is make sure we fetch the last field as well so that the option labels match those shown in the form.

I'm also a little concerned about having placed the new getOptionLabels() method in the Widget rather than somewhere else. It's really a much more general method than just something for this context, but an obvious better place within this bundle didn't stand out to me.

@Toflar Toflar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for trying to fix this issue. I agree that we should try to improve the token values out-of-the box. However, unfortunately your logic falls short with for example missing support for option_callbacks. Can you try to use the Haste Formatter for that? https://github.com/codefog/contao-haste/blob/main/docs/Formatter.md

return $formFieldValue;
}

$field = FormFieldModel::findBy(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be grouped into one query for all fields instead of potentially hundreds of queries for big forms. Should be able to use findPublishedByPid() maybe?

}

$fft = FormFieldModel::getTable();
$palettesWithOpts = preg_grep('/([,;]|^)options([,;]|$)/', array_filter($GLOBALS['TL_DCA'][$fft]['palettes'], 'is_string'));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work for widgets not using the options field. I don't see how this guard would make sense, maybe you can elaborate?

$formFieldOptLabels = \is_array($formFieldValue)
? implode(', ', array_intersect_key($optsAssoc, array_flip($formFieldValue)))
: $optsAssoc[$formFieldValue];
$formFieldOptLabels = System::getContainer()->get('contao.insert_tag.parser')->replace($formFieldOptLabels);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If insert tags are supported we should support them in the haste formatter as well - that would be the place to add that then.

@gmpf

gmpf commented May 27, 2026

Copy link
Copy Markdown
Author

Thank you for the very quick feedback!

Thanks for trying to fix this issue. I agree that we should try to improve the token values out-of-the box. However, unfortunately your logic falls short with for example missing support for option_callbacks.

Can you elaborate on that? How does options_callback enter the picture in the context of a Form Generator Widget? The Core never calls it when rendering a Form Generator form. And, to my understanding, mp_forms doesn't function in a non Form Generator context, so what is the use case here?

Parts of your review might to be downstream from a non Form Generator viewpoint as well, so I'd like to understand this first before trying to work on that.

@Toflar

Toflar commented May 27, 2026

Copy link
Copy Markdown
Member

I thought the whole point of your PR is to show the option labels instead of the option raw values in a selection? Which should work in general. For all widgets.

@gmpf

gmpf commented May 27, 2026

Copy link
Copy Markdown
Author

No, this PR has nothing to do with the options that are rendered in the actual form field widgets (input/select) themselves. There's no problem there that I know of. This PR is referring to the summary tokens in the Placeholder widget.

@Toflar

Toflar commented May 27, 2026

Copy link
Copy Markdown
Member

Yes, and the summary token should take the values from the form field options? (which can be provided via options_callback in case you did not specify the options yourself)?

@fritzmg

fritzmg commented May 27, 2026

Copy link
Copy Markdown
Contributor

which can be provided via options_callback in case you did not specify the options yourself

You cannot provide form field options via the options_callback - that's only for select/checkbox fields in the DCA, not for the form generator widgets.

@gmpf

gmpf commented May 27, 2026

Copy link
Copy Markdown
Author

@fritzmg Thank you, I didn't know what else to say other than, "uh, misunderstanding of unclear provenance?!" 😅

@Toflar You might be thinking of modifying the value of the options field via a compileFormFields listener. That should not be a problem because the field models will already be attached to the Registry at this point. I'm not sure if I checked this though – will look into that.

@fritzmg

fritzmg commented May 27, 2026

Copy link
Copy Markdown
Contributor

(awkwardly) modifying the options via the hook shouldn't make a difference 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants