跳到内容

字段类型

编辑此页

列表和显示操作

在列表和显示操作中可以使用许多字段类型

字段类型 描述
FieldDescriptionInterface::TYPE_ARRAY 显示来自数组的值
FieldDescriptionInterface::TYPE_BOOLEAN 根据布尔值显示绿色或红色图片
FieldDescriptionInterface::TYPE_DATE 显示格式化的日期。 接受 format 选项
FieldDescriptionInterface::TYPE_TIME 显示格式化的时间。 接受 formattimezone 选项
FieldDescriptionInterface::TYPE_DATETIME 显示格式化的日期和时间。 接受 formattimezone 选项
FieldDescriptionInterface::TYPE_STRING 显示文本
FieldDescriptionInterface::TYPE_EMAIL 显示 mailto 链接。 接受 as_string, subjectbody 选项
FieldDescriptionInterface::TYPE_ENUM 显示枚举
FieldDescriptionInterface::TYPE_TEXTAREA 显示文本区域
FieldDescriptionInterface::TYPE_TRANS 使用提供的 value_translation_domainformat (sprintf 格式) 选项翻译值
FieldDescriptionInterface::TYPE_FLOAT 显示数字
FieldDescriptionInterface::TYPE_CURRENCY 显示带有提供的 currency 选项的数字
FieldDescriptionInterface::TYPE_PERCENT 显示百分比
FieldDescriptionInterface::TYPE_CHOICE 使用给定值作为 choices 数组的索引,并显示(以及可选地翻译)匹配的值
FieldDescriptionInterface::TYPE_URL 显示链接
FieldDescriptionInterface::TYPE_HTML 显示(并可选择截断或剥离标签)原始 html
FieldDescriptionInterface::TYPE_MANY_TO_MANY 用于关系表
FieldDescriptionInterface::TYPE_MANY_TO_ONE 用于关系表
FieldDescriptionInterface::TYPE_ONE_TO_MANY 用于关系表
FieldDescriptionInterface::TYPE_ONE_TO_ONE 用于关系表

这些类型接受 editable 选项,以从列表操作中编辑值。目前仅限于标量类型(文本、整数、url...)和带有关联字段的选择类型。

注意

如果项目中安装了 SonataIntlBundle,某些模板类型将被更改为使用本地化信息。

货币类型的选项必须是官方 ISO 代码,例如:EUR 代表 “欧元”。 ISO 代码列表:https://en.wikipedia.org/wiki/List_of_circulating_currencies

FieldDescriptionInterface::TYPE_DATE, FieldDescriptionInterface::TYPE_TIMEFieldDescriptionInterface::TYPE_DATETIME 字段类型中,format 模式必须匹配 twig 的 date 过滤器规范,请访问:https://twig.symfony.ac.cn/doc/2.x/filters/date.html

FieldDescriptionInterface::TYPE_TIMEFieldDescriptionInterface::TYPE_DATETIME 字段类型中,timezone 语法必须匹配 twig 的 date 过滤器规范,请访问:https://twig.symfony.ac.cn/doc/2.x/filters/date.html 以及 php 时区列表:https://php.ac.cn/manual/en/timezones.php 您可以在列表中使用 view-timezone 在表单中允许的内容,这是一种在用户时区中呈现日期的方式

1
2
3
4
5
6
7
8
9
10
11
protected function configureListFields(ListMapper $list): void
{
    $list

        // store date in UTC but display is in the user timezone
        ->add('date', null, [
            'format' => 'Y-m-d H:i',
            'timezone' => 'America/New_York',
        ])
    ;
}

FieldDescriptionInterface::TYPE_ARRAY

您可以使用以下选项

选项 描述
inline 如果为 true,数组将显示为单行,整个数组和每个数组级别都将用方括号括起来。 如果为 false,数组将显示为无序列表。 对于 show 操作,默认值为 true,对于 list 操作,默认值为 false
display 定义应显示的内容:键、值或两者。 默认为 'both'。 可用选项为:'both', 'keys', 'values'
key_translation_domain 此选项确定是否应翻译键以及在哪个翻译域中进行翻译。 此选项的值可以是 true(使用管理翻译域)、false(禁用翻译)、null(使用父翻译域或默认域)或表示要使用的确切翻译域的字符串。
value_translation_domain 此选项确定是否应翻译值以及在哪个翻译域中进行翻译。 此选项的值可以是 true(使用管理翻译域)、false(禁用翻译)、null(使用父翻译域或默认域)或表示要使用的确切翻译域的字符串。
1
2
3
4
5
6
7
8
9
10
11
protected function configureListFields(ListMapper $list): void
{
    $list
        ->add('options', FieldDescriptionInterface::TYPE_ARRAY, [
            'inline' => true,
            'display' => 'both',
            'key_translation_domain' => true,
            'value_translation_domain' => null
        ])
    ;
}

FieldDescriptionInterface::TYPE_BOOLEAN

您可以使用以下选项

