Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In the end, the most straightforward solution was to define a var in the Admin class as follows:</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 { // two options to define var and make them available to twig by admin. mechanism public $dateFormat = "Y-m-d";// ---&gt; this var is available in twig : admin.dateFormat // or to allow better relation to context public $parameters = array();// ---&gt; this array is available in twig : admin.parameters.x.y.z protected function configureListFields(ListMapper $listMapper) { // assuming the $dateFormat is not set, by convention it could be defined here with proper intent in mind // it could then be contained in an array like that $this-&gt;parameters = array('listmapper' =&gt; array('dateFormat' =&gt; 'Y-m-d',),); // then specialized in twig : admin.parameters.listmapper.dateFormat $listMapper -&gt;addIdentifier('acme_field') -&gt;add('date', 'date', array( 'template' =&gt; "AcmeBundle:CRUD:list_date.html.twig",// checking admin.parameters.listmapper.dateFormat )) -&gt;add('_action', 'actions', array( 'actions' =&gt; array( 'edit' =&gt; array(), 'delete' =&gt; array(), ), )) ; } </code></pre> <p>Then in twig:</p> <pre><code> {% block field%} {% if value is empty %} &amp;nbsp; {% else %} {# retrieving the dateFormat variable from the listmapper by convention... twig is very forgiving in eval, so checking for defined var is peachy e.g if admin.parameters is not defined or empty then else is triggered #} {% if admin.parameters.listmapper.dateFormat is defined %} {{ value|date(admin.parameters.listmapper.dateFormat)|title }} {% else %} {{ value|localizeddate('medium', 'none')|title }} {% endif %} {% endif %} {% endblock %} </code></pre> <p>Thanks to <a href="https://stackoverflow.com/questions/11482910/sonata-admin-add-custom-triggers-actions-to-list-edit-action?rq=1">Sonata Admin: Add custom triggers/actions to list/edit action</a> I realized/remembered/connected the dots... that twig has access to the admin variable, so by defining a variable in the class we can retrieve it in twig.</p> <p>Now, it is just a question of convention to put in place a sort of constraint to a container for the admin variables. (cf parameters array)</p> <p>For this particular case, this solution does not add any point as localizeddate serves well my purpose, however, for the sake of the argument it is good to know IMHO.</p> <p>Cheers, Hope that helps!</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.
    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