Skip to content

Commit

Permalink
Bubble horizontal down like translation_domain does (#1180)
Browse files Browse the repository at this point in the history
* Buhble horizontal down like translation_domain does

* Update tests which weren't starting with horizontal because they weren't compound

* Remove horizontal_wrap_children as it should work properly now. Added tests for collections.

* Fix tests for newer symfony using entry_type instead of type on collections.

* Missed another key
  • Loading branch information
isometriks committed Jul 26, 2016
1 parent f054627 commit 3060fd7
Show file tree
Hide file tree
Showing 7 changed files with 403 additions and 62 deletions.
65 changes: 22 additions & 43 deletions Form/Extension/HorizontalFormTypeExtension.php
Expand Up @@ -44,49 +44,30 @@ public function __construct(array $options)
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
// Set the root form to the default value if none given
if ($options['horizontal'] === null && ((!$view->parent && $options['compound']) || ($form->getParent() === null && $options['compound']))) {
$horizontal = $this->options['horizontal'];
} else {
$horizontal = $options['horizontal'];
$horizontal = $options['horizontal'];

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

$view->vars['horizontal'] = $horizontal;
$view->vars['horizontal_label_class'] = $options['horizontal_label_class'];
$view->vars['horizontal_label_offset_class'] = $options['horizontal_label_offset_class'];
$view->vars['horizontal_input_wrapper_class'] = $options['horizontal_input_wrapper_class'];
$view->vars['horizontal_label_div_class'] = $options['horizontal_label_div_class'];
$view->vars = array_replace($view->vars, array(
'horizontal' => $horizontal,
'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'],
'horizontal_label_div_class' => $options['horizontal_label_div_class'],
));
}

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

$isForm = true;
}

if ($isForm || ($form->getParent() === null && $options['compound'])) {
$this->setChildrenHorizontal($view);
}
}

public function setChildrenHorizontal(FormView $view)
{
foreach ($view->children as $child) {
if (!in_array('form', $child->vars['block_prefixes'])) {
continue;
}

if ($child->vars['horizontal'] === null) {
$child->vars['horizontal'] = $view->vars['horizontal'];
}

if (count($view->children) > 0) {
$this->setChildrenHorizontal($child);
}
}
}

Expand All @@ -105,15 +86,13 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(
array(
'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'],
)
);
$resolver->setDefaults(array(
'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'],
));
}