选项 描述
ajax_hidden 是/否; ajax_hidden 允许在 AJAX 上下文中隐藏列表字段。
editable 是/否; editable 允许在授权的情况下直接从列表中编辑。
inverse 是/否; 反转背景颜色(false 为绿色,true 为红色)。
1
2
3
4
5
6
7
8
9
protected function configureListFields(ListMapper $list): void
{
    $list
        ->add('invalid', FieldDescriptionInterface::TYPE_BOOLEAN, [
            'editable' => true,
            'inverse'  => true,
        ])
    ;
}

注意

对于布尔值,最好尽可能首选非负面概念,因此如果您真的找不到足够好的反义词来表示您拥有的名称,请使用 inverse 选项。

FieldDescriptionInterface::TYPE_CHOICE

您可以使用以下选项

选项 描述
choices 选项数组。
multiple 确定是否允许选择多个选项。 默认为 false。
delimiter 值的分隔符(如果为多选)。
choice_translation_domain 翻译域。
class 可编辑关联字段的类限定名。
required editable 选项设置为 true 时,字段是否为必填项(默认为 true)。 如果为 false,将添加一个空占位符。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
protected function configureListFields(ListMapper $list)
{
    // For the value `prog`, the displayed text is `In progress`. The `App` domain will be used to translate `In progress` message.
    $list
        ->add('status', FieldDescriptionInterface::TYPE_CHOICE, [
            'choices' => [
                'prep' => 'Prepared',
                'prog' => 'In progress',
                'done' => 'Done',
            ],
            'choice_translation_domain' => 'App',
        ])
    ;
}

FieldDescriptionInterface::TYPE_CHOICE 字段类型也支持可以用 delimiter 分隔的多个值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
protected function configureListFields(ListMapper $list): void
{
    // For the value `['r', 'b']`, the displayed text ist `red | blue`.
    $list
        ->add('colors', FieldDescriptionInterface::TYPE_CHOICE, [
            'multiple' => true,
            'delimiter' => ' | ',
            'choices' => [
                'r' => 'red',
                'g' => 'green',
                'b' => 'blue',
            ]
        ])
    ;
}

注意

默认分隔符是逗号 ,

FieldDescriptionInterface::TYPE_ENUM

您可以使用以下选项

选项 描述
use_value 确定字段是否必须显示值或 case' 名称。 false 默认值。 如果枚举实现了 Symfony 的 TranslatableInterface ,则忽略此项。
enum_translation_domain 翻译域。 如果设置,枚举值或 case' 名称将发送给翻译器。 {{ value|trans({}, translation_domain) }} 如果枚举实现了 Symfony 的 TranslatableInterface ,则忽略此项。

注意

如果枚举实现了 Symfony 的 TranslatableInterface,则上面的选项将被忽略,而是使用枚举的 trans() 方法来显示枚举。

这提供了与 symfony 的 EnumType 表单类型的完全兼容性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
protected function configureListFields(ListMapper $list): void
{
    $list
        // Sonata Admin will select the `FieldDescriptionInterface::TYPE_ENUM`
        // field type automatically. If the enum implements `TranslatableInterface`,
        // the `trans()` method will be used to render its value.
        ->add('saluation')
    ;
}

protected function configureFormFields(FormMapper $form): void
{
    $form
        // Symfony's EnumType form field will automatically detect the usage of
        // the `TranslatableInterface` and use the enum's `trans()` method to
        // render the choice labels.
        ->add('salutation', EnumType::class, [
            'class' => Salutation::class,
        ])
    ;
}

protected function configureShowFields(ShowMapper $show): void
{
    $show
        // Again, Sonata Admin will select the `FieldDescriptionInterface::TYPE_ENUM`
        // field type automatically. If the enum implements `TranslatableInterface`,
        // the `trans()` method will be used to render its value.
        ->add('salutation')
    ;
}

FieldDescriptionInterface::TYPE_URL

显示外部网站或控制器操作的 URL 链接。

您可以使用以下选项

