有时希望能够自定义list页面的每一行,让它能够根据需求灵活多变。虽然有的人认为sonataadminbundle不够灵活,可能无法实现。不然,只需要简单的修改其实是可以自定义很多东西的。
模拟已经安装了sonataadminbundle,并创建了Entity(Job)和Amin/JobAdmin.php,并能够正常访问。这里我们在list页面做一些手脚。
1. 在admin.yml的service加上setTemplates
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<service id="sonata_admin.job" class="AppBundle\Admin\JobAdmin"> <tag name="sonata.admin" manager_type="orm" group="关于我们" label="招聘" /> <argument /> <argument>AppBundle\Entity\Job</argument> <argument/> <call method="setTranslationDomain"> <argument>AppBundle</argument> </call> <call method="setTemplates"> <argument type="collection"> <argument key="inner_list_row">AppBundle:Admin:list.html.twig</argument> <argument key="base_list_field">SonataAdminBundle:CRUD:base_list_flat_field.html.twig</argument> </argument> </call> </service> |
2.创建AppBundle:Admin:list.html.twig文件
文件目录src\AppBundle\Resources\views\Admin\list.html.twig
1 2 3 4 5 6 7 8 9 |
{% extends 'SonataAdminBundle:CRUD:base_list_flat_inner_row.html.twig' %} {% block row %} {# you can use fields defined in the the Admin class #} {{ object|render_list_element(admin.list['title']) }} - {% endblock %} |
3. 回到Jobadmin.php文件,找到configureListFields
添加自定义变量 testvalue
1 2 3 4 5 6 7 8 9 10 11 12 |
class JobAdmin extends Admin { public $testvalue; ... protected function configureListFields(ListMapper $listMapper) { $listMapper ->addIdentifier('title',null,['label'=>'自定义标题']) ; $this->testvalue="测试给sonataadmin传值"; } |
4. 再次回到AppBundle:Admin:list.html.twig文件
添加刚定义的 {{ admin.testvalue }}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
{% extends 'SonataAdminBundle:CRUD:base_list_flat_inner_row.html.twig' %} {% block row %} {# you can use fields defined in the the Admin class #} {{ object|render_list_element(admin.list['title']) }} - <small> {{ admin.testvalue }} </small> {% endblock %} |
5. ok!完成了