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
/**
* @param \Sonata\AdminBundle\Datagrid\ListMapper $listMapper
* @return void
*/
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.
{% 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
public function getNombreAnnonce()
{
return count($this->getAnnonces());
}
That’s all.