action_show.rst 5.4 KB

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