Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize form field rendering

I would like to customize the rendering of a form field in the edit page from sonata admin bundle to include an applet that uses the text content of a field.

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

  • What is the syntax to provide a field form template
  • Where to put the template file ( which directory )
  • What the template have to looks like.
like image 873
qdelettre Avatar asked Jan 07 '13 10:01

qdelettre


People also ask

What is form rendering?

This form is used to export the currently displayed graphics scene to an image file or to a geometric scene description file suitable for use by one of several external renderers, which can produce a final image.

What is Form_widget?

form_widget(form_view, variables)Renders the HTML widget of a given field. If you apply this to an entire form or collection of fields, each underlying form row will be rendered.


2 Answers

Found a solution

What i have done is:

  1. Created a field type, lets 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';
        }
    }
    
  2. Registered the Type in app/config/services.yml

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

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

    and

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

    and the 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 form field value by the twig variable "value" !

So easy... when you got it.

like image 153
qdelettre Avatar answered Sep 24 '22 18:09

qdelettre


user1254498's solution won't work unless the block name prefix matches the name of the form type. At least with the last version of sonata admin bundle (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, regarding getFormTheme(), you shoud return also the parent theme, otherwise you may break the whole style...

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

Also, you can access the admin service in the twig template with the variable sonata_admin.admim.

like image 37
devilcius Avatar answered Sep 21 '22 18:09

devilcius