用于预过滤列表的Sonata管理员操作按钮 [英] Sonata Admin Action Button to Pre Filtered List

查看:84
本文介绍了用于预过滤列表的Sonata管理员操作按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Symfony相当陌生(2周),所以请原谅我的无知。

I am rather new to Symfony (2 weeks) so forgive my ignorance.

我试图添加一个自定义操作按钮,该按钮将链接到预先过滤的列表相关实体的名称。我做了很多研究,但似乎找不到我需要的东西。

I am trying to add a custom action button that will link to a pre filtered list of a RELATED entity. I have done a lot of research but can't quite seem to find what I need.

目前,我有两个实体书籍和作者,并且具有manyToOne关系。
我已经按照通常的方式在Sonata Admin中设置了这些,并且一切正常。我什至希望在图书清单页面上有一个作者过滤器,希望可以利用它来实现我的目标。

Currently I have two entities Books and Authors with a manyToOne relation ship. I have these set up in Sonata Admin in the usual way and all works well. I even have an author filter on the book list page which I am hoping can be leveraged to accomplish my goal.

在作者列表视图中,我想添加一个查看和编辑旁边的每一行上的操作按钮,称为按作者查看图书。我可以使用该按钮,但无法正确构建URL。

In the Author list view, I would like to add an action button on each row next to View and Edit, called "View Books By Author". I can get the button but fail to correctly build the URL.

路由存在3个问题:

1)我正在尝试在按钮模板中使用admin.generateObjectUrl()或类似内容来干净地构建管理URL,但无法获取替代实体的路径。就我而言,由于我当前正在查看作者,因此链接始终指向作者实体,而不是我想要的书。

1) I am trying to use admin.generateObjectUrl() or similar in my button template to cleanly build an admin URL but can't get a path to an alternate entity. In my case, since I am currently viewing authors, the links always point to the author entity not books as I would like.

2)我不确定如何获得为了将其传递给过滤器,当前作者的ID。

2) I am uncertain how to get the id of the current author in order to pass it to the filters

3)我不确定如何干净地构建过滤器查询字符串参数。如有必要,我可以手动进行操作:bookEntityListPath +?filter [author] [value] [] = + $ authorID
但是,显然这不是那么干净,如果可能的话,我希望使用更好的方法。

3) I am uncertain how to cleanly build the filter query string parameters. I could do it by hand if necessary: bookEntityListPath + "?filter[author][value][]=" + $authorID But obviously this is not that clean and I would prefer a better method if possible.

预先感谢!

推荐答案

我遇到了同样的问题,当我找不到答案时,便开始尝试一些代码。使用path()树枝方法(路由到路径转换),创建这样的链接非常容易。

I had the same issue and started trying out some code when I could not find an answer. Using the path() twig method (route to path conversion) it is quite easy to create a link like that.

请参阅以下文档:http://symfony.com/doc/current/book/routing.html#generating-urls-from -a-template

您的链接看起来像这样:

Your link would look something like this:

<a href="{{ path('admin_application_book_list', {
  'filter[author][value]': object.id  
}) }}" class="btn btn-small">

其中'admin_application_book_list'是书籍列表的路径(bookEntityListPath?),而object.id是您作者的ID(在该实体的详细信息或编辑模板中具有名称对象)

With 'admin_application_book_list' being the path to the list of books (bookEntityListPath?) and object.id being the id of your author (would have the name object in a detail or edit template of that entity)

这篇关于用于预过滤列表的Sonata管理员操作按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