Skip to content

Commit

Permalink
add compatibility with symfony 2.8 and 3.0 for choice type field
Browse files Browse the repository at this point in the history
  • Loading branch information
anacona16 committed Feb 23, 2017
1 parent 5ff7adc commit fc8fb4e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
9 changes: 8 additions & 1 deletion Form/Type/ScalingSettingFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Anacona16\Bundle\ImageCropBundle\Form\Type;

use Anacona16\Bundle\ImageCropBundle\Util\LegacyFormHelper;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
Expand All @@ -25,10 +26,16 @@ public function configureOptions(OptionsResolver $resolver)
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$optionsScaling = $options['scaling'];

if (!LegacyFormHelper::isLegacy()) {
$optionsScaling = array_flip($optionsScaling);
}

$builder
->add('scaling', ChoiceType::class, array(
'label' => 'form.label.scaling',
'choices' => $options['scaling'],
'choices' => $optionsScaling,
'translation_domain' => 'ImageCropBundle',
))
;
Expand Down
24 changes: 24 additions & 0 deletions Util/LegacyFormHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Anacona16\Bundle\ImageCropBundle\Util;

use Symfony\Component\HttpKernel\Kernel;

final class LegacyFormHelper
{
const MINIMUM_MAJOR_VERSION = 3;

/**
* Check whether to use legacy form behaviour from Symfony <3.0.
*
* @param int $majorVersion Major version
*
* @static
*
* @return bool
*/
public static function isLegacy($majorVersion = Kernel::MAJOR_VERSION)
{
return $majorVersion < self::MINIMUM_MAJOR_VERSION;
}
}

0 comments on commit fc8fb4e

Please sign in to comment.