action_show.rst 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. Customising the query used to show the object from within your Admin class
  17. --------------------------------------------------------------------------
  18. Setting up a showAction is pretty much the same as a form, which we did in the initial setup.
  19. It is actually a bit easier, because we are only concerned with displaying information.
  20. Smile, the hard part is already done.
  21. The following is a working example of a ShowAction
  22. .. code-block:: php
  23. <?php
  24. // src/Acme/DemoBundle/Admin/PostAdmin.php
  25. use Sonata\AdminBundle\Show\ShowMapper;
  26. class ClientAdmin extends Admin
  27. {
  28. protected function configureShowFields(ShowMapper $showMapper)
  29. {
  30. // Here we set the fields of the ShowMapper variable, $showMapper (but this can be called anything)
  31. $showMapper
  32. /*
  33. * The default option is to just display the value as text (for boolean this will be 1 or 0)
  34. */
  35. ->add('Name')
  36. ->add('Phone')
  37. ->add('Email')
  38. /*
  39. * The boolean option is actually very cool
  40. * - True shows a check mark and says 'yes'
  41. * - False shows an 'X' and says 'no'
  42. */
  43. ->add('DateCafe','boolean')
  44. ->add('DatePub','boolean')
  45. ->add('DateClub','boolean')
  46. ;
  47. }
  48. }
  49. Setting up a custom show template (very useful)
  50. ===============================================
  51. The first thing you need to do is define it in app/config/config/yml:
  52. .. code-block:: yaml
  53. sonata_admin:
  54. title: Acme Admin Area
  55. title_logo: img/logo_small.png
  56. templates:
  57. show: AcmeDemoBundle:Admin:Display_Client.html.twig
  58. Once you have defined this, Sonata Admin looks for it in the following location:
  59. ``src/Acme/DemoBundle/Resources/views/Admin/Display_Client.html.twig``
  60. Now that you have told Sonata Admin where to find the template, it is time to put one in there.
  61. The recommended way to start is to copy the default template, and paste it into its new home.
  62. This ensures that you can update Sonata Admin and keep all of your hard work.
  63. The original template can be found in the following location:
  64. ``vendor/sonata-project/admin-bundle/Resources/views/CRUD/base_show.html.twig``
  65. Now that you have a copy of the default template, check to make sure it works.
  66. That's it, now go code.