如何使用来自数据的货币字符串在列表中添加货币字段?

问题描述 投票:0回答:1

(在列表视图中添加currency字段非常简单

protected function configureShowFields(ShowMapper $showMapper)
    {
        $showMapper
            ->add('price', 'currency', [
                'currency' => 'EUR',
                'locale' => 'fr',
            ])
        ;
    }

但是,如果我的货币字符串(欧元,美元,......)来自数据本身(又不是在代码片段中,而是来自数据库表中的字段)?

我可以以某种方式注入currency字符串吗?

sonata-admin
1个回答
0
投票

您可以为项目设置自定义模板,然后将对象访问到其中。

    protected function configureShowFields(ShowMapper $showMapper)
    {
        $showMapper
            ->add('price', 'currency', [
                'currency' => 'EUR',
                'locale' => 'fr',
                'template' =>  '@AdminTemplates/sonata/show_currency.html.twig',
            ])
        ;
    }
{# @AdminTemplates/sonata/show_currency.html.twig #}
{% extends '@SonataAdmin/CRUD/base_show_field.html.twig' %}

{%- block field -%}
    {% spaceless %}
        {%- if value is null -%}
             
        {%- else -%}
        {{ value|localizedcurrency(object.currencyField) }}
        {%- endif -%}
    {% endspaceless %}
{%- endblock field -%}

在这个例子中,我使用了localizedcurrency中的Twig Intl Extension

如果你使用SonataIntlBundle,你的模板可以扩展show_currency.html.twig,也许你可以覆盖currency字段选项。

希望这可以帮助

© www.soinside.com 2019 - 2024. All rights reserved.