Sonata Admin - 如何将 link 添加到字段

Sonata Admin - How to add a link to a field

我在 Sonata Admin 的表单上有一个字段:

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->add('filePath', TextType::class, [
            'disabled' => true,
        ]);
}

protected function configureShowFields(ShowMapper $showMapper)
{
    $showMapper
        ->add('filePath');
}

这与存储文件路径的实体有关。

class User 
{
    /**
     * @ORM\Column(type="string", nullable=true)
     */
    private $filePath;

如何更新表单以便我能够单击该字段以便在另一个选项卡中打开文件?

您需要在 configureShowFields 内为 filePath 字段声明一个模板,这里是您的案例示例:

protected function configureShowFields(ShowMapper $showMapper)
{
    $showMapper
        ->add('filePath', null, [
            'template' => '@App/Admin/file_path_link.html.twig',
        ]);
}

@App/Admin/file_path_link.html.twig:

{% if value %}
<a href="{{ value }}">click to download</a>
{% else %}
No file path
{% endif %}