8000 feature #45985 [TwigBridge] Add form_label_content and form_help_cont… · symfony/symfony@d096349 · GitHub
[go: up one dir, main page]

Skip to content

Commit d096349

Browse files
committed
feature #45985 [TwigBridge] Add form_label_content and form_help_content block to form_div_layout (alexander-schranz)
This PR was squashed before being merged into the 6.2 branch. Discussion ---------- [TwigBridge] Add form_label_content and form_help_content block to form_div_layout | Q | A | ------------- | --- | Branch? | 6.1 | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> Add form_label_text and form_help_text block. When it is required to override the `form_label` block a lot uf logic need to be duplicated which is just the translation logic of the label. To make the things easier e.g. when creating a `legend` label the new `form_label_content` block can be used e.g.: ```twig {%- block _custom_label -%} <legend> {{- block('form_label_content') -}} </legend> {%- endblock -%} ``` The same I added for the `form_help` which also has some translation logic in it: ```twig {%- block _custom_help -%} <div class="custom-help"> {{- block('form_help_content') -}} </div> {%- endblock -%} ``` Already the duplication of the logic in the `bootstrap_4_layout` theme can be avoided this way. With the naming I was not sure first I had `form_label_inner` but thought that maybe `form_label_content` would better represent it. Commits ------- a40dbea [TwigBridge] Add form_label_content and form_help_content block to form_div_layout
2 parents f3a9ac1 + a40dbea commit d096349

File tree

3 files changed

+52
-95
lines changed

3 files changed

+52
-95
lines changed

src/Symfony/Bridge/Twig/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.2
5+
---
6+
7+
* Add `form_label_content` and `form_help_content` block to form themes
8+
49
6.1
510
---
611

src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_4_layout.html.twig

Lines changed: 3 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -233,30 +233,8 @@
233233
{% if required -%}
234234
{% set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' required')|trim}) %}
235235
{%- endif -%}
236-
{% if label is empty -%}
237-
{%- if label_format is not empty -%}
238-
{% set label = label_format|replace({
239-
'%name%': name,
240-
'%id%': id,
241-
}) %}
242-
{%- else -%}
243-
{% set label = name|humanize %}
244-
{%- endif -%}
245-
{%- endif -%}
246236
<{{ element|default('label') }}{% if label_attr %}{% with { attr: label_attr } %}{{ block('attributes') }}{% endwith %}{% endif %}>
247-
{%- if translation_domain is same as(false) -%}
248-
{%- if label_html is same as(false) -%}
249-
{{- label -}}
250-
{%- else -%}
251-
{{- label|raw -}}
252-
{%- endif -%}
253-
{%- else -%}
254-
{%- if label_html is same as(false) -%}
255-
{{- label|trans(label_translation_parameters, translation_domain) -}}
256-
{%- else -%}
257-
{{- label|trans(label_translation_parameters, translation_domain)|raw -}}
258-
{%- endif -%}
259-
{%- endif -%}
237+
{{- block('form_label_content') -}}
260238
{% block form_label_errors %}{{- form_errors(form) -}}{% endblock form_label_errors %}</{{ element|default('label') }}>
261239
{%- else -%}
262240
{%- if errors|length > 0 -%}
@@ -283,33 +261,11 @@
283261
{%- if required -%}
284262
{%- set label_attr = label_attr|merge({class: (label_attr.class|default('') ~ ' required')|trim}) -%}
285263
{%- endif -%}
286-
{%- if label is not same as(false) and label is empty -%}
287-
{%- if label_format is not empty -%}
288-
{%- set label = label_format|replace({
289-
'%name%': name,
290-
'%id%': id,
291-
}) -%}
292-
{%- else -%}
293-
{%- set label = name|humanize -%}
294-
{%- endif -%}
295-
{%- endif -%}
296264

297265
{{ widget|raw }}
298266
<label{% with { attr: label_attr } %}{{ block('attributes') }}{% endwith %}>
299267
{%- if label is not same as(false) -%}
300-
{%- if translation_domain is same as(false) -%}
301-
{%- if label_html is same as(false) -%}
302-
{{- label -}}
303-
{%- else -%}
304-
{{- label|raw -}}
305-
{%- endif -%}
306-
{%- else -%}
307-
{%- if label_html is same as(false) -%}
308-
{{- label|trans(label_translation_parameters, translation_domain) -}}
309-
{%- else -%}
310-
{{- label|trans(label_translation_parameters, translation_domain)|raw -}}
311-
{%- endif -%}
312-
{%- endif -%}
268+
{{- block('form_label_content') -}}
313269
{%- endif -%}
314270
{{- form_errors(form) -}}
315271
</label>
@@ -353,19 +309,7 @@
353309
{%- if help is not empty -%}
354310
{%- set help_attr = help_attr|merge({class: (help_attr.class|default('') ~ ' form-text text-muted')|trim}) -%}
355311
<small id="{{ id }}_help"{% with { attr: help_attr } %}{{ block('attributes') }}{% endwith %}>
356-
{%- if translation_domain is same as(false) -%}
357-
{%- if help_html is same as(false) -%}
358-
{{- help -}}
359-
{%- else -%}
360-
{{- help|raw -}}
361-
{%- endif -%}
362-
{%- else -%}
363-
{%- if help_html is same as(false) -%}
364-
{{- help|trans(help_translation_parameters, translation_domain) -}}
365-
{%- else -%}
366-
{{- help|trans(help_translation_parameters, translation_domain)|raw -}}
367-
{%- endif -%}
368-
F438 {%- endif -%}
312+
{{- block('form_help_content') -}}
369313
</small>
370314
{%- endif -%}
371315
{%- endblock form_help %}

