[英]Symfony2 : Sort / Order a translated entity form field?
I am trying to order an entity form field witch is translated.我正在尝试订购实体表单字段女巫被翻译。
I am using the symfony translation tool, so i can't order values with a SQL statement.我正在使用 symfony 翻译工具,所以我无法使用 SQL 语句对值进行排序。 Is there a way to sort values after there are loaded and translated ?
有没有办法在加载和翻译后对值进行排序?
Maybe using a form event ?也许使用表单事件?
$builder
->add('country', 'entity',
array(
'class' => 'MyBundle:Country',
'translation_domain' => 'countries',
'property' => 'name',
'empty_value' => '---',
)
)
I found the solution to sort my field values in my Form Type.我找到了在表单类型中对字段值进行排序的解决方案。
We have to use the finishView() method which is called when the form view is created :我们必须使用在创建表单视图时调用的 finishView() 方法:
<?php
namespace My\Namespace\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;
use Symfony\Bundle\FrameworkBundle\Translation\Translator;
class MyFormType extends AbstractType
{
protected $translator;
public function __construct(Translator $translator)
{
$this->translator = $translator;
}
public function finishView(FormView $view, FormInterface $form, array $options)
{
// Order translated countries
$collator = new \Collator($this->translator->getLocale());
usort(
$view->children['country']->vars['choices'],
function ($a, $b) use ($collator) {
return $collator->compare(
$this->translator->trans($a->label, array(), 'countries'),
$this->translator->trans($b->label, array(), 'countries')
);
}
);
}
// ...
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('country', 'entity',
array(
'class' => 'MyBundle:Country',
'translation_domain' => 'countries',
'property' => 'name',
'empty_value' => '---',
)
)
;
}
}
OLD ANSWER旧答案
I found a solution for my problem, I can sort them in my controller after creating the view :我找到了解决我的问题的方法,我可以在创建视图后在我的控制器中对它们进行排序:
$fview = $form->createView();
usort(
$fview->children['country']->vars['choices'],
function($a, $b) use ($translator){
return strcoll($translator->trans($a->label, array(), 'countries'), $translator->trans($b->label, array(), 'countries'));
}
);
Maybe I can do that in a better way ?也许我可以用更好的方式做到这一点? Originally I wished to do directly in my form builder instead of adding extra code in controllers where I use this form.
最初我希望直接在我的表单构建器中执行,而不是在我使用此表单的控制器中添加额外的代码。
I think it's impossible.我认为这是不可能的。 You need to use
PHP
sorting, but if you use Symfony Form Type, I would advise to sort it with JavaScript
after page is loaded.您需要使用
PHP
排序,但如果您使用 Symfony Form Type,我建议在页面加载后使用JavaScript
对其进行排序。
If your countries are in an array, just use the sort()
function, with the SORT_STRING
flag.如果您的国家/地区位于数组中,只需使用带有
SORT_STRING
标志的sort()
函数SORT_STRING
。 You will do some gymnastic to have it in my opinion.在我看来,你会做一些体操来拥有它。 Check this doc : http://php.net/manual/fr/function.sort.php
检查这个文档: http : //php.net/manual/fr/function.sort.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.