action_show.rst 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. The Show action
  2. ===============
  3. .. note::
  4. This document is a stub representing a new work in progress. If you're reading
  5. this you can help contribute, **no matter what your experience level with Sonata
  6. is**. Check out the `issues on GitHub`_ for more information about how to get involved.
  7. This document will cover the Show action and related configuration options.
  8. Basic configuration
  9. -------------------
  10. To do:
  11. - a note about Routes and how disabling them disables the related action
  12. - a note about lifecycle events triggered by delete?
  13. - options available when adding general fields, inc custom templates
  14. - targeting submodel fields using dot-separated notation
  15. - (Note, if this is very similar to the form documentation it can be combined)
  16. Group options
  17. ~~~~~~~~~~~~~
  18. When adding a group to your show page, you may specify some options for the group itself.
  19. - ``collapsed``: unused at the moment
  20. - ``class``: the class for your group in the admin; by default, the value is set to ``col-md-12``.
  21. - ``fields``: the fields in your group (you should NOT override this unless you know what you're doing).
  22. - ``box_class``: the class for your group box in the admin; by default, the value is set to ``box box-primary``.
  23. - ``description``: to complete
  24. - ``translation_domain``: to complete
  25. To specify options, do as follow:
  26. .. code-block:: php
  27. <?php
  28. // src/AppBundle/Admin/PersonAdmin.php
  29. class PersonAdmin extends Admin
  30. {
  31. public function configureShowFields(ShowMapper $showMapper)
  32. {
  33. $showMapper
  34. ->tab('General') // the tab call is optional
  35. ->with('Addresses', array(
  36. 'class' => 'col-md-8',
  37. 'box_class' => 'box box-solid box-danger',
  38. 'description' => 'Lorem ipsum',
  39. ))
  40. ->add('title')
  41. // ...
  42. ->end()
  43. ->end()
  44. ;
  45. }
  46. Customising the query used to show the object from within your Admin class
  47. --------------------------------------------------------------------------
  48. Setting up a showAction is pretty much the same as a form, which we did in the initial setup.
  49. It is actually a bit easier, because we are only concerned with displaying information.
  50. Smile, the hard part is already done.
  51. The following is a working example of a ShowAction
  52. .. code-block:: php
  53. <?php
  54. // src/AppBundle/Admin/PostAdmin.php
  55. use Sonata\AdminBundle\Show\ShowMapper;
  56. class ClientAdmin extends Admin
  57. {
  58. protected function configureShowFields(ShowMapper $showMapper)
  59. {
  60. // here we set the fields of the ShowMapper variable,
  61. // $showMapper (but this can be called anything)
  62. $showMapper
  63. // The default option is to just display the
  64. // value as text (for boolean this will be 1 or 0)
  65. ->add('name')
  66. ->add('phone')
  67. ->add('email')
  68. // The boolean option is actually very cool
  69. // true shows a check mark and the 'yes' label
  70. // false shows a check mark and the 'no' label
  71. ->add('dateCafe', 'boolean')
  72. ->add('datePub', 'boolean')
  73. ->add('dateClub', 'boolean')
  74. ;
  75. }
  76. }
  77. Setting up a custom show template (very useful)
  78. ===============================================
  79. The first thing you need to do is define it in app/config/config/yml:
  80. .. configuration-block::
  81. .. code-block:: yaml
  82. sonata_admin:
  83. title: Acme
  84. title_logo: img/logo_small.png
  85. templates:
  86. show: AppBundle:Admin:Display_Client.html.twig
  87. Once you have defined this, Sonata Admin looks for it in the following location:
  88. ``src/AppBundle/Resources/views/Admin/Display_Client.html.twig``
  89. Now that you have told Sonata Admin where to find the template, it is time to put one in there.
  90. The recommended way to start is to copy the default template, and paste it into its new home.
  91. This ensures that you can update Sonata Admin and keep all of your hard work.
  92. The original template can be found in the following location:
  93. ``vendor/sonata-project/admin-bundle/Resources/views/CRUD/base_show.html.twig``
  94. Now that you have a copy of the default template, check to make sure it works.
  95. That's it, now go code.
  96. .. _`issues on GitHub`: https://github.com/sonata-project/SonataAdminBundle/issues/1519