dashboard.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. Dashboard
  2. =========
  3. The Dashboard is the main landing page. By default it lists your mapped models,
  4. as defined by your ``Admin`` services. This is useful to help you start using
  5. ``SonataAdminBundle`` right away, but there is much more that you can do to take
  6. advantage of the Dashboard.
  7. The Dashboard is, by default, available at ``/admin/dashboard``, which is handled by
  8. the ``SonataAdminBundle:Core:dashboard`` controller action. The default view file for
  9. this action is ``SonataAdminBundle:Core:dashboard.html.twig``, but you can change
  10. this in your ``config.yml``:
  11. .. configuration-block::
  12. .. code-block:: yaml
  13. # app/config/config.yml
  14. sonata_admin:
  15. templates:
  16. dashboard: SonataAdminBundle:Core:dashboard.html.twig
  17. .. note::
  18. This view, like most of the ``SonataAdminBundle`` views, extends a global
  19. template file, which also contains significant parts to the page. More information
  20. about this is available in the :doc:`templates` chapter.
  21. Blocks
  22. ------
  23. The Dashboard is actually built using ``Blocks`` from ``SonataBlockBundle``. You
  24. can learn more about this bundle and how to build your own Blocks on the
  25. `SonataBlock documentation page`_.
  26. The ``Admin`` list block
  27. ------------------------
  28. The ``Admin`` list is a ``Block`` that fetches information from the ``Admin`` service's
  29. ``Pool`` and prints it in the nicely formatted list you have on your default Dashboard.
  30. The ``Admin`` list is defined by the ``sonata.admin.block.admin_list`` service, which is
  31. implemented by the ``Block\AdminListBlockService`` class. It is then rendered using the
  32. ``SonataAdminBundle:Block:block_admin_list.html.twig`` template file.
  33. Feel free to take a look at these files. You'll find the code rather short and easy to
  34. understand, and it will be a great help when implementing your own blocks.
  35. Configuring the ``Admin`` list
  36. ------------------------------
  37. As you probably noticed by now, the ``Admin`` list groups ``Admin`` mappings together.
  38. There are several ways in which you can configure these groups.
  39. By default the admins are ordered the way you defined them. With the setting ``sort_admins``
  40. groups and admins will be ordered by their respective label with a fallback to the admin id.
  41. Using the ``Admin`` service declaration
  42. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  43. The first, and most commonly used, method is to set a group when defining your ``Admin``
  44. services:
  45. .. configuration-block::
  46. .. code-block:: xml
  47. <service id="app.admin.post" class="AppBundle\Admin\PostAdmin">
  48. <tag name="sonata.admin" manager_type="orm"
  49. group="Content"
  50. label="Post" />
  51. <argument />
  52. <argument>AppBundle\Entity\Post</argument>
  53. <argument />
  54. </service>
  55. .. code-block:: yaml
  56. services:
  57. app.admin.post:
  58. class: AppBundle\Admin\PostAdmin
  59. tags:
  60. - name: sonata.admin
  61. manager_type: orm
  62. group: "Content"
  63. label: "Post"
  64. arguments:
  65. - ~
  66. - AppBundle\Entity\Post
  67. - ~
  68. In these examples, notice the ``group`` tag, stating that this particular ``Admin``
  69. service belongs to the ``Content`` group.
  70. .. configuration-block::
  71. .. code-block:: xml
  72. <service id="app.admin.post" class="AppBundle\Admin\PostAdmin">
  73. <tag name="sonata.admin" manager_type="orm"
  74. group="app.admin.group.content"
  75. label="app.admin.model.post" label_catalogue="AppBundle" />
  76. <argument />
  77. <argument>AppBundle\Entity\Post</argument>
  78. <argument />
  79. </service>
  80. .. code-block:: yaml
  81. services:
  82. app.admin.post:
  83. class: AppBundle\Admin\PostAdmin
  84. tags:
  85. - name: sonata.admin
  86. manager_type: orm
  87. group: "app.admin.group.content"
  88. label: "app.admin.model.post"
  89. label_catalogue: "AppBundle"
  90. arguments:
  91. - ~
  92. - AppBundle\Entity\Post
  93. - ~
  94. In this example, the labels are translated by ``AppBundle``, using the given
  95. ``label_catalogue``. So, you can use the above examples to support multiple languages
  96. in your project.
  97. .. note::
  98. You can use parameters (e.g. ``%app_admin.group_post%``) for the group names
  99. in either scenario.
  100. Using the ``config.yml``
  101. ^^^^^^^^^^^^^^^^^^^^^^^^
  102. You can also configure the ``Admin`` list in your ``config.yml`` file. This
  103. configuration method overrides any settings defined in the Admin service
  104. declarations.
  105. .. configuration-block::
  106. .. code-block:: yaml
  107. # app/config/config.yml
  108. sonata_admin:
  109. dashboard:
  110. groups:
  111. app.admin.group.content:
  112. label: app.admin.group.content
  113. label_catalogue: AppBundle
  114. items:
  115. - app.admin.post
  116. app.admin.group.blog:
  117. items: ~
  118. item_adds:
  119. - sonata.admin.page
  120. roles: [ ROLE_ONE, ROLE_TWO ]
  121. app.admin.group.misc: ~
  122. .. note::
  123. This is an academic, full configuration, example. In real cases, you will usually
  124. not need to use all the displayed options. To use a default value for any setting
  125. either leave out that key or use the ``~`` value for that option.
  126. This configuration specifies that the ``app.admin.group.content`` group uses the
  127. ``app.admin.group.content`` label, which is translated using the ``AppBundle``
  128. translation catalogue (the same label and translation configuration that we declared
  129. previously, in the service definition example).
  130. It also states that the ``app.admin.group.content`` group contains just the
  131. ``app.admin.post`` ``Admin`` mapping, meaning that any other ``Admin`` services
  132. declared as belonging to this group will not be displayed here.
  133. Secondly, we declare a ``app.admin.group.blog`` group as having all its default items
  134. (i.e. the ones specified in the ``Admin`` service declarations), plus an *additional*
  135. ``sonata.admin.page`` mapping, that was not initially part of this group.
  136. We also use the ``roles`` option here, which means that only users with the ``ROLE_ONE``
  137. or ``ROLE_TWO`` privileges will be able to see this group, as opposed to the default setting
  138. which allows everyone to see a given group. Users with ``ROLE_SUPER_ADMIN`` are always
  139. able to see groups that would otherwise be hidden by this configuration option.
  140. The third group, ``app.admin.group.misc``, is set up as a group which uses all its
  141. default values, as declared in the service declarations.
  142. Adding more Blocks
  143. ------------------
  144. Like we said before, the Dashboard comes with a default ``Admin`` list block, but
  145. you can create and add more blocks to it.
  146. .. figure:: ../images/dashboard.png
  147. :align: center
  148. :alt: Dashboard
  149. :width: 500
  150. In this screenshot, in addition to the default ``Admin`` list block on the left, we added
  151. a text block and RSS feed block on the right. The configuration for this scenario would be:
  152. .. configuration-block::
  153. .. code-block:: yaml
  154. # app/config/config.yml
  155. sonata_admin:
  156. dashboard:
  157. blocks:
  158. -
  159. position: left
  160. type: sonata.admin.block.admin_list
  161. -
  162. position: right
  163. type: sonata.block.service.text
  164. settings:
  165. content: >
  166. <h2>Welcome to the Sonata Admin</h2>
  167. <p>This is a <code>sonata.block.service.text</code> from the Block
  168. Bundle, you can create and add new block in these area by configuring
  169. the <code>sonata_admin</code> section.</p> <br /> For instance, here
  170. a RSS feed parser (<code>sonata.block.service.rss</code>):
  171. -
  172. position: right
  173. type: sonata.block.service.rss
  174. roles: [POST_READER]
  175. settings:
  176. title: Sonata Project's Feeds
  177. url: https://sonata-project.org/blog/archive.rss
  178. .. note::
  179. Blocks may accept/require additional settings to be passed in order to
  180. work properly. Refer to the associated documentation/implementation to
  181. get more information on each block's options and requirements.
  182. You can also configure the ``roles`` section to configure users that can
  183. view the block.
  184. Display two ``Admin`` list blocks with different dashboard groups
  185. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  186. The same block can have multiple instances, and be displayed multiple times
  187. across the Dashboard using different configuration settings for each instance.
  188. A particular example is the ``Admin`` list block, which can be configured to
  189. suit this scenario.
  190. .. configuration-block::
  191. .. code-block:: yaml
  192. # app/config/config.yml
  193. sonata_admin:
  194. dashboard:
  195. blocks:
  196. # display two dashboard blocks
  197. -
  198. position: left
  199. type: sonata.admin.block.admin_list
  200. settings:
  201. groups: [sonata_page1, sonata_page2]
  202. -
  203. position: right
  204. type: sonata.admin.block.admin_list
  205. settings:
  206. groups: [sonata_page3]
  207. groups:
  208. sonata_page1:
  209. items:
  210. - sonata.page.admin.myitem1
  211. sonata_page2:
  212. items:
  213. - sonata.page.admin.myitem2
  214. - sonata.page.admin.myitem3
  215. sonata_page3:
  216. items:
  217. - sonata.page.admin.myitem4
  218. In this example, you would have two ``admin_list`` blocks on your dashboard, each
  219. of them containing just the respectively configured groups.
  220. .. _`SonataBlock documentation page`: https://sonata-project.org/bundles/block/master/doc/index.html
  221. Statistic Block
  222. ~~~~~~~~~~~~~~~
  223. A statistic block can be used to display a simple counter with a color, an font awesome icon and a text. A
  224. counter is related to the filters from one admin
  225. .. configuration-block::
  226. .. code-block:: yaml
  227. sonata_admin:
  228. dashboard:
  229. blocks:
  230. -
  231. class: col-lg-3 col-xs-6 # twitter bootstrap responsive code
  232. position: top # zone in the dashboard
  233. type: sonata.admin.block.stats # block id
  234. settings:
  235. code: sonata.page.admin.page # admin code - service id
  236. icon: fa-magic # font awesome icon
  237. text: Edited Pages
  238. color: bg-yellow # colors: bg-green, bg-red and bg-aqua
  239. filters: # filter values
  240. edited: { value: 1 }
  241. Dashboard Layout
  242. ~~~~~~~~~~~~~~~~
  243. Supported positions right now are the following:
  244. * top
  245. * left
  246. * center
  247. * right
  248. * bottom
  249. The layout is as follows:
  250. .. code-block:: bash
  251. TOP TOP TOP
  252. LEFT CENTER RIGHT
  253. LEFT CENTER RIGHT
  254. LEFT CENTER RIGHT
  255. BOTTOM BOTTOM BOTTOM
  256. On ``top`` and ``bottom`` positions, you can also specify an optional ``class`` option to set the width of the block.
  257. .. configuration-block::
  258. .. code-block:: yaml
  259. # app/config/config.yml
  260. sonata_admin:
  261. dashboard:
  262. blocks:
  263. # display dashboard block in the top zone with a col-md-6 css class
  264. -
  265. position: top
  266. class: col-md-6
  267. type: sonata.admin.block.admin_list
  268. Configuring what actions are available for each item on the dashboard
  269. ---------------------------------------------------------------------
  270. By default. A "list" and a "create" option are available for each item on the
  271. dashboard. If you created a custom action and want to display it along the
  272. other two on the dashboard, you can do so by overriding the
  273. ``getDashboardActions()`` method of your admin class:
  274. .. code-block:: php
  275. <?php
  276. // src/AppBundle/Admin/PostAdmin.php
  277. class PostAdmin extends Admin
  278. {
  279. // ...
  280. public function getDashboardActions()
  281. {
  282. $actions = parent::getDashboardActions();
  283. $actions['import'] = array(
  284. 'label' => 'Import',
  285. 'url' => $this->generateUrl('import'),
  286. 'icon' => 'import',
  287. 'translation_domain' => 'SonataAdminBundle', // optional
  288. 'template' => 'SonataAdminBundle:CRUD:dashboard__action.html.twig', // optional
  289. );
  290. return $actions;
  291. }
  292. }
  293. You can also hide an action from the dashboard by unsetting it:
  294. .. code-block:: php
  295. <?php
  296. // src/AppBundle/Admin/PostAdmin.php
  297. class PostAdmin extends Admin
  298. {
  299. // ...
  300. public function getDashboardActions()
  301. {
  302. $actions = parent::getDashboardActions();
  303. unset($actions['list']);
  304. return $actions;
  305. }
  306. }
  307. If you do this, you need to be aware that the action is only hidden. it will
  308. still be available by directly calling its URL, unless you prevent that using
  309. proper security measures (e.g. ACL or role based).