src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -290,34 +290,38 @@
290290
{% if required -%}
291291
{% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %}
292292
{%- endif -%}
293-
{% if label is empty -%}
294-
{%- if label_format is not empty -%}
295-
{% set label = label_format|replace({
296-
'%name%': name,
297-
'%id%': id,
298-
}) %}
299-
{%- else -%}
300-
{% set label = name|humanize %}
301-
{%- endif -%}
302-
{%- endif -%}
303293
<{{ element|default('label') }}{% if label_attr %}{% with { attr: label_attr } %}{{ block('attributes') }}{% endwith %}{% endif %}>
304-
{%- if translation_domain is same as(false) -%}
305-
{%- if label_html is same as(false) -%}
306-
{{- label -}}
307-
{%- else -%}
308-
{{- label|raw -}}
309-
{%- endif -%}
310-
{%- else -%}
311-
{%- if label_html is same as(false) -%}
312-
{{- label|trans(label_translation_parameters, translation_domain) -}}
313-
{%- else -%}
314-
{{- label|trans(label_translation_parameters, translation_domain)|raw -}}
315-
{%- endif -%}
316-
{%- endif -%}
294+
{{- block('form_label_content') -}}
317295
</{{ element|default('label') }}>
318296
{%- endif -%}
319297
{%- endblock form_label -%}
320298

299+
{%- block form_label_content -%}
300+
{%- if label is empty -%}
301+
{%- if label_format is not empty -%}
302+
{% set label = label_format|replace({
303+
'%name%': name,
304+
'%id%': id,
305+
}) %}
306+
{%- else -%}
307+
{% set label = name|humanize %}
308+
{%- endif -%}
309+
{%- endif -%}
310+
{%- if translation_domain is same as(false) -%}
311+
{%- if label_html is same as(false) -%}
312+
{{- label -}}
313+
{%- else -%}
314+
{{- label|raw -}}
315+
{%- endif -%}
316+
{%- else -%}
317+
{%- if label_html is same as(false) -%}
318+
{{- label|trans(label_translation_parameters, translation_domain) -}}
319+
{%- else -%}
320+
{{- label|trans(label_translation_parameters, translation_domain)|raw -}}
321+
{%- endif -%}
322+
{%- endif -%}
323+
{%- endblock form_label_content -%}
324+
321325
{%- block button_label -%}{%- endblock -%}
322326

323327
{# Help #}
@@ -326,23 +330,27 @@
326330
{%- if help is not empty -%}
327331
{%- set help_attr = help_attr|merge({class: (help_attr.class|default('') ~ ' help-text')|trim}) -%}
328332
<div id="{{ id }}_help"{% with { attr: help_attr } %}{{ block('attributes') }}{% endwith %}>
329-
{%- if translation_domain is same as(false) -%}
330-
{%- if help_html is same as(false) -%}
331-
{{- help -}}
332-
{%- else -%}
333-
{{- help|raw -}}
334-
{%- endif -%}
335-
{%- else -%}
336-
{%- if help_html is same as(false) -%}
337-
{{- help|trans(help_translation_parameters, translation_domain) -}}
338-
{%- else -%}
339-
{{- help|trans(help_translation_parameters, translation_domain)|raw -}}
340-
{%- endif -%}
341-
{%- endif -%}
333+
{{- block('form_help_content') -}}
342334
</div>
343335
{%- endif -%}
344336
{%- endblock form_help %}
345337

338+
{% block form_help_content -%}
339+
{%- if translation_domain is same as(false) -%}
340+
{%- if help_html is same as(false) -%}
341+
{{- help -}}
342+
{%- else -%}
343+
{{- help|raw -}}
344+
{%- endif -%}
345+
{%- else -%}
346+
{%- if help_html is same as(false) -%}
347+
{{- help|trans(help_translation_parameters, translation_domain) -}}
348+
{%- else -%}
349+
{{- help|trans(help_translation_parameters, translation_domain)|raw -}}
350+
{%- endif -%}
351+
{%- endif -%}
352+
{%- endblock form_help_content %}
353+
346354
{# Rows #}
347355

348356
{%- block repeated_row -%}

0 commit comments

Comments
 (0)
0