Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1cb9afd

Browse filesBrowse files
committed
feature #20365 [TwigBridge] Handle form label attributes like others (ro0NL)
This PR was squashed before being merged into the 3.3-dev branch (closes #20365). Discussion ---------- [TwigBridge] Handle form label attributes like others | Q | A | | --- | --- | | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | | Deprecations? | no | | Tests pass? | yes | Fixed tickets | - | License | MIT | | Doc PR | - The HTML for rendering attributes is duplicated in multiple blocks, making it error prone/hard to maintain. Next, the label attributes followed a different approach. Imo. all should follow the same base rendering, showing the above is actually an issue. Commits ------- e317e0a [TwigBridge] Handle form label attributes like others
2 parents a4a4166 + e317e0a commit 1cb9afd
Copy full SHA for 1cb9afd

File tree

6 files changed

+20
-63
lines changed
Filter options

6 files changed

+20
-63
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig
+5-32Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
{{- block('choice_widget_options') -}}
8080
</optgroup>
8181
{%- else -%}
82-
<option value="{{ choice.value }}"{% if choice.attr %} {% set attr = choice.attr %}{{ block('attributes') }}{% endif %}{% if choice is selectedchoice(value) %} selected="selected"{% endif %}>{{ choice_translation_domain is same as(false) ? choice.label : choice.label|trans({}, choice_translation_domain) }}</option>
82+
<option value="{{ choice.value }}"{% if choice.attr %} {% with { attr: choice.attr } %}{{ block('attributes') }}{% endwith %}{% endif %}{% if choice is selectedchoice(value) %} selected="selected"{% endif %}>{{ choice_translation_domain is same as(false) ? choice.label : choice.label|trans({}, choice_translation_domain) }}</option>
8383
{%- endif -%}
8484
{% endfor %}
8585
{%- endblock choice_widget_options -%}
@@ -259,7 +259,7 @@
259259
{% set label = name|humanize %}
260260
{%- endif -%}
261261
{%- endif -%}
262-
<label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ translation_domain is same as(false) ? label : label|trans({}, translation_domain) }}</label>
262+
<label{% if label_attr %} {% with { attr: label_attr } %}{{ block('attributes') }}{% endwith %}{% endif %}>{{ translation_domain is same as(false) ? label : label|trans({}, translation_domain) }}</label>
263263
{%- endif -%}
264264
{%- endblock form_label -%}
265265

@@ -351,44 +351,17 @@
351351
id="{{ id }}" name="{{ full_name }}"
352352
{%- if disabled %} disabled="disabled"{% endif -%}
353353
{%- if required %} required="required"{% endif -%}
354-
{%- for attrname, attrvalue in attr -%}
355-
{{- " " -}}
356-
{%- if attrname in ['placeholder', 'title'] -%}
357-
{{- attrname }}="{{ translation_domain is same as(false) ? attrvalue : attrvalue|trans({}, translation_domain) }}"
358-
{%- elseif attrvalue is same as(true) -%}
359-
{{- attrname }}="{{ attrname }}"
360-
{%- elseif attrvalue is not same as(false) -%}
361-
{{- attrname }}="{{ attrvalue }}"
362-
{%- endif -%}
363-
{%- endfor -%}
354+
{{ block('attributes') }}
364355
{%- endblock widget_attributes -%}
365356

366357
{%- block widget_container_attributes -%}
367358
{%- if id is not empty %}id="{{ id }}"{% endif -%}
368-
{%- for attrname, attrvalue in attr -%}
369-
{{- " " -}}
370-
{%- if attrname in ['placeholder', 'title'] -%}
371-
{{- attrname }}="{{ translation_domain is same as(false) ? attrvalue : attrvalue|trans({}, translation_domain) }}"
372-
{%- elseif attrvalue is same as(true) -%}
373-
{{- attrname }}="{{ attrname }}"
374-
{%- elseif attrvalue is not same as(false) -%}
375-
{{- attrname }}="{{ attrvalue }}"
376-
{%- endif -%}
377-
{%- endfor -%}
359+
{{ block('attributes') }}
378360
{%- endblock widget_container_attributes -%}
379361

380362
{%- block button_attributes -%}
381363
id="{{ id }}" name="{{ full_name }}"{% if disabled %} disabled="disabled"{% endif -%}
382-
{%- for attrname, attrvalue in attr -%}
383-
{{- " " -}}
384-
{%- if attrname in ['placeholder', 'title'] -%}
385-
{{- attrname }}="{{ translation_domain is same as(false) ? attrvalue : attrvalue|trans({}, translation_domain) }}"
386-
{%- elseif attrvalue is same as(true) -%}
387-
{{- attrname }}="{{ attrname }}"
388-
{%- elseif attrvalue is not same as(false) -%}
389-
{{- attrname }}="{{ attrvalue }}"
390-
{%- endif -%}
391-
{%- endfor -%}
364+
{{ block('attributes') }}
392365
{%- endblock button_attributes -%}
393366

