Skip to content

Commit

Permalink
Replace 'horizontal' option with 'layout' (#1044)
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmanuelVella authored and isometriks committed Aug 24, 2017
1 parent cbbd2d9 commit 1e73d90
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 33 deletions.
19 changes: 17 additions & 2 deletions DependencyInjection/Configuration.php
Expand Up @@ -35,6 +35,8 @@ public function getConfigTreeBuilder()

protected function addFormConfig(ArrayNodeDefinition $rootNode)
{
$layouts = array(false, 'horizontal', 'inline');

$rootNode
->children()
->arrayNode('form')
Expand All @@ -45,8 +47,10 @@ protected function addFormConfig(ArrayNodeDefinition $rootNode)
->scalarNode('templating')
->defaultValue("MopaBootstrapBundle:Form:fields.html.twig")
->end()
->booleanNode('horizontal')
->defaultTrue()
->enumNode('layout')
->info('Default form layout')
->values($layouts)
->defaultValue('horizontal')
->end()
->scalarNode('horizontal_label_class')
->defaultValue("col-sm-3")
Expand Down Expand Up @@ -219,6 +223,17 @@ protected function addFormConfig(ArrayNodeDefinition $rootNode)
->end()
->end()
->end()
->beforeNormalization()
->ifTrue(function ($v) {
return isset($v['horizontal']);
})
->then(function ($v) {
$v['layout'] = $v['horizontal'] ? 'horizontal' : false;
unset($v['horizontal']);

return $v;
})
->end()
->end()
->end();
}
Expand Down
Expand Up @@ -14,15 +14,16 @@
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

/**
* Extension for enabling Horizontal Forms.
* Extension to customize forms layout.
*
* @author phiamo <phiamo@googlemail.com>
*/
class HorizontalFormTypeExtension extends AbstractTypeExtension
class LayoutFormTypeExtension extends AbstractTypeExtension
{
/**
* @var array
Expand All @@ -44,18 +45,19 @@ public function __construct(array $options)
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
$horizontal = $options['horizontal'];
$layout = $options['layout'];

if ($horizontal === null) {
if ($layout === null) {
if ($view->parent) {
$horizontal = $view->parent->vars['horizontal'];
$layout = $view->parent->vars['layout'];
} else {
$horizontal = $this->options['horizontal'];
$layout = $this->options['layout'];
}
}

$view->vars = array_replace($view->vars, array(
'horizontal' => $horizontal,
'layout' => $layout,
'horizontal' => 'horizontal' === $layout, // BC
'horizontal_label_class' => $options['horizontal_label_class'],
'horizontal_label_offset_class' => $options['horizontal_label_offset_class'],
'horizontal_input_wrapper_class' => $options['horizontal_input_wrapper_class'],
Expand All @@ -65,9 +67,9 @@ public function buildView(FormView $view, FormInterface $form, array $options)

public function finishView(FormView $view, FormInterface $form, array $options)
{
if (!$view->parent && $options['compound'] && $view->vars['horizontal']) {
if (!$view->parent && $options['compound'] && $view->vars['layout']) {
$class = isset($view->vars['attr']['class']) ? $view->vars['attr']['class'].' ' : '';
$view->vars['attr']['class'] = $class.'form-horizontal';
$view->vars['attr']['class'] = $class.'form-'.$view->vars['layout'];
}
}

Expand All @@ -87,12 +89,28 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'layout' => function (Options $options) {
// BC
if (isset($options['horizontal']) && false === $options['horizontal']) {
return false;
}

return null;
},
'horizontal' => null,
'horizontal_label_class' => $this->options['horizontal_label_class'],
'horizontal_label_offset_class' => $this->options['horizontal_label_offset_class'],
'horizontal_input_wrapper_class' => $this->options['horizontal_input_wrapper_class'],
'horizontal_label_div_class' => $this->options['horizontal_label_div_class'],
));

$allowedValues = array(false, null, 'horizontal', 'inline');

if (method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')) {
$resolver->setAllowedValues('layout', $allowedValues);
} else {
$resolver->setAllowedValues(array('layout' => $allowedValues)); // SF <2.8 BC
}
}

/**
Expand Down
6 changes: 3 additions & 3 deletions Resources/config/form.xml
Expand Up @@ -13,7 +13,7 @@
<parameter key="mopa_bootstrap.form.type_extension.legend.class">Mopa\Bundle\BootstrapBundle\Form\Extension\LegendFormTypeExtension</parameter>
<parameter key="mopa_bootstrap.form.type_extension.error.class">Mopa\Bundle\BootstrapBundle\Form\Extension\ErrorTypeFormTypeExtension</parameter>
<parameter key="mopa_bootstrap.form.type_extension.widget.class">Mopa\Bundle\BootstrapBundle\Form\Extension\WidgetFormTypeExtension</parameter>
<parameter key="mopa_bootstrap.form.type_extension.horizontal.class">Mopa\Bundle\BootstrapBundle\Form\Extension\HorizontalFormTypeExtension</parameter>
<parameter key="mopa_bootstrap.form.type_extension.layout.class">Mopa\Bundle\BootstrapBundle\Form\Extension\LayoutFormTypeExtension</parameter>
<parameter key="mopa_bootstrap.form.type_extension.widget_collection.class">Mopa\Bundle\BootstrapBundle\Form\Extension\WidgetCollectionFormTypeExtension</parameter>
<parameter key="mopa_bootstrap.form.type_extension.date.class">Mopa\Bundle\BootstrapBundle\Form\Extension\DateTypeExtension</parameter>
<parameter key="mopa_bootstrap.form.type_extension.datetime.class">Mopa\Bundle\BootstrapBundle\Form\Extension\DatetimeTypeExtension</parameter>
Expand Down Expand Up @@ -89,9 +89,9 @@
<tag name="form.type_extension" alias="form" extended-type="Symfony\Component\Form\Extension\Core\Type\FormType" />
</service>

<service id="mopa_bootstrap.form.type_extension.horizontal" class="%mopa_bootstrap.form.type_extension.horizontal.class%">
<service id="mopa_bootstrap.form.type_extension.horizontal" class="%mopa_bootstrap.form.type_extension.layout.class%">
<argument type="collection">
<argument key="horizontal">%mopa_bootstrap.form.horizontal%</argument>
<argument key="layout">%mopa_bootstrap.form.layout%</argument>
<argument key="horizontal_label_class">%mopa_bootstrap.form.horizontal_label_class%</argument>
<argument key="horizontal_label_div_class">%mopa_bootstrap.form.horizontal_label_div_class%</argument>
<argument key="horizontal_label_offset_class">%mopa_bootstrap.form.horizontal_label_offset_class%</argument>
Expand Down
3 changes: 2 additions & 1 deletion Resources/doc/misc/configuration-reference.md
Expand Up @@ -8,7 +8,8 @@ mopa_bootstrap:
form:
allow_legacy: false
templating: MopaBootstrapBundle:Form:fields.html.twig
horizontal: true
# Default form layout
layout: ~ # One of false; "horizontal"
horizontal_label_class: col-sm-3 control-label
horizontal_label_div_class: null
horizontal_label_offset_class: col-sm-offset-3
Expand Down
14 changes: 10 additions & 4 deletions Resources/views/Form/fields.html.twig
Expand Up @@ -36,7 +36,7 @@
</div>
</div>
{% else %}
<div>
<div class="form-group">
{{ form_widget(form) }}
</div>
{% endif %}
Expand Down Expand Up @@ -204,6 +204,9 @@
{% if expanded %}
{% set attr = attr|merge({'class': attr.class|default('') ~ ' ' ~ horizontal_input_wrapper_class}) %}
{% endif %}
{% if layout is sameas(false) %}

This comment has been minimized.

Copy link
@rodgermd

rodgermd Aug 25, 2017

deprecated usage of "same as" test

This comment has been minimized.

Copy link
@bderidder

bderidder Aug 25, 2017

This particular line throws a 500 Internal Server Error when running on Symfony 3.2.

Error from Twig:

Unknown "sameas" test. Did you mean "same as"?

<div>
{% endif %}
{% if widget_type == 'inline-btn' %}
{% set tagName = 'button' %}
<div class="btn-group" data-toggle="buttons">
Expand Down Expand Up @@ -240,6 +243,9 @@
{% if widget_type == 'inline-btn' %}
</div>
{% endif %}
{% if layout is sameas(false) %}
</div>
{% endif %}
{% endspaceless %}
{% endblock choice_widget_expanded %}

Expand Down Expand Up @@ -270,7 +276,7 @@
{% endif %}
{% endif %}
<label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}
{%- if not horizontal %} class="checkbox-inline"{% endif %}>
{%- if layout == 'inline' %} class="checkbox-inline"{% endif %}>
{% endif %}
<input type="checkbox" {{ block('widget_attributes') }}{% if value is defined %} value="{{ value }}"{% endif %}{% if checked %} checked="checked"{% endif %}/>
{% if form.parent != null and 'choice' not in form.parent.vars.block_prefixes %}
Expand Down Expand Up @@ -459,7 +465,7 @@
{% set label_attr = label_attr|merge({'for': id}) %}
{% endif %}
{% set label_attr_class = '' %}
{% if horizontal %}
{% if layout == 'horizontal' %}
{% set label_attr_class = 'control-label ' ~ label_attr_class ~ horizontal_label_class %}
{% endif %}
{% if horizontal_label_div_class %}
Expand Down Expand Up @@ -565,7 +571,7 @@
{% else %}
{{ block('widget_form_group_start') }}

{% if horizontal and not label_render %}
{% if layout == 'horizontal' and not label_render %}
{% set horizontal_input_wrapper_class = horizontal_input_wrapper_class ~ ' ' ~ horizontal_label_offset_class %}
{% endif %}

Expand Down
12 changes: 6 additions & 6 deletions Tests/Form/AbstractDivLayoutTest.php
Expand Up @@ -5,7 +5,7 @@
use Mopa\Bundle\BootstrapBundle\Form\Extension\EmbedFormExtension;
use Mopa\Bundle\BootstrapBundle\Form\Extension\ErrorTypeFormTypeExtension;
use Mopa\Bundle\BootstrapBundle\Form\Extension\HelpFormTypeExtension;
use Mopa\Bundle\BootstrapBundle\Form\Extension\HorizontalFormTypeExtension;
use Mopa\Bundle\BootstrapBundle\Form\Extension\LayoutFormTypeExtension;
use Mopa\Bundle\BootstrapBundle\Form\Extension\LegendFormTypeExtension;
use Mopa\Bundle\BootstrapBundle\Form\Extension\StaticTextExtension;
use Mopa\Bundle\BootstrapBundle\Form\Extension\TabbedFormTypeExtension;
Expand Down Expand Up @@ -105,7 +105,7 @@ protected function getExtensions()
$this->getHelpFormTypeExtension(),
$this->getWidgetFormTypeExtension(),
$this->getLegendFormTypeExtension(),
$this->getHorizontalFormTypeExtension(),
$this->getLayoutFormTypeExtension(),
$this->getErrorTypeFormTypeExtension(),
$this->getEmbedFormExtension(),
$this->getTabbedFormTypeExtension(),
Expand Down Expand Up @@ -175,12 +175,12 @@ protected function getLegendFormTypeExtension()
}

/**
* @return HorizontalFormTypeExtension
* @return LayoutFormTypeExtension
*/
protected function getHorizontalFormTypeExtension()
protected function getLayoutFormTypeExtension()
{
return new HorizontalFormTypeExtension(array(
'horizontal' => true,
return new LayoutFormTypeExtension(array(
'layout' => 'horizontal',
'horizontal_label_class' => 'col-sm-3',
'horizontal_label_div_class' => null,
'horizontal_label_offset_class' => 'col-sm-offset-3',
Expand Down
8 changes: 4 additions & 4 deletions Tests/Form/CollectionLayoutTest.php
Expand Up @@ -176,8 +176,8 @@ public function testChildrenHorizontal()
))
->add('names', $this->getFormType('collection'), array(
$this->getCollectionTypeKey() => $this->getFormType('text'),
$this->getCollectionOptionsKey() => array('horizontal' => true),
'horizontal' => false,
$this->getCollectionOptionsKey() => array('layout' => 'horizontal'),
'layout' => false,
))
->getForm()
->createView()
Expand Down Expand Up @@ -261,7 +261,7 @@ public function testAllNotHorizontal()
))
->add('names', $this->getFormType('collection'), array(
$this->getCollectionTypeKey() => $this->getFormType('text'),
'horizontal' => false,
'layout' => false,
))
->getForm()
->createView()
Expand Down Expand Up @@ -324,4 +324,4 @@ public function testAllNotHorizontal()
'
);
}
}
}
2 changes: 1 addition & 1 deletion Tests/Form/SimpleDivLayoutTest.php
Expand Up @@ -8,7 +8,7 @@ public function testHorizontalRow()
{
$view = $this->factory
->createNamed('name', $this->getFormType('email'), null, array(
'horizontal' => true,
'layout' => 'horizontal',
))
->createView()
;
Expand Down
6 changes: 3 additions & 3 deletions Tests/Form/TypeTestCase.php
Expand Up @@ -108,10 +108,10 @@ protected function getTypeExtensions()
'help_widget_popover' => $this->container->getParameter('mopa_bootstrap.form.help_widget.popover')
]
),
new MopaExtensions\HorizontalFormTypeExtension(
new MopaExtensions\LayoutFormTypeExtension(
[
'horizontal' => $this->container->getParameter(
'mopa_bootstrap.form.horizontal'
'layout' => $this->container->getParameter(
'mopa_bootstrap.form.layout'
),
'horizontal_label_class' => $this->container->getParameter(
'mopa_bootstrap.form.horizontal_label_class'
Expand Down

0 comments on commit 1e73d90

Please sign in to comment.