I have an entity "File" and i want to show boolean value of existing related file in filesystem. For checking existence need to use my DirectoriesManager service which can detect it uses this File entity. What is the right way of configuring ListMapper for this task or it can be solved only by rewriting some sonata templates?
So, what i made:
config.yml
twig:
debug: '%kernel.debug%'
strict_variables: '%kernel.debug%'
globals:
container: '@service_container'
My Sonata Admin class
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->add('id')
->add('exist', null, [
'template' => 'AdminBundle:Files:exist.html.twig'
]);
}
And my template exist.html.twig
{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
{% block field %}
{% set value = container.get('dirs_manager').entityFileExist(object) %}
{% if value %}
{% set text = 'label_type_yes'|trans({}, 'SonataAdminBundle') %}
{% else %}
{% set text = 'label_type_no'|trans({}, 'SonataAdminBundle') %}
{% endif %}
{% if value %}
{% set class = 'label-success' %}
{% else %}
{% set class = 'label-danger' %}
{% endif %}
<span class="label {{ class }}">{{ text }}</span>
{% endblock %}
Where service DirectoriesManager has alias dirs_manager.
I know that in Yii2 can configure GridView widget columns with callback for all models that shows any value. May be i can make something like in ListMapper?
I would add listener callback for doctrine's postLoad event. And inside that callback would use the service and set corresponding boolean entity's value.