From 125a614af43003c6fe14b29f24aa8c69ba654907 Mon Sep 17 00:00:00 2001 From: Decoder Date: Sun, 13 Dec 2015 21:31:02 +0300 Subject: [PATCH] Symfony 3.0 support --- Form/EventListener/TranslationsListener.php | 27 ++++++++----- Form/Type/TranslatedEntityType.php | 28 +++++++++---- Form/Type/TranslationsFieldsType.php | 20 ++++++++-- Form/Type/TranslationsFormsType.php | 30 ++++++++++---- Form/Type/TranslationsLocalesSelectorType.php | 22 +++++++--- Form/Type/TranslationsType.php | 24 ++++++++--- Util/LegacyFormHelper.php | 40 +++++++++++++++++++ 7 files changed, 150 insertions(+), 41 deletions(-) create mode 100644 Util/LegacyFormHelper.php diff --git a/Form/EventListener/TranslationsListener.php b/Form/EventListener/TranslationsListener.php index 3297883..20ce664 100644 --- a/Form/EventListener/TranslationsListener.php +++ b/Form/EventListener/TranslationsListener.php @@ -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 @@ -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']) + ) + ); + } } } } diff --git a/Form/Type/TranslatedEntityType.php b/Form/Type/TranslatedEntityType.php index 06a4fc6..78ff2c6 100644 --- a/Form/Type/TranslatedEntityType.php +++ b/Form/Type/TranslatedEntityType.php @@ -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 @@ -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', @@ -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'; } diff --git a/Form/Type/TranslationsFieldsType.php b/Form/Type/TranslationsFieldsType.php index 87f948d..320ff6d 100644 --- a/Form/Type/TranslationsFieldsType.php +++ b/Form/Type/TranslationsFieldsType.php @@ -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 @@ -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'; } diff --git a/Form/Type/TranslationsFormsType.php b/Form/Type/TranslationsFormsType.php index 806191e..5164b5d 100644 --- a/Form/Type/TranslationsFormsType.php +++ b/Form/Type/TranslationsFormsType.php @@ -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 @@ -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'], @@ -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, @@ -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'; } diff --git a/Form/Type/TranslationsLocalesSelectorType.php b/Form/Type/TranslationsLocalesSelectorType.php index e0c6770..8348c23 100644 --- a/Form/Type/TranslationsLocalesSelectorType.php +++ b/Form/Type/TranslationsLocalesSelectorType.php @@ -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 @@ -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()), @@ -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'; } diff --git a/Form/Type/TranslationsType.php b/Form/Type/TranslationsType.php index ec52cf0..8e2ea15 100644 --- a/Form/Type/TranslationsType.php +++ b/Form/Type/TranslationsType.php @@ -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 @@ -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, @@ -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'; } diff --git a/Util/LegacyFormHelper.php b/Util/LegacyFormHelper.php new file mode 100644 index 0000000..4ff3a8e --- /dev/null +++ b/Util/LegacyFormHelper.php @@ -0,0 +1,40 @@ + '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() + { + } +}