From 1e73d9006045431769d80184032972376e556dd3 Mon Sep 17 00:00:00 2001 From: Emmanuel Vella Date: Thu, 24 Aug 2017 16:25:16 +0200 Subject: [PATCH] Replace 'horizontal' option with 'layout' (#1044) --- DependencyInjection/Configuration.php | 19 ++++++++-- ...ension.php => LayoutFormTypeExtension.php} | 36 ++++++++++++++----- Resources/config/form.xml | 6 ++-- Resources/doc/misc/configuration-reference.md | 3 +- Resources/views/Form/fields.html.twig | 14 +++++--- Tests/Form/AbstractDivLayoutTest.php | 12 +++---- Tests/Form/CollectionLayoutTest.php | 8 ++--- Tests/Form/SimpleDivLayoutTest.php | 2 +- Tests/Form/TypeTestCase.php | 6 ++-- 9 files changed, 73 insertions(+), 33 deletions(-) rename Form/Extension/{HorizontalFormTypeExtension.php => LayoutFormTypeExtension.php} (71%) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 5aaa6e334..34fc52b66 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -35,6 +35,8 @@ public function getConfigTreeBuilder() protected function addFormConfig(ArrayNodeDefinition $rootNode) { + $layouts = array(false, 'horizontal', 'inline'); + $rootNode ->children() ->arrayNode('form') @@ -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") @@ -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(); } diff --git a/Form/Extension/HorizontalFormTypeExtension.php b/Form/Extension/LayoutFormTypeExtension.php similarity index 71% rename from Form/Extension/HorizontalFormTypeExtension.php rename to Form/Extension/LayoutFormTypeExtension.php index 42174ff5b..26c7a7e7a 100644 --- a/Form/Extension/HorizontalFormTypeExtension.php +++ b/Form/Extension/LayoutFormTypeExtension.php @@ -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 */ -class HorizontalFormTypeExtension extends AbstractTypeExtension +class LayoutFormTypeExtension extends AbstractTypeExtension { /** * @var array @@ -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'], @@ -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']; } } @@ -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 + } } /** diff --git a/Resources/config/form.xml b/Resources/config/form.xml index 86f202d20..7ac7bab20 100644 --- a/Resources/config/form.xml +++ b/Resources/config/form.xml @@ -13,7 +13,7 @@ Mopa\Bundle\BootstrapBundle\Form\Extension\LegendFormTypeExtension Mopa\Bundle\BootstrapBundle\Form\Extension\ErrorTypeFormTypeExtension Mopa\Bundle\BootstrapBundle\Form\Extension\WidgetFormTypeExtension - Mopa\Bundle\BootstrapBundle\Form\Extension\HorizontalFormTypeExtension + Mopa\Bundle\BootstrapBundle\Form\Extension\LayoutFormTypeExtension Mopa\Bundle\BootstrapBundle\Form\Extension\WidgetCollectionFormTypeExtension Mopa\Bundle\BootstrapBundle\Form\Extension\DateTypeExtension Mopa\Bundle\BootstrapBundle\Form\Extension\DatetimeTypeExtension @@ -89,9 +89,9 @@ - + - %mopa_bootstrap.form.horizontal% + %mopa_bootstrap.form.layout% %mopa_bootstrap.form.horizontal_label_class% %mopa_bootstrap.form.horizontal_label_div_class% %mopa_bootstrap.form.horizontal_label_offset_class% diff --git a/Resources/doc/misc/configuration-reference.md b/Resources/doc/misc/configuration-reference.md index ed568f535..a1e6ed206 100644 --- a/Resources/doc/misc/configuration-reference.md +++ b/Resources/doc/misc/configuration-reference.md @@ -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 diff --git a/Resources/views/Form/fields.html.twig b/Resources/views/Form/fields.html.twig index 28ab03ab7..edcacec0a 100644 --- a/Resources/views/Form/fields.html.twig +++ b/Resources/views/Form/fields.html.twig @@ -36,7 +36,7 @@ {% else %} -
+
{{ form_widget(form) }}
{% endif %} @@ -204,6 +204,9 @@ {% if expanded %} {% set attr = attr|merge({'class': attr.class|default('') ~ ' ' ~ horizontal_input_wrapper_class}) %} {% endif %} + {% if layout is sameas(false) %} +
+ {% endif %} {% if widget_type == 'inline-btn' %} {% set tagName = 'button' %}
@@ -240,6 +243,9 @@ {% if widget_type == 'inline-btn' %}
{% endif %} + {% if layout is sameas(false) %} +
+ {% endif %} {% endspaceless %} {% endblock choice_widget_expanded %} @@ -270,7 +276,7 @@ {% endif %} {% endif %} + {%- if layout == 'inline' %} class="checkbox-inline"{% endif %}> {% endif %} {% if form.parent != null and 'choice' not in form.parent.vars.block_prefixes %} @@ -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 %} @@ -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 %} diff --git a/Tests/Form/AbstractDivLayoutTest.php b/Tests/Form/AbstractDivLayoutTest.php index b37e3b121..385bcfca4 100644 --- a/Tests/Form/AbstractDivLayoutTest.php +++ b/Tests/Form/AbstractDivLayoutTest.php @@ -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; @@ -105,7 +105,7 @@ protected function getExtensions() $this->getHelpFormTypeExtension(), $this->getWidgetFormTypeExtension(), $this->getLegendFormTypeExtension(), - $this->getHorizontalFormTypeExtension(), + $this->getLayoutFormTypeExtension(), $this->getErrorTypeFormTypeExtension(), $this->getEmbedFormExtension(), $this->getTabbedFormTypeExtension(), @@ -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', diff --git a/Tests/Form/CollectionLayoutTest.php b/Tests/Form/CollectionLayoutTest.php index cc7fd3e72..178984c5e 100644 --- a/Tests/Form/CollectionLayoutTest.php +++ b/Tests/Form/CollectionLayoutTest.php @@ -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() @@ -261,7 +261,7 @@ public function testAllNotHorizontal() )) ->add('names', $this->getFormType('collection'), array( $this->getCollectionTypeKey() => $this->getFormType('text'), - 'horizontal' => false, + 'layout' => false, )) ->getForm() ->createView() @@ -324,4 +324,4 @@ public function testAllNotHorizontal() ' ); } -} \ No newline at end of file +} diff --git a/Tests/Form/SimpleDivLayoutTest.php b/Tests/Form/SimpleDivLayoutTest.php index f7d3d81e3..179d00c53 100644 --- a/Tests/Form/SimpleDivLayoutTest.php +++ b/Tests/Form/SimpleDivLayoutTest.php @@ -8,7 +8,7 @@ public function testHorizontalRow() { $view = $this->factory ->createNamed('name', $this->getFormType('email'), null, array( - 'horizontal' => true, + 'layout' => 'horizontal', )) ->createView() ; diff --git a/Tests/Form/TypeTestCase.php b/Tests/Form/TypeTestCase.php index 5de3639ae..cf6fcbd16 100644 --- a/Tests/Form/TypeTestCase.php +++ b/Tests/Form/TypeTestCase.php @@ -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'