This is how I currently do it, is there a better way, or more proper way?
protected function configureListFields(ListMapper $listMapper)
{
$this->sale_type_arr = array('1'=>'Value1','2'=>'Value2');
$listMapper
->add('saleType','string', array(
'template' => 'AppBundle:ItemAdmin:list_saleType.html.twig',
'label'=>'saleType'
))
...
}
And my template file like this:
<td>
{% if object.saleType !='0' %}
{{ attribute( admin.sale_type_arr, object.saleType) }}
{% endif %}
</td>
It all works fine, no problem, just doesn't feel right. There should be a way of how to directly inject array value, based on Items database value inside add(..).
Help appreciated.
I do not advise you to use {{ attribute(...) }} in field. The scope is completely hidden when visualising your template.
A better solution is to set directly the associative array in template. It is not ideal in architecture point of view, but offers you best code visibility.
See what I propose below (I write all necessary code for the complete template):
{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
{% block field %}
{% set sale_type_arr_custom = {1: 'Value1', 2: 'Value2'} %}
{% if object.saleType !='0' %}
{# Additionaly, protect for unknown key #}
{% if object.saleType in sale_type_arr_custom|keys %}
{{ sale_type_arr_custom[object.saleType] }}
{% endif %}
{% endif %}
{% endblock %}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.