mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-08 18:49:26 +03:00
Form labels translation support.
This commit is contained in:
parent
81274509da
commit
befd12e3e5
@ -22,6 +22,7 @@ use Symfony\Component\Form\FormFactoryInterface;
|
|||||||
use Symfony\Component\Form\FormInterface;
|
use Symfony\Component\Form\FormInterface;
|
||||||
use Symfony\Component\Form\ResolvedFormTypeInterface;
|
use Symfony\Component\Form\ResolvedFormTypeInterface;
|
||||||
use Symfony\Component\OptionsResolver\Exception\MissingOptionsException;
|
use Symfony\Component\OptionsResolver\Exception\MissingOptionsException;
|
||||||
|
use Symfony\Component\Translation\TranslatorInterface;
|
||||||
|
|
||||||
class FormTypeParser implements ParserInterface
|
class FormTypeParser implements ParserInterface
|
||||||
{
|
{
|
||||||
@ -35,6 +36,11 @@ class FormTypeParser implements ParserInterface
|
|||||||
*/
|
*/
|
||||||
protected $formRegistry;
|
protected $formRegistry;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \Symfony\Component\Translation\TranslatorInterface
|
||||||
|
*/
|
||||||
|
protected $translator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var boolean
|
* @var boolean
|
||||||
*/
|
*/
|
||||||
@ -105,9 +111,10 @@ class FormTypeParser implements ParserInterface
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
public function __construct(FormFactoryInterface $formFactory, $entityToChoice)
|
public function __construct(FormFactoryInterface $formFactory, TranslatorInterface $translator, $entityToChoice)
|
||||||
{
|
{
|
||||||
$this->formFactory = $formFactory;
|
$this->formFactory = $formFactory;
|
||||||
|
$this->translator = $translator;
|
||||||
$this->entityToChoice = (boolean) $entityToChoice;
|
$this->entityToChoice = (boolean) $entityToChoice;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -202,6 +209,8 @@ class FormTypeParser implements ParserInterface
|
|||||||
private function parseForm($form)
|
private function parseForm($form)
|
||||||
{
|
{
|
||||||
$parameters = array();
|
$parameters = array();
|
||||||
|
$domain = $form->getConfig()->getOption('translation_domain');
|
||||||
|
|
||||||
foreach ($form as $name => $child) {
|
foreach ($form as $name => $child) {
|
||||||
$config = $child->getConfig();
|
$config = $child->getConfig();
|
||||||
$options = $config->getOptions();
|
$options = $config->getOptions();
|
||||||
@ -293,7 +302,7 @@ class FormTypeParser implements ParserInterface
|
|||||||
'default' => null,
|
'default' => null,
|
||||||
'subType' => $subType,
|
'subType' => $subType,
|
||||||
'required' => $config->getRequired(),
|
'required' => $config->getRequired(),
|
||||||
'description' => ($config->getOption('description')) ? $config->getOption('description') : $config->getOption('label'),
|
'description' => $this->getFormDescription($config, $domain),
|
||||||
'readonly' => $config->getDisabled(),
|
'readonly' => $config->getDisabled(),
|
||||||
'children' => $children,
|
'children' => $children,
|
||||||
);
|
);
|
||||||
@ -311,7 +320,7 @@ class FormTypeParser implements ParserInterface
|
|||||||
'actualType' => 'string',
|
'actualType' => 'string',
|
||||||
'default' => $config->getData(),
|
'default' => $config->getData(),
|
||||||
'required' => $config->getRequired(),
|
'required' => $config->getRequired(),
|
||||||
'description' => ($config->getOption('description')) ? $config->getOption('description') : $config->getOption('label'),
|
'description' => $this->getFormDescription($config, $domain),
|
||||||
'readonly' => $config->getDisabled(),
|
'readonly' => $config->getDisabled(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -327,7 +336,7 @@ class FormTypeParser implements ParserInterface
|
|||||||
'subType' => $subType,
|
'subType' => $subType,
|
||||||
'default' => $config->getData(),
|
'default' => $config->getData(),
|
||||||
'required' => $config->getRequired(),
|
'required' => $config->getRequired(),
|
||||||
'description' => ($config->getOption('description')) ? $config->getOption('description'):$config->getOption('label'),
|
'description' => $this->getFormDescription($config, $domain),
|
||||||
'readonly' => $config->getDisabled(),
|
'readonly' => $config->getDisabled(),
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -445,4 +454,17 @@ class FormTypeParser implements ParserInterface
|
|||||||
|
|
||||||
return $choices;
|
return $choices;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getFormDescription($config, $domain = null)
|
||||||
|
{
|
||||||
|
$description = ($config->getOption('description'))
|
||||||
|
? $config->getOption('description')
|
||||||
|
: $config->getOption('label');
|
||||||
|
|
||||||
|
if ($description != null) {
|
||||||
|
return $this->translator->trans($description, [], $domain);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
<services>
|
<services>
|
||||||
<service id="nelmio_api_doc.parser.form_type_parser" class="%nelmio_api_doc.parser.form_type_parser.class%">
|
<service id="nelmio_api_doc.parser.form_type_parser" class="%nelmio_api_doc.parser.form_type_parser.class%">
|
||||||
<argument type="service" id="form.factory" />
|
<argument type="service" id="form.factory" />
|
||||||
|
<argument type="service" id="translator" />
|
||||||
<argument>%nelmio_api_doc.sandbox.entity_to_choice%</argument>
|
<argument>%nelmio_api_doc.sandbox.entity_to_choice%</argument>
|
||||||
<tag name="nelmio_api_doc.extractor.parser" />
|
<tag name="nelmio_api_doc.extractor.parser" />
|
||||||
</service>
|
</service>
|
||||||
|
@ -22,6 +22,7 @@ use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
|
|||||||
use Symfony\Component\Form\FormFactory;
|
use Symfony\Component\Form\FormFactory;
|
||||||
use Symfony\Component\Form\FormFactoryBuilder;
|
use Symfony\Component\Form\FormFactoryBuilder;
|
||||||
use Symfony\Component\Form\ResolvedFormTypeFactory;
|
use Symfony\Component\Form\ResolvedFormTypeFactory;
|
||||||
|
use Symfony\Component\Translation\Translator;
|
||||||
|
|
||||||
class FormTypeParserTest extends \PHPUnit_Framework_TestCase
|
class FormTypeParserTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
@ -37,7 +38,7 @@ class FormTypeParserTest extends \PHPUnit_Framework_TestCase
|
|||||||
$formFactoryBuilder->addTypeExtension(new DescriptionFormTypeExtension());
|
$formFactoryBuilder->addTypeExtension(new DescriptionFormTypeExtension());
|
||||||
$formFactoryBuilder->addType(new DependencyType(array('foo')));
|
$formFactoryBuilder->addType(new DependencyType(array('foo')));
|
||||||
$formFactory = $formFactoryBuilder->getFormFactory();
|
$formFactory = $formFactoryBuilder->getFormFactory();
|
||||||
$formTypeParser = new FormTypeParser($formFactory, $entityToChoice = true);
|
$formTypeParser = new FormTypeParser($formFactory, new Translator('en'), $entityToChoice = true);
|
||||||
|
|
||||||
set_error_handler(array('Nelmio\ApiDocBundle\Tests\WebTestCase', 'handleDeprecation'));
|
set_error_handler(array('Nelmio\ApiDocBundle\Tests\WebTestCase', 'handleDeprecation'));
|
||||||
trigger_error('test', E_USER_DEPRECATED);
|
trigger_error('test', E_USER_DEPRECATED);
|
||||||
@ -64,7 +65,7 @@ class FormTypeParserTest extends \PHPUnit_Framework_TestCase
|
|||||||
$formFactoryBuilder->addExtension(new CoreExtension());
|
$formFactoryBuilder->addExtension(new CoreExtension());
|
||||||
$formFactoryBuilder->addTypeExtension(new DescriptionFormTypeExtension());
|
$formFactoryBuilder->addTypeExtension(new DescriptionFormTypeExtension());
|
||||||
$formFactory = $formFactoryBuilder->getFormFactory();
|
$formFactory = $formFactoryBuilder->getFormFactory();
|
||||||
$formTypeParser = new FormTypeParser($formFactory, $entityToChoice = true);
|
$formTypeParser = new FormTypeParser($formFactory, new Translator('en'), $entityToChoice = true);
|
||||||
|
|
||||||
set_error_handler(array('Nelmio\ApiDocBundle\Tests\WebTestCase', 'handleDeprecation'));
|
set_error_handler(array('Nelmio\ApiDocBundle\Tests\WebTestCase', 'handleDeprecation'));
|
||||||
trigger_error('test', E_USER_DEPRECATED);
|
trigger_error('test', E_USER_DEPRECATED);
|
||||||
@ -87,7 +88,7 @@ class FormTypeParserTest extends \PHPUnit_Framework_TestCase
|
|||||||
$formFactoryBuilder->addTypeExtension(new DescriptionFormTypeExtension());
|
$formFactoryBuilder->addTypeExtension(new DescriptionFormTypeExtension());
|
||||||
$formFactoryBuilder->addType(new DependencyType(array('bar')));
|
$formFactoryBuilder->addType(new DependencyType(array('bar')));
|
||||||
$formFactory = $formFactoryBuilder->getFormFactory();
|
$formFactory = $formFactoryBuilder->getFormFactory();
|
||||||
$formTypeParser = new FormTypeParser($formFactory, $entityToChoice = false);
|
$formTypeParser = new FormTypeParser($formFactory, new Translator('en'), $entityToChoice = false);
|
||||||
|
|
||||||
set_error_handler(array('Nelmio\ApiDocBundle\Tests\WebTestCase', 'handleDeprecation'));
|
set_error_handler(array('Nelmio\ApiDocBundle\Tests\WebTestCase', 'handleDeprecation'));
|
||||||
trigger_error('test', E_USER_DEPRECATED);
|
trigger_error('test', E_USER_DEPRECATED);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user