To add a custom column on a page list, just follow these steps. In the example, we will add a “Nombre d’annoce(s)” column.
On your admin class file (ex: /AdminBundle/Admin/SecteurAdmin.php), on your configureListFields() fonction, add a new entry on the listMapper object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
protected function configureListFields(ListMapper $listMapper ) {
$listMapper
->addIdentifier( 'titre' )
->add( 'Tri' , 'string' , array ( 'template' => 'TripixAdminBundle:Admin:list_tri.html.twig' ))
->add( 'Nb' , 'string' , array ( 'label' => 'Nombre d\'annonce(s)' , 'template' => 'TripixAdminBundle:Admin:list_nb_annonce.html.twig' ))
->add( '_action' , 'actions' , array (
'actions' => array (
'edit' => array (),
'delete' => array (),
)
))
;
}
|
This new entry said to use the TripixADminBundle:Admin:list_tri.html.twig template.
Now, create your template file (list_nb_annonce.html) in the /AdminBundle/ressources/view/admin/ directory.
1 2 3 4 5 6 7 8 9 | {% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
{% block field%}
{% if admin.datagrid.results|length > 1 %}
<div>
<strong>{{ object.getNombreAnnonce}}</strong>
</div>
{% endif %}
{% endblock %}
|
In our example, getNombreAnnonce() is not defined, let’s do it
In the /adminBundle/Entity/Secteur.php file, add
1 2 3 4 | public function getNombreAnnonce()
{
return count ( $this ->getAnnonces());
}
|
That’s all.