简体   繁体   English

Sonata Admin - 如何向字段添加链接

[英]Sonata Admin - How to add a link to a field

I have a field on my form in Sonata Admin:我在 Sonata Admin 的表单上有一个字段:

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

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

This relates to an entity which stores the path of a file.这与存储文件路径的实体有关。

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

How can I update the form so that I am able to click on the field so that it opens the file in another tab?如何更新表单以便我能够单击该字段以便它在另一个选项卡中打开文件?

You need to declare a template inside configureShowFields for the filePath field, here is a sample for your case:您需要在configureShowFieldsfilePath字段声明一个模板,这是您的案例的示例:

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

And the @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 %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM