EasyAdmin Slug字段
此字段用于动态生成另一个文本字段的 slug。文本的 slug 是一个简化版本,可以安全地包含在 URL 和文件名中。例如,Lorem Ipsum Dolor Sit Amet
的 slug 通常类似于 lorem-ipsum-dolor-sit-amet
。
在表单页面(编辑和新建)中,它看起来像这样

基本信息
- PHP 类:
EasyCorp
\Bundle \EasyAdminBundle \Field \SlugField - Doctrine DBAL 类型 用于存储此值:
string
或ascii_string
- Symfony 表单类型 用于渲染字段:
SlugType
,EasyAdmin 基于 Symfony 的 TextType 创建的自定义表单类型 渲染为:
1
<input type="text" value="...">
选项
setTargetFieldName
此字段使用 JavaScript 根据另一个字段的内容动态生成 slug。此选项定义与该字段关联的实体属性的名称
1
yield SlugField::new('...')->setTargetFieldName('title');
您可以传递多个字段名称来生成 slug,将所有这些字段的 slug 连接起来(它们按照您定义字段的顺序连接)
1 2
// the slugs are concatenated in the same order (e.g. '2023-news-lorem-ipsum')
yield SlugField::new('...')->setTargetFieldName(['year', 'type', 'title']);
setUnlockConfirmationMessage
默认情况下,slug 和另一个字段都同步,当另一个字段的内容更改时,slug 文本也会更改。但是,用户可以单击此字段的锁定图标来停止同步内容,这对于自定义 slug 文本是必需的。
如果您想自定义停止内容同步时显示的消息,请使用此选项
1 2 3
yield SlugField::new('...')->setUnlockConfirmationMessage(
'It is highly recommended to use the automatic slugs, but you can customize them'
);
这项工作,包括代码示例,均根据 Creative Commons BY-SA 3.0 许可协议获得许可。