Skip to content

Commit 1e73d90

Browse files
EmmanuelVellaisometriks
authored andcommitted
Replace 'horizontal' option with 'layout' (#1044)
1 parent cbbd2d9 commit 1e73d90

File tree

9 files changed

+73
-33
lines changed

9 files changed

+73
-33
lines changed

DependencyInjection/Configuration.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public function getConfigTreeBuilder()
3535

3636
protected function addFormConfig(ArrayNodeDefinition $rootNode)
3737
{
38+
$layouts = array(false, 'horizontal', 'inline');
39+
3840
$rootNode
3941
->children()
4042
->arrayNode('form')
@@ -45,8 +47,10 @@ protected function addFormConfig(ArrayNodeDefinition $rootNode)
4547
->scalarNode('templating')
4648
->defaultValue("MopaBootstrapBundle:Form:fields.html.twig")
4749
->end()
48-
->booleanNode('horizontal')
49-
->defaultTrue()
50+
->enumNode('layout')
51+
->info('Default form layout')
52+
->values($layouts)
53+
->defaultValue('horizontal')
5054
->end()
5155
->scalarNode('horizontal_label_class')
5256
->defaultValue("col-sm-3")
@@ -219,6 +223,17 @@ protected function addFormConfig(ArrayNodeDefinition $rootNode)
219223
->end()
220224
->end()
221225
->end()
226+
->beforeNormalization()
227+
->ifTrue(function ($v) {
228+
return isset($v['horizontal']);
229+
})
230+
->then(function ($v) {
231+
$v['layout'] = $v['horizontal'] ? 'horizontal' : false;
232+
unset($v['horizontal']);
233+
234+
return $v;
235+
})
236+
->end()
222237
->end()
223238
->end();
224239
}

Form/Extension/HorizontalFormTypeExtension.php renamed to Form/Extension/LayoutFormTypeExtension.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@
1414
use Symfony\Component\Form\AbstractTypeExtension;
1515
use Symfony\Component\Form\FormInterface;
1616
use Symfony\Component\Form\FormView;
17+
use Symfony\Component\OptionsResolver\Options;
1718
use Symfony\Component\OptionsResolver\OptionsResolver;
1819
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
1920

2021
/**
21-
* Extension for enabling Horizontal Forms.
22+
* Extension to customize forms layout.
2223
*
2324
* @author phiamo <phiamo@googlemail.com>
2425
*/
25-
class HorizontalFormTypeExtension extends AbstractTypeExtension
26+
class LayoutFormTypeExtension extends AbstractTypeExtension
2627
{
2728
/**
2829
* @var array
@@ -44,18 +45,19 @@ public function __construct(array $options)
4445
*/
4546
public function buildView(FormView $view, FormInterface $form, array $options)
4647
{
47-
$horizontal = $options['horizontal'];
48+
$layout = $options['layout'];
4849

49-
if ($horizontal === null) {
50+
if ($layout === null) {
5051
if ($view->parent) {
51-
$horizontal = $view->parent->vars['horizontal'];
52+
$layout = $view->parent->vars['layout'];
5253
} else {
53-
$horizontal = $this->options['horizontal'];
54+
$layout = $this->options['layout'];
5455
}
5556
}
5657

5758
$view->vars = array_replace($view->vars, array(
58-
'horizontal' => $horizontal,
59+
'layout' => $layout,
60+
'horizontal' => 'horizontal' === $layout, // BC
5961
'horizontal_label_class' => $options['horizontal_label_class'],
6062
'horizontal_label_offset_class' => $options['horizontal_label_offset_class'],
6163
'horizontal_input_wrapper_class' => $options['horizontal_input_wrapper_class'],
@@ -65,9 +67,9 @@ public function buildView(FormView $view, FormInterface $form, array $options)
6567

6668
public function finishView(FormView $view, FormInterface $form, array $options)
6769
{
68-
if (!$view->parent && $options['compound'] && $view->vars['horizontal']) {
70+
if (!$view->parent && $options['compound'] && $view->vars['layout']) {
6971
$class = isset($view->vars['attr']['class']) ? $view->vars['attr']['class'].' ' : '';
70-
$view->vars['attr']['class'] = $class.'form-horizontal';
72+
$view->vars['attr']['class'] = $class.'form-'.$view->vars['layout'];
7173
}
7274
}
7375

@@ -87,12 +89,28 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
8789
public function configureOptions(OptionsResolver $resolver)
8890
{
8991
$resolver->setDefaults(array(
92+
'layout' => function (Options $options) {
93+
// BC
94+
if (isset($options['horizontal']) && false === $options['horizontal']) {
95+
return false;
96+
}
97+
98+
return null;
99+
},
90100
'horizontal' => null,
91101
'horizontal_label_class' => $this->options['horizontal_label_class'],
92102
'horizontal_label_offset_class' => $this->options['horizontal_label_offset_class'],
93103
'horizontal_input_wrapper_class' => $this->options['horizontal_input_wrapper_class'],
94104
'horizontal_label_div_class' => $this->options['horizontal_label_div_class'],
95105
));
106+
107+
$allowedValues = array(false, null, 'horizontal', 'inline');
108+
109+
if (method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')) {
110+
$resolver->setAllowedValues('layout', $allowedValues);
111+
} else {
112+
$resolver->setAllowedValues(array('layout' => $allowedValues)); // SF <2.8 BC
113+
}
96114
}
97115

98116
/**

Resources/config/form.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<parameter key="mopa_bootstrap.form.type_extension.legend.class">Mopa\Bundle\BootstrapBundle\Form\Extension\LegendFormTypeExtension</parameter>
1414
<parameter key="mopa_bootstrap.form.type_extension.error.class">Mopa\Bundle\BootstrapBundle\Form\Extension\ErrorTypeFormTypeExtension</parameter>
1515
<parameter key="mopa_bootstrap.form.type_extension.widget.class">Mopa\Bundle\BootstrapBundle\Form\Extension\WidgetFormTypeExtension</parameter>
16-
<parameter key="mopa_bootstrap.form.type_extension.horizontal.class">Mopa\Bundle\BootstrapBundle\Form\Extension\HorizontalFormTypeExtension</parameter>
16+
<parameter key="mopa_bootstrap.form.type_extension.layout.class">Mopa\Bundle\BootstrapBundle\Form\Extension\LayoutFormTypeExtension</parameter>
1717
<parameter key="mopa_bootstrap.form.type_extension.widget_collection.class">Mopa\Bundle\BootstrapBundle\Form\Extension\WidgetCollectionFormTypeExtension</parameter>
1818
<parameter key="mopa_bootstrap.form.type_extension.date.class">Mopa\Bundle\BootstrapBundle\Form\Extension\DateTypeExtension</parameter>
1919
<parameter key="mopa_bootstrap.form.type_extension.datetime.class">Mopa\Bundle\BootstrapBundle\Form\Extension\DatetimeTypeExtension</parameter>
@@ -89,9 +89,9 @@
8989
<tag name="form.type_extension" alias="form" extended-type="Symfony\Component\Form\Extension\Core\Type\FormType" />
9090
</service>
9191

92-
<service id="mopa_bootstrap.form.type_extension.horizontal" class="%mopa_bootstrap.form.type_extension.horizontal.class%">
92+
<service id="mopa_bootstrap.form.type_extension.horizontal" class="%mopa_bootstrap.form.type_extension.layout.class%">
9393
<argument type="collection">
94-
<argument key="horizontal">%mopa_bootstrap.form.horizontal%</argument>
94+
<argument key="layout">%mopa_bootstrap.form.layout%</argument>
9595
<argument key="horizontal_label_class">%mopa_bootstrap.form.horizontal_label_class%</argument>
9696
<argument key="horizontal_label_div_class">%mopa_bootstrap.form.horizontal_label_div_class%</argument>
9797
<argument key="horizontal_label_offset_class">%mopa_bootstrap.form.horizontal_label_offset_class%</argument>

Resources/doc/misc/configuration-reference.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ mopa_bootstrap:
88
form:
99
allow_legacy: false
1010
templating: MopaBootstrapBundle:Form:fields.html.twig
11-
horizontal: true
11+
# Default form layout
12+
layout: ~ # One of false; "horizontal"
1213
horizontal_label_class: col-sm-3 control-label
1314
horizontal_label_div_class: null
1415
horizontal_label_offset_class: col-sm-offset-3

Resources/views/Form/fields.html.twig

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
</div>
3737
</div>
3838
{% else %}
39-
<div>
39+
<div class="form-group">
4040
{{ form_widget(form) }}
4141
</div>
4242
{% endif %}
@@ -204,6 +204,9 @@
204204
{% if expanded %}
205205
{% set attr = attr|merge({'class': attr.class|default('') ~ ' ' ~ horizontal_input_wrapper_class}) %}
206206
{% endif %}
207+
{% if layout is sameas(false) %}
208+
<div>
209+
{% endif %}
207210
{% if widget_type == 'inline-btn' %}
208211
{% set tagName = 'button' %}
209212
<div class="btn-group" data-toggle="buttons">
@@ -240,6 +243,9 @@
240243
{% if widget_type == 'inline-btn' %}
241244
</div>
242245
{% endif %}
246+
{% if layout is sameas(false) %}
247+
</div>
248+
{% endif %}
243249
{% endspaceless %}
244250
{% endblock choice_widget_expanded %}
245251

@@ -270,7 +276,7 @@
270276
{% endif %}
271277
{% endif %}
272278
<label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}
273-
{%- if not horizontal %} class="checkbox-inline"{% endif %}>
279+
{%- if layout == 'inline' %} class="checkbox-inline"{% endif %}>
274280
{% endif %}
275281
<input type="checkbox" {{ block('widget_attributes') }}{% if value is defined %} value="{{ value }}"{% endif %}{% if checked %} checked="checked"{% endif %}/>
276282
{% if form.parent != null and 'choice' not in form.parent.vars.block_prefixes %}
@@ -459,7 +465,7 @@
459465
{% set label_attr = label_attr|merge({'for': id}) %}
460466
{% endif %}
461467
{% set label_attr_class = '' %}
462-
{% if horizontal %}
468+
{% if layout == 'horizontal' %}
463469
{% set label_attr_class = 'control-label ' ~ label_attr_class ~ horizontal_label_class %}
464470
{% endif %}
465471
{% if horizontal_label_div_class %}
@@ -565,7 +571,7 @@
565571
{% else %}
566572
{{ block('widget_form_group_start') }}
567573

568-
{% if horizontal and not label_render %}
574+
{% if layout == 'horizontal' and not label_render %}
569575
{% set horizontal_input_wrapper_class = horizontal_input_wrapper_class ~ ' ' ~ horizontal_label_offset_class %}
570576
{% endif %}
571577

Tests/Form/AbstractDivLayoutTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Mopa\Bundle\BootstrapBundle\Form\Extension\EmbedFormExtension;
66
use Mopa\Bundle\BootstrapBundle\Form\Extension\ErrorTypeFormTypeExtension;
77
use Mopa\Bundle\BootstrapBundle\Form\Extension\HelpFormTypeExtension;
8-
use Mopa\Bundle\BootstrapBundle\Form\Extension\HorizontalFormTypeExtension;
8+
use Mopa\Bundle\BootstrapBundle\Form\Extension\LayoutFormTypeExtension;
99
use Mopa\Bundle\BootstrapBundle\Form\Extension\LegendFormTypeExtension;
1010
use Mopa\Bundle\BootstrapBundle\Form\Extension\StaticTextExtension;
1111
use Mopa\Bundle\BootstrapBundle\Form\Extension\TabbedFormTypeExtension;
@@ -105,7 +105,7 @@ protected function getExtensions()
105105
$this->getHelpFormTypeExtension(),
106106
$this->getWidgetFormTypeExtension(),
107107
$this->getLegendFormTypeExtension(),
108-
$this->getHorizontalFormTypeExtension(),
108+
$this->getLayoutFormTypeExtension(),
109109
$this->getErrorTypeFormTypeExtension(),
110110
$this->getEmbedFormExtension(),
111111
$this->getTabbedFormTypeExtension(),
@@ -175,12 +175,12 @@ protected function getLegendFormTypeExtension()
175175
}
176176

177177
/**
178-
* @return HorizontalFormTypeExtension
178+
* @return LayoutFormTypeExtension
179179
*/
180-
protected function getHorizontalFormTypeExtension()
180+
protected function getLayoutFormTypeExtension()
181181
{
182-
return new HorizontalFormTypeExtension(array(
183-
'horizontal' => true,
182+
return new LayoutFormTypeExtension(array(
183+
'layout' => 'horizontal',
184184
'horizontal_label_class' => 'col-sm-3',
185185
'horizontal_label_div_class' => null,
186186
'horizontal_label_offset_class' => 'col-sm-offset-3',

Tests/Form/CollectionLayoutTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ public function testChildrenHorizontal()
176176
))
177177
->add('names', $this->getFormType('collection'), array(
178178
$this->getCollectionTypeKey() => $this->getFormType('text'),
179-
$this->getCollectionOptionsKey() => array('horizontal' => true),
180-
'horizontal' => false,
179+
$this->getCollectionOptionsKey() => array('layout' => 'horizontal'),
180+
'layout' => false,
181181
))
182182
->getForm()
183183
->createView()
@@ -261,7 +261,7 @@ public function testAllNotHorizontal()
261261
))
262262
->add('names', $this->getFormType('collection'), array(
263263
$this->getCollectionTypeKey() => $this->getFormType('text'),
264-
'horizontal' => false,
264+
'layout' => false,
265265
))
266266
->getForm()
267267
->createView()
@@ -324,4 +324,4 @@ public function testAllNotHorizontal()
324324
'
325325
);
326326
}
327-
}
327+
}

Tests/Form/SimpleDivLayoutTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public function testHorizontalRow()
88
{
99
$view = $this->factory
1010
->createNamed('name', $this->getFormType('email'), null, array(
11-
'horizontal' => true,
11+
'layout' => 'horizontal',
1212
))
1313
->createView()
1414
;

Tests/Form/TypeTestCase.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,10 @@ protected function getTypeExtensions()
108108
'help_widget_popover' => $this->container->getParameter('mopa_bootstrap.form.help_widget.popover')
109109
]
110110
),
111-
new MopaExtensions\HorizontalFormTypeExtension(
111+
new MopaExtensions\LayoutFormTypeExtension(
112112
[
113-
'horizontal' => $this->container->getParameter(
114-
'mopa_bootstrap.form.horizontal'
113+
'layout' => $this->container->getParameter(
114+
'mopa_bootstrap.form.layout'
115115
),
116116
'horizontal_label_class' => $this->container->getParameter(
117117
'mopa_bootstrap.form.horizontal_label_class'

0 commit comments

Comments
 (0)