Skip to content

Commit

Permalink
Adding ButtonTypeExtension which:
Browse files Browse the repository at this point in the history
* fixes double 'type' in markup bug
* allows usage of BS-Button styled <a>-Tag
* (+1 squashed commits)

Squashed commits:

[6f1d0e4] renaming button extensions new options for better clarity
  • Loading branch information
derpue committed Aug 20, 2014
1 parent 8489d8f commit 679c67e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
46 changes: 46 additions & 0 deletions Form/Extension/ButtonTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Braincrafted\Bundle\BootstrapBundle\Form\Extension;

use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;

/**
* FormControlStaticType
*
* @package BraincraftedBootstrapBundle
* @subpackage Form
* @author André Püschel <pue@der-pue.de>
* @copyright 2014 André Püschel
* @license http://opensource.org/licenses/MIT The MIT License
* @link http://bootstrap.braincrafted.com Bootstrap for Symfony2
*/
class ButtonTypeExtension extends AbstractTypeExtension
{
/**
* {@inheritDoc}
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['button_class'] = $form->getConfig()->getOption('button_class');
$view->vars['as_link'] = $form->getConfig()->getOption('as_link');
}

/**
* {@inheritdoc}
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setOptional(array('button_class', 'as_link'));
}

/**
* {@inheritdoc}
*/
public function getExtendedType()
{
return 'button';
}
}
5 changes: 5 additions & 0 deletions Resources/config/services/form.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<parameter key="braincrafted_bootstrap.form.type.money.class">Braincrafted\Bundle\BootstrapBundle\Form\Type\MoneyType</parameter>
<parameter key="braincrafted_bootstrap.form.type.form_actions.class">Braincrafted\Bundle\BootstrapBundle\Form\Type\FormActionsType</parameter>
<parameter key="braincrafted_bootstrap.form.extension.typesetter_extension.class">Braincrafted\Bundle\BootstrapBundle\Form\Extension\TypeSetterExtension</parameter>
<parameter key="braincrafted_bootstrap.form.extension.button_extension.class">Braincrafted\Bundle\BootstrapBundle\Form\Extension\ButtonTypeExtension</parameter>
<parameter key="braincrafted_bootstrap.form.extension.input_group_button.class">Braincrafted\Bundle\BootstrapBundle\Form\Extension\InputGroupButtonExtension</parameter>
</parameters>

Expand All @@ -29,6 +30,10 @@
<tag name="form.type_extension" alias="form" />
</service>

<service id="braincrafted_bootstrap.form.extension.form_action_button" class="%braincrafted_bootstrap.form.extension.button_extension.class%">
<tag name="form.type_extension" alias="button" />
</service>

<service id="braincrafted_bootstrap.form.extension.input_group_button" class="%braincrafted_bootstrap.form.extension.input_group_button.class%">
<tag name="form.type_extension" alias="text" />
</service>
Expand Down
10 changes: 7 additions & 3 deletions Resources/views/Form/bootstrap.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -502,11 +502,15 @@
{% set label = name|humanize %}
{% endif %}
{% if type is defined and type == 'submit' %}
{% set attr = attr|merge({ 'class': (attr.class|default('') ~ ' btn btn-'~attr.type|default('primary'))|trim }) %}
{% set attr = attr|merge({ 'class': (attr.class|default('') ~ ' btn btn-'~button_class|default('primary'))|trim }) %}
{% else %}
{% set attr = attr|merge({ 'class': (attr.class|default('') ~ ' btn btn-'~attr.type|default('default'))|trim }) %}
{% set attr = attr|merge({ 'class': (attr.class|default('') ~ ' btn btn-'~button_class|default('default'))|trim }) %}
{% endif %}
{% if as_link is defined and as_link == true %}
<a {{ block('button_attributes') }}>{% if attr.icon is defined and attr.icon != '' %}{{ icon(attr.icon) }}{% endif %}{{ label|trans({}, translation_domain) }}</a>
{% else %}
<button type="{{ type|default('button') }}" {{ block('button_attributes') }}>{% if attr.icon is defined and attr.icon != '' %}{{ icon(attr.icon) }}{% endif %}{{ label|trans({}, translation_domain) }}</button>
{% endif %}
<button type="{{ type|default('button') }}" {{ block('button_attributes') }}>{% if attr.icon is defined and attr.icon != '' %}{{ icon(attr.icon) }}{% endif %}{{ label|trans({}, translation_domain) }}</button>
{% endspaceless %}
{% endblock button_widget %}

Expand Down

0 comments on commit 679c67e

Please sign in to comment.