Environment-driven CDN host for SonataMediaBundle images in wkhtmltopdf generated with Symfony KnpSnappyBundle and TWIG

To display images in wkhtmltopdf you need to enable images:

 

In addition you have to use absolute paths in your twig template. But sometimes you want to use different path based on your environment. Using assets you can easily do this:

 

Instead of setting the CDN host statically you can use parameters.yml:

Source: https://github.com/Jolg42/sonata-sandbox/blob/master/app/config/parameters.yml.sample#L30

 

Then in your sonata_media.yml :

Source: https://github.com/Jolg42/sonata-sandbox/blob/master/app/config/sonata/sonata_media.yml#L33

 

Now you can use the Sonata Media helpers to display the images:

 

How to check if Subject is New resp. action is create or edit in Sonata Admin

In your Admin Class:

In your Twig Template:

Source: https://github.com/sonata-project/SonataAdminBundle/blob/master/Resources/views/CRUD/base_edit_form.html.twig#L4

 

Backlinks:

Translating dynamic vars concatening object property in twig template using SonataAdminBundle

My Contract Entity has a history of states and the current state is displayed in the listView. The state is stored as a simple string e.g. “pending” in the database.

 

But the translation string are stored with a prefix e.g. “contracts.states.pending”:

 

To make the translation work inside the twig template you can concat the prefix and the property from the database like this:

 

Backlinks:

Create custom template list field for title and name with SonataUserBundle User Entity GENDER constants in SonataAdminBundle

The SonataUserBundle UserInterface uses the following GENDER constants:

We will use the gender to determine the title of a User and prefix it to the full name. To set a custom template in my UserAdmin class we configure the field:

The following custom template uses older Twig Syntax to retrieve the constants:

The following version is recommended and uses Twig Syntax (e.g. “ternary operator”) since 1.12.0: