Customize form field rendering

爱⌒轻易说出口 提交于 2019-11-30 07:27:00
user1254498

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.

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.

In your services.yml file you define the template for your edit 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 that template you can then 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 that:

{% 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, my countries check boxes appear in column of 60 elements, not in one column with the whole list of elements.

Hope this is helpful to someone else.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!