recipe_knp_menu.rst 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. KnpMenu
  2. =======
  3. The admin comes with `KnpMenu`_ integration.
  4. It integrates a menu with the KnpMenu library. This menu can be a SonataAdmin service, a menu created with a Knp menu provider or a route of a custom controller.
  5. Add a custom controller entry in the menu
  6. -----------------------------------------
  7. To add a custom controller entry in the admin menu:
  8. Create your controller:
  9. .. code-block:: php
  10. class BlogController
  11. {
  12. /**
  13. * @Route("/blog", name="blog_home")
  14. */
  15. public function blogAction()
  16. {
  17. // ...
  18. }
  19. /**
  20. * @Route("/blog/article/{articleId}", name="blog_article")
  21. */
  22. public function ArticleAction($articleId)
  23. {
  24. // ...
  25. }
  26. }
  27. Add the controller route as an item of the menu:
  28. .. configuration-block::
  29. .. code-block:: yaml
  30. # app/config/config.yml
  31. sonata_admin:
  32. dashboard:
  33. groups:
  34. news:
  35. label: ~
  36. label_catalogue: ~
  37. items:
  38. - sonata.news.admin.post
  39. - route: blog_home
  40. label: Blog
  41. - route: blog_article
  42. route_params: { articleId: 3 }
  43. label: Article
  44. You can also override the template of knp_menu used by sonata. The default one is `SonataAdminBundle:Menu:sonata_menu.html.twig`:
  45. .. configuration-block::
  46. .. code-block:: yaml
  47. # app/config/config.yml
  48. sonata_admin:
  49. templates:
  50. knp_menu_template: ApplicationAdminBundle:Menu:custom_knp_menu.html.twig
  51. And voilà, now you have a menu group which contains a link to a sonata admin via its id, to your blog and to a specific article.
  52. Using a menu provider
  53. ---------------------
  54. As seen above, the main way to declare your menu is by declaring items in your sonata admin config file. In some case you may have to create a more complex menu depending on your business logic. This is possible by using a menu provider to populate a whole menu group. This is done with the ``provider`` config value.
  55. The following configuration uses a menu provider to populate the menu group ``my_group``:
  56. .. configuration-block::
  57. .. code-block:: yaml
  58. # app/config/config.yml
  59. sonata_admin:
  60. dashboard:
  61. groups:
  62. my_group:
  63. provider: 'MyBundle:MyMenuProvider:getMyMenu'
  64. icon: '<i class="fa fa-edit"></i>'
  65. With KnpMenuBundle you can create a custom menu by using a builder class or by declaring it as a service. Please see the `Knp documentation`_ for further information.
  66. In sonata, whatever the implementation you choose, you only have to provide the menu alias to the provider config key:
  67. * If you are using a builder class, your menu alias should be something like ``MyBundle:MyMenuProvider:getMyMenu``.
  68. * If you are using a service, your menu alias is the alias set in the ``knp_menu.menu`` tag. In the following example this is ``my_menu_alias``:
  69. .. configuration-block::
  70. .. code-block:: xml
  71. <service id="my_menu_provider" class="MyBundle/MyDirectory/MyMenuProvider">
  72. <tag name="knp_menu.menu" alias="my_menu_alias" />
  73. </service>
  74. Please note that when using the provider option, you can't set the menu label via the configuration. It is done in your custom menu.
  75. Extending the menu
  76. ------------------
  77. You can modify the menu via events easily. You can register as many listeners as you want for the event with name ``sonata.admin.event.configure.menu.sidebar``:
  78. .. code-block:: php
  79. <?php
  80. // src/AppBundle/EventListener/MenuBuilderListener.php
  81. namespace AppBundle\EventListener;
  82. use Sonata\AdminBundle\Event\ConfigureMenuEvent;
  83. class MenuBuilderListener
  84. {
  85. public function addMenuItems(ConfigureMenuEvent $event)
  86. {
  87. $menu = $event->getMenu();
  88. $child = $menu->addChild('reports', array(
  89. 'route' => 'app_reports_index',
  90. 'labelAttributes' => array('icon' => 'fa fa-bar-chart'),
  91. ));
  92. $child->setLabel('Daily and monthly reports');
  93. }
  94. }
  95. .. configuration-block::
  96. .. code-block:: yaml
  97. # src/AppBundle/Resources/config/services.yml
  98. services:
  99. app.menu_listener:
  100. class: AppBundle\EventListener\MenuBuilderListener
  101. tags:
  102. - { name: kernel.event_listener, event: sonata.admin.event.configure.menu.sidebar, method: addMenuItems }
  103. Please see the `Using events to allow a menu to be extended`_ for further information.
  104. Hiding menu items
  105. -----------------
  106. You can modify the menu to hide some menu items. You need to add the ``show_in_dashboard`` option in
  107. your admin services or simply remove menu items from the ``sonata_admin`` dashboard group configuration:
  108. .. code-block:: yaml
  109. sonata_admin.admin.post:
  110. class: Sonata\AdminBundle\Admin\PostAdmin
  111. arguments: [~, Sonata\AdminBundle\Entity\Post, SonataAdminBundle:CRUD]
  112. tags:
  113. - {name: sonata.admin, manager_type: orm, group: admin, label: Post, show_in_dashboard: false}
  114. .. code-block:: yaml
  115. # app/config/config.yml
  116. sonata_admin:
  117. dashboard:
  118. groups:
  119. news:
  120. label: ~
  121. label_catalogue: ~
  122. items:
  123. # just comment or remove the sonata.news.admin.post declaration to hide it from the menu.
  124. # - sonata.news.admin.post
  125. - route: blog_home
  126. label: Blog
  127. - sonata.news.admin.news
  128. Show menu item without treeview
  129. -------------------------------
  130. You can modify the menu to show menu item without treeview. You need to add option ``on_top`` in your admin services
  131. or in sonata_admin dashboard group configuration:
  132. .. code-block:: yaml
  133. sonata_admin.admin.post:
  134. class: Sonata\AdminBundle\Admin\PostAdmin
  135. arguments: [~, Sonata\AdminBundle\Entity\Post, SonataAdminBundle:CRUD]
  136. tags:
  137. - {name: sonata.admin, manager_type: orm, group: admin, label: Post, on_top: true}
  138. .. code-block:: yaml
  139. # app/config/config.yml
  140. sonata_admin:
  141. dashboard:
  142. groups:
  143. news:
  144. on_top: true
  145. label: ~
  146. label_catalogue: ~
  147. items:
  148. - sonata.news.admin.post
  149. .. figure:: ../images/demo_on_top.png
  150. :align: center
  151. :alt: on_top option
  152. :width: 500
  153. In this screenshot, we add ``on_top`` option to ``Tag`` and ``Blog Post`` admin services.
  154. Your can't use this option for two or more items in the same time, for example:
  155. .. code-block:: yaml
  156. # app/config/config.yml
  157. sonata_admin:
  158. dashboard:
  159. groups:
  160. news:
  161. on_top: true
  162. label: ~
  163. label_catalogue: ~
  164. items:
  165. - sonata.news.admin.post
  166. - route: blog_home
  167. label: Blog
  168. In this case you have an exception: "You can't use ``on_top`` option with multiple same name groups".
  169. .. _KnpMenu: https://github.com/KnpLabs/KnpMenu
  170. .. _Knp documentation: http://symfony.com/doc/current/bundles/KnpMenuBundle/index.html#create-your-first-menu
  171. .. _Using events to allow a menu to be extended: http://symfony.com/doc/master/bundles/KnpMenuBundle/events.html