Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Form] A programmatic way to get the label of a field #46636

Open
derrabus opened this issue Jun 10, 2022 · 3 comments
Open

[Form] A programmatic way to get the label of a field #46636

derrabus opened this issue Jun 10, 2022 · 3 comments

Comments

@derrabus
Copy link
Member

Description

I need a way to get the human-readable label of a form field without rendering the whole form. Unfortunately, the logic that I need is currently implemented in Twig templates:

{%- block form_label_content -%}
{%- if label is empty -%}
{%- if label_format is not empty -%}
{% set label = label_format|replace({
'%name%': name,
'%id%': id,
}) %}
{%- else -%}
{% set label = name|humanize %}
{%- endif -%}
{%- endif -%}
{%- if translation_domain is same as(false) -%}
{%- if label_html is same as(false) -%}
{{- label -}}
{%- else -%}
{{- label|raw -}}
{%- endif -%}
{%- else -%}
{%- if label_html is same as(false) -%}
{{- label|trans(label_translation_parameters, translation_domain) -}}
{%- else -%}
{{- label|trans(label_translation_parameters, translation_domain)|raw -}}
{%- endif -%}
{%- endif -%}
{%- endblock form_label_content -%}

Do you think it's possible to extract it into a service to make it reusable outside of the rendering process?

Example

No response

@carsonbot carsonbot added the Form label Jun 10, 2022
@javiereguiluz
Copy link
Member

Symfony Forms have lots of logic in the templates. Maybe we can take a look if it's possible to move most of that logic, not only the field labels, back to PHP?

@javiereguiluz javiereguiluz added this to the 6.2 milestone Jun 10, 2022
@HeahDude
Copy link
Contributor

What about https://github.com/symfony/symfony/blob/6.2/src/Symfony/Bridge/Twig/Extension/FormExtension.php#L73?
It may only miss an update for handling label_html 🤔.

@stof
Copy link
Member

stof commented Jun 10, 2022

The handling of label_html is purely a template thing though, as it is only about choosing whether the template should apply escaping or no.

@xabbuh xabbuh removed this from the 6.2 milestone Nov 20, 2022
@xabbuh xabbuh added the Feature label Nov 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants