Sonata管理包:从嵌入管理类访问子编辑路径

时间:2014-02-28 14:48:06

标签: symfony routes sonata-admin

我有些头疼,我想,重新制作轮子。

在奏鸣曲管理包中,我正在尝试对所有管理实体进行逻辑导航。

每个实体都是级联相关的:programEntity - > LevelEntity - > ExerciceEntity - > somethingEntity - >等等

根据我的阅读,sonata admin捆绑句柄(希望这将改变)父母和子管理类之间只有一个嵌入关系。

能够编辑/列出父母的子女并返回仪表板编辑/列出父母的孙子,事实上并不是非常用户友好

我正在尝试通过从编辑路线中删除父路径来制作自定义路线以编辑parend的孩子:

http://localhost/domain/admin/acme/app/parent/4/child/3/edit

我想用这样的直接替换这个网址:

http://localhost/domain/admin/acme/app/child/3/edit

通过这种方式,我可以从父母选择的孩子那里获得大孩子。

我试图从sonata adminClass覆盖generateObjectUrl和generateUrl但没有成功,我还想到为我的每个实体覆盖模板,但它不是非常便于导致。

任何想法?

1 个答案:

答案 0 :(得分:1)

我能够找到解决这个问题的方法:

1)覆盖你自己的bundle中的base_list_field(复制你自己的bundle中的vendor \ sonata-project \ admin-bundle \ Sonata \ AdminBundle \ Resources \ views \ CRUD \ base_list_field.html.twig中的文件,如acme / bundle /资源/观点/ CRUD)

2)将文件修改为如下所示:

{#

This file is part of the Sonata package.

 (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>

 For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.

 #}

 <td class="sonata-ba-list-field sonata-ba-list-field-{{ field_description.type }}"     objectId="{{ admin.id(object) }}">
    {% if
        field_description.options.identifier is defined
    and field_description.options.route is defined
    and admin.isGranted(field_description.options.route.name == 'show' ? 'VIEW' : field_description.options.route.name|upper, object)
    and admin.hasRoute(field_description.options.route.name)
%}

<script type="text/javascript">
    var route = "{{ admin.generateObjectUrl(field_description.options.route.name, object, field_description.options.route.parameters) }}";
    var url = route.split(/(app)\//);
    var tabUrl = url[2].split ( "/" );
    alert ( 'test' ) ;

    var new_url = '';

    // case where adminclass is a child
    if ( tabUrl.length == 5 )
    {
        new_url = url[0] + url[1] + '/' + tabUrl[2] + '/' + tabUrl[3] + '/' +  tabUrl[4] ;
    }

    // case where adminclass is not a child
    if ( tabUrl.length == 3 )
    {
        new_url = "{{ admin.generateObjectUrl(field_description.options.route.name, object, field_description.options.route.parameters) }}";
    }

    document.write( {%raw%}"<a href='"{%endraw%} + new_url + {%raw%}"'>{%endraw%}{%- block field %}{{ value }}{% endblock -%}{%raw%}</a>"{%endraw%} );
 </script>
{% else %}
    {{ block('field') }}
{% endif %}

如果tabUrl(whitch是带有'/'的url的分割)找到5个结果,则表示我们在adminclass中嵌入,否则它是正常的管理类列表。

代码不是很干净和优化但是有效。

3)更新你的config.yml

sonata_admin:
title: Bonk
title_logo: public/img/logo-admin.png
security:
    handler: sonata.admin.security.handler.noop
templates:
    # default global templates
    layout:  SonataAdminBundle::standard_layout.html.twig
    ajax:    SonataAdminBundle::ajax_layout.html.twig
    dashboard: SonataAdminBundle:Core:dashboard.html.twig

    # default actions templates, should extend a global templates
    list:    SonataAdminBundle:CRUD:list.html.twig
    show:    SonataAdminBundle:CRUD:show.html.twig
    edit:    SonataAdminBundle:CRUD:edit.html.twig

    ------>>>  base_list_field: AcmeMyBundle:CRUD:base_list_field.html.twig

sonata_doctrine_orm_admin:
# default value is null, so doctrine uses the value defined in the configuration
entity_manager: '@doctrine.orm.entity_manager'

templates:
    form:
        - SonataDoctrineORMAdminBundle:Form:form_admin_fields.html.twig
    filter:
    - SonataDoctrineORMAdminBundle:Form:filter_admin_fields.html.twig
    types:
        list:
            array:      SonataAdminBundle:CRUD:list_array.html.twig
            boolean:    SonataAdminBundle:CRUD:list_boolean.html.twig
            date:       SonataAdminBundle:CRUD:list_date.html.twig
            time:       SonataAdminBundle:CRUD:list_time.html.twig
            datetime:   SonataAdminBundle:CRUD:list_datetime.html.twig
            text:       acmeMyBundle:CRUD:base_list_field.html.twig
            trans:      SonataAdminBundle:CRUD:list_trans.html.twig
            string:     acmeMyBundle:CRUD:base_list_field.html.twig
            smallint:   acmeMyBundle:CRUD:base_list_field.html.twig
            bigint:     acmeMyBundle:CRUD:base_list_field.html.twig
            integer:    acmeMyBundle:CRUD:base_list_field.html.twig
            decimal:    acmeMyBundle:CRUD:base_list_field.html.twig
            identifier: acmeMyBundle:CRUD:base_list_field.html.twig
希望这会有所帮助!