Skip to content

Commit

Permalink
Merge pull request #214 from idildin/master
Browse files Browse the repository at this point in the history
Symfony 3.0 support
  • Loading branch information
webda2l committed Jan 12, 2016
2 parents 2af32a2 + 125a614 commit b01e953
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 41 deletions.
27 changes: 17 additions & 10 deletions Form/EventListener/TranslationsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

namespace A2lix\TranslationFormBundle\Form\EventListener;

use Symfony\Component\Form\FormEvent,
use A2lix\TranslationFormBundle\Util\LegacyFormHelper,
A2lix\TranslationFormBundle\TranslationForm\TranslationForm,
Symfony\Component\Form\FormEvent,
Symfony\Component\Form\FormEvents,
Symfony\Component\EventDispatcher\EventSubscriberInterface,
A2lix\TranslationFormBundle\TranslationForm\TranslationForm;
Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* @author David ALLIX
Expand Down Expand Up @@ -37,13 +38,19 @@ public function preSetData(FormEvent $event)
$formOptions = $form->getConfig()->getOptions();
$fieldsOptions = $this->translationForm->getFieldsOptions($translationClass, $formOptions);

foreach ($formOptions['locales'] as $locale) {
if (isset($fieldsOptions[$locale])) {
$form->add($locale, 'a2lix_translationsFields', array(
'data_class' => $translationClass,
'fields' => $fieldsOptions[$locale],
'required' => in_array($locale, $formOptions['required_locales'])
));
if (isset($formOptions['locales'])) {
foreach ($formOptions['locales'] as $locale) {
if (isset($fieldsOptions[$locale])) {
$form->add(
$locale,
LegacyFormHelper::getType('A2lix\TranslationFormBundle\Form\Type\TranslationsFieldsType'),
array(
'data_class' => $translationClass,
'fields' => $fieldsOptions[$locale],
'required' => in_array($locale, $formOptions['required_locales'])
)
);
}
}
}
}
Expand Down
28 changes: 20 additions & 8 deletions Form/Type/TranslatedEntityType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace A2lix\TranslationFormBundle\Form\Type;

use Symfony\Component\Form\AbstractType,
use Doctrine\ORM\EntityRepository,
Symfony\Component\Form\AbstractType,
Symfony\Component\OptionsResolver\OptionsResolverInterface,
Symfony\Component\OptionsResolver\OptionsResolver,
Symfony\Component\OptionsResolver\Options,
Symfony\Component\HttpFoundation\Request,
Doctrine\ORM\EntityRepository;
Symfony\Component\HttpFoundation\Request;

/**
* Translated entity
Expand All @@ -23,10 +24,9 @@ public function setRequest(Request $request = null)
}

/**
*
* @param \Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver
* @param OptionsResolver $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'translation_path' => 'translations',
Expand All @@ -40,18 +40,30 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
if (null === $this->request) {
throw new \Exception('Error while getting request');
}

return $options['translation_path'] .'['. $this->request->getLocale() .'].'. $options['translation_property'];
},
));
}

// BC for SF < 2.7
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$this->configureOptions($resolver);
}

public function getParent()
{
return 'entity';
}


// BC for SF < 3.0
public function getName()
{
return $this->getBlockPrefix();
}

public function getBlockPrefix()
{
return 'a2lix_translatedEntity';
}
Expand Down
20 changes: 16 additions & 4 deletions Form/Type/TranslationsFieldsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

use Symfony\Component\Form\AbstractType,
Symfony\Component\Form\FormBuilderInterface,
Symfony\Component\OptionsResolver\OptionsResolverInterface;
Symfony\Component\OptionsResolver\OptionsResolverInterface,
Symfony\Component\OptionsResolver\OptionsResolver;

/**
* Translations fields
Expand All @@ -29,17 +30,28 @@ public function buildForm(FormBuilderInterface $builder, array $options)
}

/**
*
* @param \Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver
* @param OptionsResolver $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'fields' => array(),
));
}

// BC for SF < 2.7
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$this->configureOptions($resolver);
}

// BC for SF < 3.0
public function getName()
{
return $this->getBlockPrefix();
}

public function getBlockPrefix()
{
return 'a2lix_translationsFields';
}
Expand Down
30 changes: 22 additions & 8 deletions Form/Type/TranslationsFormsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

namespace A2lix\TranslationFormBundle\Form\Type;

use Symfony\Component\Form\FormView,
use A2lix\TranslationFormBundle\TranslationForm\TranslationForm,
A2lix\TranslationFormBundle\Form\EventListener\TranslationsFormsListener,
A2lix\TranslationFormBundle\Locale\LocaleProviderInterface,
Symfony\Component\Form\FormView,
Symfony\Component\Form\AbstractType,
Symfony\Component\Form\FormInterface,
Symfony\Component\Form\FormBuilderInterface,
Symfony\Component\OptionsResolver\OptionsResolverInterface,
A2lix\TranslationFormBundle\TranslationForm\TranslationForm,
A2lix\TranslationFormBundle\Form\EventListener\TranslationsFormsListener,
A2lix\TranslationFormBundle\Locale\LocaleProviderInterface;
Symfony\Component\OptionsResolver\OptionsResolver;

/**
* @author David ALLIX
Expand Down Expand Up @@ -44,6 +45,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$builder->addEventSubscriber($this->translationsListener);

$formsOptions = $this->translationForm->getFormsOptions($options);

foreach ($options['locales'] as $locale) {
if (isset($formsOptions[$locale])) {
$builder->add($locale, $options['form_type'],
Expand All @@ -63,13 +65,12 @@ public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['default_locale'] = $this->localeProvider->getDefaultLocale();
$view->vars['required_locales'] = $options['required_locales'];
}
}

/**
*
* @param \Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver
* @param OptionsResolver $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'by_reference' => false,
Expand All @@ -83,7 +84,20 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
));
}

// BC for SF < 2.7
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$this->configureOptions($resolver);
}


// BC for SF < 3.0
public function getName()
{
return $this->getBlockPrefix();
}

public function getBlockPrefix()
{
return 'a2lix_translationsForms';
}
Expand Down
22 changes: 17 additions & 5 deletions Form/Type/TranslationsLocalesSelectorType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace A2lix\TranslationFormBundle\Form\Type;

use Symfony\Component\Form\FormView,
use A2lix\TranslationFormBundle\Locale\LocaleProviderInterface,
Symfony\Component\Form\FormView,
Symfony\Component\Form\AbstractType,
Symfony\Component\Form\FormInterface,
Symfony\Component\OptionsResolver\OptionsResolverInterface,
A2lix\TranslationFormBundle\Locale\LocaleProviderInterface;
Symfony\Component\OptionsResolver\OptionsResolver;

/**
* @author David ALLIX
Expand Down Expand Up @@ -37,10 +38,9 @@ public function buildView(FormView $view, FormInterface $form, array $options)
}

/**
*
* @param \Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver
* @param OptionsResolver $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'choices' => array_combine($this->localeProvider->getLocales(), $this->localeProvider->getLocales()),
Expand All @@ -52,12 +52,24 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
));
}

// BC for SF < 2.7
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$this->configureOptions($resolver);
}

public function getParent()
{
return 'choice';
}

// BC for SF < 3.0
public function getName()
{
return $this->getBlockPrefix();
}

public function getBlockPrefix()
{
return 'a2lix_translationsLocalesSelector';
}
Expand Down
24 changes: 18 additions & 6 deletions Form/Type/TranslationsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

namespace A2lix\TranslationFormBundle\Form\Type;

use Symfony\Component\Form\FormView,
use A2lix\TranslationFormBundle\Form\EventListener\TranslationsListener,
A2lix\TranslationFormBundle\Locale\LocaleProviderInterface,
Symfony\Component\Form\FormView,
Symfony\Component\Form\AbstractType,
Symfony\Component\Form\FormInterface,
Symfony\Component\Form\FormBuilderInterface,
Symfony\Component\OptionsResolver\OptionsResolverInterface,
A2lix\TranslationFormBundle\Form\EventListener\TranslationsListener,
A2lix\TranslationFormBundle\Locale\LocaleProviderInterface;
Symfony\Component\OptionsResolver\OptionsResolver;

/**
* Regroup by locales, all translations fields
Expand Down Expand Up @@ -55,10 +56,9 @@ public function buildView(FormView $view, FormInterface $form, array $options)
}

/**
*
* @param \Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver
* @param OptionsResolver $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'by_reference' => false,
Expand All @@ -73,7 +73,19 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
));
}

// BC for SF < 2.7
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$this->configureOptions($resolver);
}

// BC for SF < 3.0
public function getName()
{
return $this->getBlockPrefix();
}

public function getBlockPrefix()
{
return 'a2lix_translations';
}
Expand Down
40 changes: 40 additions & 0 deletions Util/LegacyFormHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace A2lix\TranslationFormBundle\Util;

final class LegacyFormHelper
{
private static $map = array(
'A2lix\TranslationFormBundle\Form\Type\TranslationsType' => 'a2lix_translations',
'A2lix\TranslationFormBundle\Form\Type\TranslationsFieldsType' => 'a2lix_translationsFields',
'A2lix\TranslationFormBundle\Form\Type\TranslationsFormsType' => 'a2lix_translationsForms',
'A2lix\TranslationFormBundle\Form\Type\TranslatedEntityType' => 'a2lix_translatedEntity',
'A2lix\TranslationFormBundle\Form\Type\TranslationsLocalesSelectorType' => 'a2lix_translationsLocalesSelector',
);

public static function getType($class)
{
if (!self::isLegacy()) {
return $class;
}

if (!isset(self::$map[$class])) {
throw new \InvalidArgumentException(sprintf('Form type with class "%s" can not be found. Please check for typos or add it to the map in LegacyFormHelper', $class));
}

return self::$map[$class];
}

public static function isLegacy()
{
return !method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix');
}

private function __construct()
{
}

private function __clone()
{
}
}

0 comments on commit b01e953

Please sign in to comment.