394367
{% block attributes -%}
+9-1Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
<?php echo $view['form']->block($form, 'widget_attributes') ?>
1+
<?php foreach ($attr as $k => $v): ?>
2+
<?php if ('placeholder' === $k || 'title' === $k): ?>
3+
<?php printf('%s="%s" ', $view->escape($k), $view->escape(false !== $translation_domain ? $view['translator']->trans($v, array(), $translation_domain) : $v)) ?>
4+
<?php elseif (true === $v): ?>
5+
<?php printf('%s="%s" ', $view->escape($k), $view->escape($k)) ?>
6+
<?php elseif (false !== $v): ?>
7+
<?php printf('%s="%s" ', $view->escape($k), $view->escape($v)) ?>
8+
<?php endif ?>
9+
<?php endforeach ?>
+2-10Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,2 @@
1-
id="<?php echo $view->escape($id) ?>" name="<?php echo $view->escape($full_name) ?>" <?php if ($disabled): ?>disabled="disabled" <?php endif ?>
2-
<?php foreach ($attr as $k => $v): ?>
3-
<?php if (in_array($k, array('placeholder', 'title'), true)): ?>
4-
<?php printf('%s="%s" ', $view->escape($k), $view->escape(false !== $translation_domain ? $view['translator']->trans($v, array(), $translation_domain) : $v)) ?>
5-
<?php elseif ($v === true): ?>
6-
<?php printf('%s="%s" ', $view->escape($k), $view->escape($k)) ?>
7-
<?php elseif ($v !== false): ?>
8-
<?php printf('%s="%s" ', $view->escape($k), $view->escape($v)) ?>
9-
<?php endif ?>
10-
<?php endforeach ?>
1+
id="<?php echo $view->escape($id) ?>" name="<?php echo $view->escape($full_name) ?>"<?php if ($disabled): ?> disabled="disabled"<?php endif ?>
2+
<?php echo $attr ? ' '.$view['form']->block($form, 'attributes') : '' ?>

‎src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/form_label.html.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/form_label.html.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
<?php if (!$label) { $label = isset($label_format)
55
? strtr($label_format, array('%name%' => $name, '%id%' => $id))
66
: $view['form']->humanize($name); } ?>
7-
<label <?php foreach ($label_attr as $k => $v) { printf('%s="%s" ', $view->escape($k), $view->escape($v)); } ?>><?php echo $view->escape(false !== $translation_domain ? $view['translator']->trans($label, array(), $translation_domain) : $label) ?></label>
7+
<label<?php if ($label_attr) { echo ' '.$view['form']->block($form, 'attributes', array('attr' => $label_attr)); } ?>><?php echo $view->escape(false !== $translation_domain ? $view['translator']->trans($label, array(), $translation_domain) : $label) ?></label>
88
<?php endif ?>
+1-9Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
11
id="<?php echo $view->escape($id) ?>" name="<?php echo $view->escape($full_name) ?>"<?php if ($disabled): ?> disabled="disabled"<?php endif ?>
22
<?php if ($required): ?> required="required"<?php endif ?>
3-
<?php foreach ($attr as $k => $v): ?>
4-
<?php if (in_array($k, array('placeholder', 'title'), true)): ?>
5-
<?php printf(' %s="%s"', $view->escape($k), $view->escape(false !== $translation_domain ? $view['translator']->trans($v, array(), $translation_domain) : $v)) ?>
6-
<?php elseif ($v === true): ?>
7-
<?php printf(' %s="%s"', $view->escape($k), $view->escape($k)) ?>
8-
<?php elseif ($v !== false): ?>
9-
<?php printf(' %s="%s"', $view->escape($k), $view->escape($v)) ?>
10-
<?php endif ?>
11-
<?php endforeach ?>
3+
<?php echo $attr ? ' '.$view['form']->block($form, 'attributes') : '' ?>
+2-10Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,2 @@
1-
<?php if (!empty($id)): ?>id="<?php echo $view->escape($id) ?>" <?php endif ?>
2-
<?php foreach ($attr as $k => $v): ?>
3-
<?php if (in_array($k, array('placeholder', 'title'), true)): ?>
4-
<?php printf('%s="%s" ', $view->escape($k), $view->escape(false !== $translation_domain ? $view['translator']->trans($v, array(), $translation_domain) : $v)) ?>
5-
<?php elseif ($v === true): ?>
6-
<?php printf('%s="%s" ', $view->escape($k), $view->escape($k)) ?>
7-
<?php elseif ($v !== false): ?>
8-
<?php printf('%s="%s" ', $view->escape($k), $view->escape($v)) ?>
9-
<?php endif ?>
10-
<?php endforeach ?>
1+
<?php if (!empty($id)): ?>id="<?php echo $view->escape($id) ?>"<?php endif ?>
2+
<?php echo $attr ? ' '.$view['form']->block($form, 'attributes') : '' ?>

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.