簡體   English   中英

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

[英]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;

如何更新表單以便我能夠單擊該字段以便它在另一個選項卡中打開文件?

您需要在configureShowFieldsfilePath字段聲明一個模板,這是您的案例的示例:

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 %}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM