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

时间:2019-03-17 09:50:14

标签: sonata-admin

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

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

但是,如果我的货币字符串(EUR,USD,...)来自数据本身(又不像摘要中那样,而是来自DB表中的字段)怎么办?

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

1 个答案:

答案 0 :(得分:0)

您可以为商品设置自定义模板,然后将其访问对象。

import { Injectable, Inject } from '@angular/core';
import { IHotParser} from './parser/parser.injector';

@Injectable()
export class EntryService {
    constructor(
        @Inject(HotParser) private parser: IHotParser // HotParser is not defined
    ) { }

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

在此示例中,我使用了Twig Intl Extension中的{# @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 -%}

如果您使用的是SonataIntlBundle,则模板可以扩展show_currency.html.twig,也许您可​​以覆盖localizedcurrency字段选项。

希望这会有所帮助

相关问题