Symfony2 sonata custom CRUD template

It is possible to change templates use by default.

First, override the controller action method, in your AdminBundle/Controller/SecteurAdminController.php file

 /**
     * return the Response object associated to the list action
     *
     * @return Response
     */
    public function listAction()
    {
        if (false === $this->admin->isGranted('LIST')) {
            throw new AccessDeniedException();
        }

        $datagrid = $this->admin->getDatagrid();
        $formView = $datagrid->getForm()->createView();

        // set the theme for the current Admin Form
        $this->get('twig')->getExtension('form')->setTheme($formView, $this->admin->getFilterTheme());

        //custom code from here
        $total = 0;
        $enable = 0;
        $new = 0;
        $site = $this->admin->site;

        $repository = $this->getDoctrine()->getRepository('TripixAdminBundle:Annonce');
        $total = $repository->getNombreAnnonceTotal($site);
        $enable = $repository->getNombreAnnonceEnabled($site);
        $new = $repository->getNombreAnnonceNew($site);



        return $this->render('TripixAdminBundle:CRUD:list_secteur.html.twig', array(
            'action'   => 'list',
            'form'     => $formView,
            'datagrid' => $datagrid,
            'total'     => $total,
            'enable'    => $enable,
            'new'       => $new,
        ));
    }

The import thing on previous code is the render() fonction, the first parameter is the template to use.
In our example, the template file will be /AdminBundle/Ressources/view/CRUD/list_secteur.html.twig

You can use the /AdminBundle/Ressources/view/CRUD/base_secteur.html.twig file as example for your custom template.

Parent function to override on the controller are here : /vendor/bundles/Sonata/AdminBundle/Controller/CRUDController.php

Symfony2 Sonata add custom column on page list

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.

Symfony2 twig add new function file_exists

An important function is missing on twig : file_exists.

I will show you here how to define a new function for twig.

On your namespace/bundle, add a Twig/Extension directory and create your class file :
/src/Adin/ArlogisBundle/Twig/Extension/FileExistsExtension.php

<?php

namespace Adin\ArlogisBundle\Twig\Extension;

class FileExistsExtension extends \twig_Extension
{
        /**
         *Return the function registered as twig extension
         *
         *@return array
         */
        public function getFunctions()
        {
                return array(
                        'file_exists' => new \Twig_Function_Function('file_exists'),
                        );
        }

        public function getName()
        {
                return 'adin_file_exists';
        }
}
?>

You then have to register your service, add a in your /Adin/ArlogisBundle/Ressources/config/services.xml file

<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
       <service id="adin.twig.tripix_extension" class="Adin\ArlogisBundle\Twig\Extension\FileExistsExtension">
        <tag name="twig.extension" />
        </service>
    </services>
</container>

It’s ready, you can use it on your twig template

{% if file_exists('/var/www/image.jpg') %}
    File exists
{% else %}
    File not exists
{% endif %}

Be carefull, you need to specify the absolute path of the file. You can create a twig global variable root_path.
app/config/config.yml

# Twig Configuration
twig:
    globals:
        root_path: %kernel.root_dir%/../web

Then on twig template

{% if file_exists({{root_path}}'/var/www/image.jpg') %}
    File exists
{% else %}
    File not exists
{% endif %}

PHP Fatal error: Method Gregwar\\ImageBundle\\ImageHandler::__toString() must not throw an exception in /home/www/recette/app/cache/dev/classes.php on line 7426

You are using the fanstactic Gregwar/ImageBundle code to manipulate your images on Symfony2 and twig.
But instead of rendering your website, you get this error on your apache error log file

PHP Fatal error:  Method Gregwar\\ImageBundle\\ImageHandler::__toString() must not throw an exception in /home/www/recette/app/cache/dev/classes.php on line 7426

or directly on your html genered code

<img src="
Fatal error: Method Gregwar\ImageBundle\ImageHandler::__toString() must not throw an exception in /home/www/recette/app/cache/dev/classes.php on line 7426

this issue may be a rights problem on your cache drectory. By default, gregwar cache directory is on your web so, launch this command

chmod 777 web/cache

This error may also occur if the file doesn’t exist, for a clean code, add

{% if file_exists('uploads/' ~ site.id ~ '.jpg') %}
    {{ image(site.picture).resize(250, 50) }}
{% endif %}

or on twig code

{% if site.has_picture %}
    {{ image(site.picture).resize(250 ,50) }}
{% endif %}

For more informations about file_exists, see here Symfony2 twig add new function file_exists