自定义SonataAdminBundle base_edit_form.html.twig

时间:2017-02-03 22:26:39

标签: symfony sonata-admin

我想以Sonata Admin Bundle

生成的形式显示地图

这是我的formMapper,

class PlaceInfoAdmin extends Admin
{
    public function configureFormFields(FormMapper $formMapper)
    {
    $formMapper
        ->with('General')
        ->add('lati')
        ->add('longi')
        ->add('name')

我正在使用象牙谷歌地图API。

简化我想做的事,

  1. 将此代码放在PlaceInfoAdmin

    使用的最后一个模板中

    {{ivory_google_api([     data.map,     data.form.field.vars [ '自动完成'] ])}}

  2. 将此代码放在longiname之间。

    {{ivory_google_map(data.map)}}

  3. 现在我研究了twig文件,发现可能是由base_edit_form.html.twig呈现的形式,

    但是我应该在哪里改变?或者如何才能找到PlaceInfoAdmin调用这个树枝? (不是由另一个**** Admin类)

    {% block form %}
        {{ sonata_block_render_event('sonata.admin.edit.form.top', { 'admin': admin, 'object': object }) }}
    
        {% set url = admin.id(object) is not null ? 'edit' : 'create' %}
    
        {% if not admin.hasRoute(url)%}
            <div>
                {{ "form_not_available"|trans({}, "SonataAdminBundle") }}
            </div>
        {% else %}
            <form
                  {% if sonata_admin.adminPool.getOption('form_type') == 'horizontal' %}class="form-horizontal"{% endif %}
                  role="form"
                  action="{% block sonata_form_action_url %}{{ admin.generateUrl(url, {'id': admin.id(object), 'uniqid': admin.uniqid, 'subclass': app.request.get('subclass')}) }}{% endblock %}"
                  {% if form.vars.multipart %} enctype="multipart/form-data"{% endif %}
                  method="POST"
                  {% if not sonata_admin.adminPool.getOption('html5_validate') %}novalidate="novalidate"{% endif %}
                  {% block sonata_form_attributes %}{% endblock %}
                  >
    
                {{ include('SonataAdminBundle:Helper:render_form_dismissable_errors.html.twig') }}
    
                {% block sonata_pre_fieldsets %}
                    <div class="row">
                {% endblock %}
    
                {% block sonata_tab_content %}
                    {% set has_tab = ((admin.formtabs|length == 1 and admin.formtabs|keys[0] != 'default') or admin.formtabs|length > 1 ) %}
    
                    <div class="col-md-12">
                        {% if has_tab %}
                            <div class="nav-tabs-custom">
                                <ul class="nav nav-tabs" role="tablist">
                                    {% for name, form_tab in admin.formtabs %}
                                        <li{% if loop.index == 1 %} class="active"{% endif %}><a href="#tab_{{ admin.uniqid }}_{{ loop.index }}" data-toggle="tab"><i class="fa fa-exclamation-circle has-errors hide" aria-hidden="true"></i> {{ name|trans({}, form_tab.translation_domain ?: admin.translationDomain) }}</a></li>
                                    {% endfor %}
                                </ul>
                                <div class="tab-content">
                                    {% for code, form_tab in admin.formtabs %}
                                        <div class="tab-pane fade{% if loop.first %} in active{% endif %}" id="tab_{{ admin.uniqid }}_{{ loop.index }}">
                                            <div class="box-body  container-fluid">
                                                <div class="sonata-ba-collapsed-fields">
                                                    {% if form_tab.description != false %}
                                                        <p>{{ form_tab.description|raw }}</p>
                                                    {% endif %}
    
                                                    {{ form_helper.render_groups(admin, form, form_tab['groups'], has_tab) }}
                                                </div>
                                            </div>
                                        </div>
                                    {% endfor %}
                                </div>
                            </div>
                        {% else %}
                            {{ form_helper.render_groups(admin, form, admin.formtabs['default'].groups, has_tab) }}
                        {% endif %}
                    </div>
                {% endblock %}
    
                {% block sonata_post_fieldsets %}
                    </div>
                {% endblock %}
    
                {{ form_rest(form) }}
    
                {% block formactions %}
                    <div class="sonata-ba-form-actions well well-small form-actions">
                    {% block sonata_form_actions %}
                        {% if app.request.isxmlhttprequest %}
                            {% if admin.id(object) is not null %}
                                <button type="submit" class="btn btn-success" name="btn_update"><i class="fa fa-save" aria-hidden="true"></i> {{ 'btn_update'|trans({}, 'SonataAdminBundle') }}</button>
                            {% else %}
                                <button type="submit" class="btn btn-success" name="btn_create"><i class="fa fa-plus-circle" aria-hidden="true"></i> {{ 'btn_create'|trans({}, 'SonataAdminBundle') }}</button>
                            {% endif %}
                        {% else %}
                            {% if admin.supportsPreviewMode %}
                                <button class="btn btn-info persist-preview" name="btn_preview" type="submit">
                                    <i class="fa fa-eye" aria-hidden="true"></i>
                                    {{ 'btn_preview'|trans({}, 'SonataAdminBundle') }}
                                </button>
                            {% endif %}
                            {% if admin.id(object) is not null %}
                                <button type="submit" class="btn btn-success" name="btn_update_and_edit"><i class="fa fa-save" aria-hidden="true"></i> {{ 'btn_update_and_edit_again'|trans({}, 'SonataAdminBundle') }}</button>
    
                                {% if admin.hasroute('list') and admin.isGranted('LIST') %}
                                    <button type="submit" class="btn btn-success" name="btn_update_and_list"><i class="fa fa-save"></i> <i class="fa fa-list" aria-hidden="true"></i> {{ 'btn_update_and_return_to_list'|trans({}, 'SonataAdminBundle') }}</button>
                                {% endif %}
    
                                {% if admin.hasroute('delete') and admin.isGranted('DELETE', object) %}
                                    {{ 'delete_or'|trans({}, 'SonataAdminBundle') }}
                                    <a class="btn btn-danger" href="{{ admin.generateObjectUrl('delete', object) }}"><i class="fa fa-minus-circle" aria-hidden="true"></i> {{ 'link_delete'|trans({}, 'SonataAdminBundle') }}</a>
                                {% endif %}
    
                                {% if admin.isAclEnabled() and admin.hasroute('acl') and admin.isGranted('MASTER', object) %}
                                    <a class="btn btn-info" href="{{ admin.generateObjectUrl('acl', object) }}"><i class="fa fa-users" aria-hidden="true"></i> {{ 'link_edit_acl'|trans({}, 'SonataAdminBundle') }}</a>
                                {% endif %}
                            {% else %}
                                {% if admin.hasroute('edit') and admin.isGranted('EDIT') %}
                                    <button class="btn btn-success" type="submit" name="btn_create_and_edit"><i class="fa fa-save" aria-hidden="true"></i> {{ 'btn_create_and_edit_again'|trans({}, 'SonataAdminBundle') }}</button>
                                {% endif %}
                                {% if admin.hasroute('list') and admin.isGranted('LIST') %}
                                    <button type="submit" class="btn btn-success" name="btn_create_and_list"><i class="fa fa-save"></i> <i class="fa fa-list" aria-hidden="true"></i> {{ 'btn_create_and_return_to_list'|trans({}, 'SonataAdminBundle') }}</button>
                                {% endif %}
                                <button class="btn btn-success" type="submit" name="btn_create_and_create"><i class="fa fa-plus-circle" aria-hidden="true"></i> {{ 'btn_create_and_create_a_new_one'|trans({}, 'SonataAdminBundle') }}</button>
                            {% endif %}
                        {% endif %}
                    {% endblock %}
                    </div>
                {% endblock formactions %}
            </form>
        {% endif%}
    
        {{ sonata_block_render_event('sonata.admin.edit.form.bottom', { 'admin': admin, 'object': object }) }}
    
    {% endblock %}
    {% endblock %}
    

1 个答案:

答案 0 :(得分:0)

创建扩展base_edit.html.twig的模板:

{% extends 'SonataAdminBundle:CRUD:base_edit.html.twig' %}

{% block form %}

    {# your custom code #}
    {{ parent() }}

{% endblock %}

然后确保只有您想要的管理员使用该模板通过setTemplate调用将其传递到管理服务的定义中:

librinfo_crm.admin.contact:
        class: Librinfo\CRMBundle\Admin\ContactAdmin
        arguments: [~, Librinfo\CRMBundle\Entity\Contact, SonataAdminBundle:CRUD]
        tags:
            -   name: sonata.admin
                manager_type: orm
                group: Customers Relationship Management
                label: librinfo.crm.contact_admin.label
                label_translator_strategy: blast_core.label.strategy.librinfo
        calls:                    
            - [ setTemplate, [edit, LibrinfoCRMBundle:OrganismAdmin:edit.html.twig]]