Note that there are some explanatory texts on larger screens.

plurals
  1. POIn symfony2 with sonata: How to pass a variable to the template defined for a given field of the listmapper in the method configureListFields?
    primarykey
    data
    text
    <p>Using Symfony2 with Sonata, in a list, fields templates can be overwritten and assigning variables to templates, i.e setTemplateVar(), can sometimes be useful! (not talking about form where 'attr' serves this purpose but list...)</p> <p>I would like to know what would be the best approach to pass a variable to the template defined for a given field of the listmapper in the method configureListFields ?</p> <pre><code> &lt;?php namespace Acme\AcmeBundle\Admin; use Sonata\AdminBundle\Admin\Admin; use Sonata\AdminBundle\Datagrid\ListMapper; use Sonata\AdminBundle\Datagrid\DatagridMapper; use Sonata\AdminBundle\Validator\ErrorElement; use Sonata\AdminBundle\Form\FormMapper; class AcmeAdmin extends Admin { protected function configureListFields(ListMapper $listMapper) { $listMapper -&gt;addIdentifier('acme_field') -&gt;add('date', 'date', array( 'template' =&gt; "AcmeBundle:CRUD:list_date.html.twig", // 'dateFormat' =&gt; "Y-m-d",// ---&gt; how to pass this var to twig ? )) -&gt;add('_action', 'actions', array( 'actions' =&gt; array( 'edit' =&gt; array(), 'delete' =&gt; array(), ), )) ; } </code></pre> <p>A solution to the specific problem of translating and formatting the date is already implemented with the twig template as follows:</p> <pre><code> {% block field%} {% if value is empty %} &amp;nbsp; {% else %} {# retrieving the dateFormat variable from the listmapper #} {% if dateFormat is defined %} {{ value|date(dateFormat)|title }} {% else %} {{ value|date('m / Y') }} {% endif %} {# passing the locale in some way here would be great, it is not available in twig.. #} {# scratch that, it is not necessary with intl.extension... #} {% if locale is defined %} {% set dflt_locale = locale %} {% else %} {% set dflt_locale = 'fr_FR.UTF-8' %} {% endif %} {{ value|localizeddate('medium', 'none', dflt_locale)|title }} {% endif %} {% endblock %} </code></pre> <p>However, my goal is to be able to retrieve a variable there from the listmapper.<br> In the example proposed, the dateFormat would be a good one to pass along...</p> <p><em>The issue about the locale is fine actually, as I realized it does not need to be passed to localizeddate... it is already available through the intl extension.</em></p> <p>The following post answer lead me to that conclusion as I could not define locale, but by not adding the parameter it was resolved! <a href="https://stackoverflow.com/questions/9480325/localize-dates-in-twigs-using-symfony-2">Localize dates in twigs using Symfony 2</a> hence:</p> <pre><code> {{ value|localizeddate('medium', 'none')|title }} </code></pre> <p>Cheers in advance for any response related to the dateFormat parameter! </p> <p>Vinz</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload