-
Notifications
You must be signed in to change notification settings - Fork 347
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace 'horizontal' option with 'layout' (#1044)
- Loading branch information
1 parent
cbbd2d9
commit 1e73d90
Showing
9 changed files
with
73 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 <[email protected]> | ||
*/ | ||
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 | ||
} | ||
} | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,7 @@ | |
</div> | ||
</div> | ||
{% else %} | ||
<div> | ||
<div class="form-group"> | ||
{{ form_widget(form) }} | ||
</div> | ||
{% 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) %} | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
bderidder
|
||
<div> | ||
{% endif %} | ||
{% if widget_type == 'inline-btn' %} | ||
{% set tagName = 'button' %} | ||
<div class="btn-group" data-toggle="buttons"> | ||
|
@@ -240,6 +243,9 @@ | |
{% if widget_type == 'inline-btn' %} | ||
</div> | ||
{% endif %} | ||
{% if layout is sameas(false) %} | ||
</div> | ||
{% endif %} | ||
{% endspaceless %} | ||
{% endblock choice_widget_expanded %} | ||
|
||
|
@@ -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 %} | ||
|
@@ -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 %} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
deprecated usage of "same as" test