Skip to content
This repository was archived by the owner on Feb 16, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace SumoCoders\FrameworkCoreBundle\Form\Extension;

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

class ButtonTypeIconExtension extends AbstractTypeExtension
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->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';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<i class="' ~ icon ~ '"></i> ' %}
{% else %}
{% set iconHtml = '' %}
{% endif %}
<button type="{{ type|default('button') }}" {{ block('button_attributes') }}>{{ iconHtml|raw }}{{ label|trans({}, translation_domain) }}</button>
{%- endblock button_widget %}