对于我的Symfony2应用的所有实体形式,我希望所有标签都具有以下形式:<entity_name>.<field_name>
之后有可能更改标签会很酷。
我怎样才能做到这一点?
答案 0 :(得分:3)
默认标签的行为在FieldType
:
(注意:在Symfony 2.1中不推荐使用FieldType类,在Symfony 2.3中删除了它)
如果要更改默认类型的行为,可以创建扩展名:
https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/AbstractTypeExtension.php
class FieldTypeEntityLabelExtension
{
static $currentTypeName;
public function buildForm(FormBuilder $builder, array $options)
{
$types = $builder->getTypes();
$type = end($types);
$dataClass = get_class($type);
if (strpos($dataClass, 'Uc') === 0) // <-- My namespace
{
$classParts = explode( '\\', $dataClass);
$translationParts = array_slice($classParts, 3);
self::$currentTypeName = implode('.', array_map('Symfony\Component\DependencyInjection\Container::underscore', $translationParts));
}
if (isset(self::$currentTypeName) && !$options['precious'])
{
$builder->setAttribute('label', sprintf('%s.%s', substr(self::$currentTypeName, 0, -5), $builder->getName()));
}
}
public function getExtendedType()
{
return 'field';
}
}