From 9bd183e52d8a1791a8f133b78ae798456074e4de Mon Sep 17 00:00:00 2001 From: Jelmer Prins Date: Wed, 6 Jan 2016 14:25:19 +0100 Subject: [PATCH] Add option to set an icon on a button At the moment, for example in the FrameworkUserBundle icons are added by setting the icon classes to the buttons This changes the font of the button to the icon font making it not match the rest of the application By extending this button it is now possible to add an icon to that button In the template: {{ form_widget(form.example, { 'icon': 'fa fa-bug', 'label': 'Bug', 'attr':{'class': 'btn btn-large btn-default btn-block' } }) }} Or in the controller $builder ->add('example', SubmitType::class, [ 'label' => 'Bug', 'icon' => 'fa fa-bug', 'attr' => [ 'class' => 'btn btn-large btn-default btn-block', ], ] ); --- .../Extension/ButtonTypeIconExtension.php | 51 +++++++++++++++++++ .../Resources/config/services.yml | 5 ++ .../Resources/views/Form/fields.html.twig | 20 ++++++++ 3 files changed, 76 insertions(+) create mode 100644 src/SumoCoders/FrameworkCoreBundle/Form/Extension/ButtonTypeIconExtension.php diff --git a/src/SumoCoders/FrameworkCoreBundle/Form/Extension/ButtonTypeIconExtension.php b/src/SumoCoders/FrameworkCoreBundle/Form/Extension/ButtonTypeIconExtension.php new file mode 100644 index 0000000..57de9dd --- /dev/null +++ b/src/SumoCoders/FrameworkCoreBundle/Form/Extension/ButtonTypeIconExtension.php @@ -0,0 +1,51 @@ +setAttribute('icon', $options['icon']); + } + + /** + * @param FormView $view + * @param FormInterface $form + * @param array $options + */ + public function buildView(FormView $view, FormInterface $form, array $options) + { + $view->vars['icon'] = $options['icon']; + } + + /** + * @param OptionsResolverInterface $resolver + */ + public function setDefaultOptions(OptionsResolverInterface $resolver) + { + $resolver->setDefaults([ + 'icon' => null, + ]); + } + + /** + * Extend the button field type + * + * @return string The name of the type being extended + */ + public function getExtendedType() + { + return 'Symfony\Component\Form\Extension\Core\Type\ButtonType'; + } +} diff --git a/src/SumoCoders/FrameworkCoreBundle/Resources/config/services.yml b/src/SumoCoders/FrameworkCoreBundle/Resources/config/services.yml index 43f560f..74946ef 100644 --- a/src/SumoCoders/FrameworkCoreBundle/Resources/config/services.yml +++ b/src/SumoCoders/FrameworkCoreBundle/Resources/config/services.yml @@ -43,6 +43,11 @@ services: tags: - { name: form.type_extension, alias: date } + framework.button_type_icon_extension: + class: SumoCoders\FrameworkCoreBundle\Form\Extension\ButtonTypeIconExtension + tags: + - { name: form.type_extension, extended-type: Symfony\Component\Form\Extension\Core\Type\ButtonType } + twig.framework_extension: class: SumoCoders\FrameworkCoreBundle\Twig\FrameworkExtension arguments: diff --git a/src/SumoCoders/FrameworkCoreBundle/Resources/views/Form/fields.html.twig b/src/SumoCoders/FrameworkCoreBundle/Resources/views/Form/fields.html.twig index 86aa4b8..72d4e25 100644 --- a/src/SumoCoders/FrameworkCoreBundle/Resources/views/Form/fields.html.twig +++ b/src/SumoCoders/FrameworkCoreBundle/Resources/views/Form/fields.html.twig @@ -294,3 +294,23 @@ {% endif %} {% endspaceless %} {% endblock form_row %} + +{% block button_widget -%} + {% set attr = attr|merge({class: (attr.class|default('') ~ ' btn')|trim}) %} + {% if label is empty -%} + {%- if label_format is not empty -%} + {% set label = label_format|replace({ + '%name%': name, + '%id%': id, + }) %} + {%- else -%} + {% set label = name|humanize %} + {%- endif -%} + {%- endif -%} + {% if icon|default %} + {% set iconHtml = ' ' %} + {% else %} + {% set iconHtml = '' %} + {% endif %} + +{%- endblock button_widget %}