Customize form field rendering

I would like to configure the rendering of the form field on the edit page from the sonata admin package to include an applet that uses the text content of the field.

I know that I need to edit the configureFormFields function in the admin class, but I need to know 3 things:

  • What is the syntax for creating a field form template.
  • Where to put the template file (which directory)
  • What does the template look like.
+8
forms sonata-admin
source share
3 answers

Solution found

What I've done:

  • Created a field type, call it myfieldType in myCompany \ myBundle \ Form \ Type \ myfieldType.php

     namespace myCompany\myBundle\Form\Type; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilder; class myfieldType extends AbstractType { public function getParent() { return 'text'; } public function getName() { return 'myfield'; } } 
  • Register type in app / config / services.yml

     myCompany.myBundle.form.type.myfield: class: myCompany\myBundle\Form\Type\myfieldType tags: - { name: form.type, alias: myfield } 
  • In my class myentityAdmin

      protected function configureFormFields(FormMapper $formMapper) { $formMapper ->add('myfieldname', 'myfield') ... } 

    and

     public function getFormTheme() { return array('myCompanymyBundle:Admin:myfield_edit.html.twig'); } 

    and template:

     {# src/mycompany/myBundle/Resources/views/Form/myfield_edit.html.twig #} {% block myfield_widget %} {% spaceless %} {{ block('textarea_widget') }} {% endspaceless %} {% endblock %} 

And now I can access the value of the form field by the twig value variable!

So easy ... when you got it.

+21
source share

The user1254498 solution will not work if the block name prefix does not match the form type name. At least with the latest version of sonata admin package (2.2.12). In this case:

 {# src/mycompany/myBundle/Resources/views/Form/myfield_edit.html.twig #} {% block myfield_widget %} {% spaceless %} {{ block('textarea_widget') }} {% endspaceless %} {% endblock %} 

And by touching getFormTheme() , you also return the parent theme, otherwise you might break the whole style ...

 public function getFormTheme() { return array_merge( parent::getFormTheme(), array( 'mycompanyBundle:Form:myfield_edit.html.twig') ); } 

In addition, you can access the admin service in the branch template with the variable sonata_admin.admim .

+8
source share

In your services.yml file, you define a template for your action

 app.admin.product: class: AppBundle\Admin\ProductAdmin arguments: [~, AppBundle\Entity\Product, AppBundle:Admin\Product] tags: - {name: sonata.admin, manager_type: orm, group: Products, label: Products} calls: - [ setTemplate, [edit, AppBundle:Product:edit.html.twig]] 

In this template, you can override templates for fields in your form:

 {% extends 'SonataAdminBundle:CRUD:base_edit.html.twig' %} {% form_theme form.selectall 'AppBundle:Form:selectall.html.twig' %} {% form_theme form.Country 'AppBundle:Form:country.html.twig' %} 

Then my template looks like this:

 {% block form_row %} <div class="form-group"> {{ form_label(form) }} {% set c = 0 %} {% for i in form %} {% set c = c+1 %} {% if (c == 1) %} <div style="float: left; width: 20%;"> {% endif%} {{ form_row(i) }} {% if ((c == 60) or (form|length == loop.index)) %} </div> {% set c = 0 %} {% endif%} {% endfor %} </div> {% endblock form_row %} 

In this case, the flags of my countries are displayed in a column of 60 elements, and not in the same column with the entire list of elements.

Hope this helps someone else.

+4
source share

All Articles