选项 描述
hide_protocol 从链接文本中删除协议部分
url URL 地址 (例如. http://example.com)
attributes html 标签属性数组 (例如. ['target' => '_blank'])
route.name 路由名称 (例如. acme_blog_homepage)
route.parameters 路由参数数组 (例如. ['type' => 'example', 'display' => 'full'])
route.absolute 布尔值,基于 route.nameroute.parameters 创建绝对或相对 url 地址 (默认 false)
route.identifier_parameter_name 添加到 route.parameters 的参数,其值是对象标识符 (例如 'id'),用于创建基于呈现对象的动态链接。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
protected function configureListFields(ListMapper $list): void
{
    $list
        // Output for value `http://example.com`:
        // `<a href="http://example.com">http://example.com</a>`
        ->add('targetUrl', FieldDescriptionInterface::TYPE_URL)

        // Output for value `http://example.com`:
        // `<a href="http://example.com" target="_blank">example.com</a>`
        ->add('targetUrl', FieldDescriptionInterface::TYPE_URL, [
            'attributes' => ['target' => '_blank']
        ])

        // Output for value `http://example.com`:
        // `<a href="http://example.com">example.com</a>`
        ->add('targetUrl', FieldDescriptionInterface::TYPE_URL, [
            'hide_protocol' => true
        ])

        // Output for value `Homepage of example.com` :
        // `<a href="http://example.com">Homepage of example.com</a>`
        ->add('title', FieldDescriptionInterface::TYPE_URL, [
            'url' => 'http://example.com'
        ])

        // Output for value `Acme Blog Homepage`:
        // `<a href="http://blog.example.com">Acme Blog Homepage</a>`
        ->add('title', FieldDescriptionInterface::TYPE_URL, [
            'route' => [
                'name' => 'acme_blog_homepage',
                'absolute' => true
            ]
        ])

        // Output for value `Sonata is great!` (related object has identifier `123`):
        // `<a href="http://blog.example.com/xml/123">Sonata is great!</a>`
        ->add('title', FieldDescriptionInterface::TYPE_URL, [
            'route' => [
                'name' => 'acme_blog_article',
                'absolute' => true,
                'parameters' => ['format' => 'xml'],
                'identifier_parameter_name' => 'id'
            ]
        ])
    ;
}

注意

不要将 FieldDescriptionInterface::TYPE_URL 类型与 addIdentifier() 方法一起使用,因为它会创建无效的嵌套 URL。

FieldDescriptionInterface::TYPE_HTML

显示(并可选择截断或剥离标签)原始 html。

您可以使用以下选项

选项 描述
strip 从字符串中剥离 HTML 和 PHP 标签
truncate 从头开始将字符串截断为 length 个字符。 意味着剥离。 注意 HTML 实体。 如果要使用截断,请确保配置您的 HTML 编辑器以禁用实体。 例如,对 ckeditor 使用 config.entities
truncate.length 要截断字符串的长度(默认为 30
truncate.cut 确定是否必须切断整个单词(默认为 true
truncate.ellipsis 要附加到修剪后的字符串的省略号(默认为 ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
protected function configureListFields(ListMapper $list): void
{
    $list

        // Output for value `<p><strong>Creating a Template for the Field</strong> and form</p>`:
        // `<p><strong>Creating a Template for the Field</strong> and form</p>` (no escaping is done)
        ->add('content', FieldDescriptionInterface::TYPE_HTML)

        // Output for value `<p><strong>Creating a Template for the Field</strong> and form</p>`:
        // `Creating a Template for the Fi...`
        ->add('content', FieldDescriptionInterface::TYPE_HTML, [
            'strip' => true
        ])

        // Output for value `<p><strong>Creating a Template for the Field</strong> and form</p>`:
        // `Creating a Template for...`
        ->add('content', FieldDescriptionInterface::TYPE_HTML, [
            'truncate' => true
        ])

        // Output for value `<p><strong>Creating a Template for the Field</strong> and form</p>`:
        // `Creating a...`
        ->add('content', FieldDescriptionInterface::TYPE_HTML, [
            'truncate' => [
                'length' => 10
            ]
        ])

        // Output for value `<p><strong>Creating a Template for the Field</strong> and form</p>`:
        // `Creating a Template for the Field...`
        ->add('content', FieldDescriptionInterface::TYPE_HTML, [
            'truncate' => [
                'cut' => false
            ]
        ])

        // Output for value `<p><strong>Creating a Template for the Field</strong> and form</p>`:
        // `Creating a Template for the Fi, etc.`
        ->add('content', FieldDescriptionInterface::TYPE_HTML, [
            'truncate' => [
                'ellipsis' => ', etc.'
            ]
        ])

        // Output for value `<p><strong>Creating a Template for the Field</strong> and form</p>`:
        // `Creating a Template for***`
        ->add('content', FieldDescriptionInterface::TYPE_HTML, [
            'truncate' => [
                'length' => 20,
                'cut' => false,
                'ellipsis' => '***'
            ]
        ])
    ;
}

创建你自己的字段类型

字段类型是在配置部分注册的 Twig 模板,与您的模型管理器匹配。 下面的示例使用 sonata_doctrine_orm_admin

1
2
3
4
5
6
7
# config/sonata_doctrine_orm_admin.yaml

sonata_doctrine_orm_admin:
    templates:
        types:
            show: # or "list"
                dump: 'field_types/show_dump.html.twig'

现在将一个 twig 文件添加到您的 templates/ 目录。 下面的示例使用 @SonataAdmin/CRUD/base_show_field.html.twig 来提供 “show” 模板使用的行布局。 在此基本模板中,您可以覆盖 field 块以重写此行中字段内容单元格的内容。

1
2
3
4
5
6
7
{# templates/field_types/show_dump.html.twig #}

{% extends '@SonataAdmin/CRUD/base_show_field.html.twig' %}

{% block field %}
    {{ dump(value) }}
{% endblock %}

查看 @SonataAdmin/Resources/views/CRUD 中的默认模板,以了解编写字段模板时的可能性。

您现在可以在您的管理后台中使用它

1
2
3
4
5
protected function configureShowFields(ShowMapper $show): void
{
    $show
        ->add('foo', 'dump');
}
这项工作,包括代码示例,均根据 Creative Commons BY-SA 3.0 许可获得许可。
目录
    版本