/**
Expand Down
9 changes: 0 additions & 9 deletions Form/Extension/WidgetCollectionFormTypeExtension.php
Expand Up @@ -76,7 +76,6 @@ public function buildView(FormView $view, FormInterface $form, array $options)
$view->vars['widget_add_btn'] = $options['widget_add_btn'];
$view->vars['widget_remove_btn'] = $options['widget_remove_btn'];
$view->vars['prototype_names'] = $options['prototype_names'];
$view->vars['horizontal_wrap_children'] = $options['horizontal_wrap_children'];
}

/**
Expand All @@ -99,15 +98,7 @@ public function configureOptions(OptionsResolver $resolver)
'widget_add_btn' => $this->options['widget_add_btn'],
'widget_remove_btn' => $this->options['widget_remove_btn'],
'prototype_names' => array(),
'horizontal_wrap_children' => false,
));
if (version_compare(Kernel::VERSION, '2.6', '<')) {
$resolver->setAllowedTypes(array(
'horizontal_wrap_children' => 'bool',
));
} else {
$resolver->setAllowedTypes('horizontal_wrap_children', 'bool');
}
}

/**
Expand Down
6 changes: 2 additions & 4 deletions Resources/views/Form/fields.html.twig
Expand Up @@ -555,13 +555,11 @@
{% else %}
{{ block('widget_form_group_start') }}

{% set show_horizontal_wrapper = horizontal and not (form.parent is not null and 'collection' in form.parent.vars.block_prefixes and form.parent.vars.horizontal_wrap_children is same as(false)) %}

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

{% if show_horizontal_wrapper %}
{% if horizontal %}
<div class="{{ horizontal_input_wrapper_class }}">
{% endif %}

Expand All @@ -572,7 +570,7 @@
{{ block('form_message') }}
{% endif %}

{% if show_horizontal_wrapper %}
{% if horizontal %}
</div>
{% endif %}

Expand Down
52 changes: 48 additions & 4 deletions Tests/Form/AbstractDivLayoutTest.php
Expand Up @@ -9,8 +9,9 @@
use Mopa\Bundle\BootstrapBundle\Form\Extension\LegendFormTypeExtension;
use Mopa\Bundle\BootstrapBundle\Form\Extension\StaticTextExtension;
use Mopa\Bundle\BootstrapBundle\Form\Extension\TabbedFormTypeExtension;
use Mopa\Bundle\BootstrapBundle\Form\Extension\WidgetCollectionFormTypeExtension;
use Mopa\Bundle\BootstrapBundle\Form\Extension\WidgetFormTypeExtension;
use Mopa\Bundle\BootstrapBundle\Twig\FormExtension as FormExtension2;
use Mopa\Bundle\BootstrapBundle\Twig\FormExtension as TwigFormExtension;
use Mopa\Bundle\BootstrapBundle\Twig\IconExtension;
use Symfony\Bridge\Twig\Extension\FormExtension;
use Symfony\Bridge\Twig\Extension\TranslationExtension;
Expand All @@ -21,7 +22,6 @@
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\PreloadedExtension;
use Symfony\Component\Form\Test\FormIntegrationTestCase;
use Twig_Environment;

abstract class AbstractDivLayoutTest extends FormIntegrationTestCase
{
Expand All @@ -31,6 +31,7 @@ abstract class AbstractDivLayoutTest extends FormIntegrationTestCase
'form' => 'Symfony\Component\Form\Extension\Core\Type\FormType',
'text' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
'email' => 'Symfony\Component\Form\Extension\Core\Type\EmailType',
'collection' => 'Symfony\Component\Form\Extension\Core\Type\CollectionType',
);

/**
Expand Down Expand Up @@ -68,10 +69,10 @@ protected function setUp()

$loader->addPath(__DIR__.'/../../Resources/views', 'MopaBootstrap');

$environment = new Twig_Environment($loader, array('strict_variables' => true));
$environment = new \Twig_Environment($loader, array('strict_variables' => true));
$environment->addExtension(new TranslationExtension(new StubTranslator()));
$environment->addExtension(new IconExtension('fontawesome'));
$environment->addExtension(new FormExtension2());
$environment->addExtension(new TwigFormExtension());
$environment->addGlobal('global', '');
$environment->addExtension($this->extension);

Expand All @@ -92,6 +93,7 @@ protected function getExtensions()
$this->getErrorTypeFormTypeExtension(),
$this->getEmbedFormExtension(),
$this->getTabbedFormTypeExtension(),
$this->getWidgetCollectionFormTypeExtension(),
),
$this->getFormType('text') => array(
$this->getStaticTextFormTypeExtension(),
Expand Down Expand Up @@ -208,6 +210,30 @@ protected function getTabbedFormTypeExtension()
));
}

/**
* @return WidgetCollectionFormTypeExtension
*/
protected function getWidgetCollectionFormTypeExtension()
{
return new WidgetCollectionFormTypeExtension(array(
'render_collection_item' => true,
'widget_add_btn' => array(
'attr' => array('class' => 'btn btn-default'),
'label' => 'add-item',
'icon' => null,
'icon_inverted' => false,
),
'widget_remove_btn' => array(
'attr' => array('class' => 'btn btn-default'),
'wrapper_div' => array('class' => 'form-group'),
'horizontal_wrapper_div' => array('class' => 'col-sm-3 col-sm-offset-3'),
'label' => 'remove-item',
'icon' => null,
'icon_inverted' => false,
),
));
}

/**
* @param string $html
* @param string $expression
Expand Down Expand Up @@ -309,4 +335,22 @@ protected function getFormType($name)

return $name;
}

protected function getCollectionTypeKey()
{
if(method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')) {
return 'entry_type';
}

return 'type';
}

protected function getCollectionOptionsKey()
{
if(method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')) {
return 'entry_options';
}

return 'options';
}
}

0 comments on commit 3060fd7

Please sign in to comment.