preview_mode.rst 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. Preview Mode
  2. ============
  3. Preview Mode is an optional view of an object before it is persisted or updated.
  4. The preview step can be enabled for an admin entity by overriding the public property
  5. ``$supportsPreviewMode`` and setting it to true.
  6. .. code-block:: php
  7. <?php // MyAdmin.php
  8. class MyAdmin extends Admin
  9. {
  10. public $supportsPreviewMode = true;
  11. /* ... */
  12. }
  13. This will show a new button during create/edit mode named preview.
  14. .. figure:: ../images/preview_mode_button.png
  15. :align: center
  16. :alt: Preview Button
  17. While in preview mode two buttons will be shown to approve or decline persistence of the
  18. entity. Decline will send you back to the edit mode with all your changes unpersisted but
  19. still in the form so no data is lost and the entity can be further adjusted.
  20. Accepting the preview will store the entity as if the preview step was never there.
  21. .. figure:: ../images/preview_show.png
  22. :align: center
  23. :alt: Preview Button
  24. Simulating front-end rendering
  25. ------------------------------
  26. Preview can be used to render how the object would look like in your front-end environment.
  27. However by default it uses a template similar to the one of the show action and works with
  28. the fields configured to be shown in the show view.
  29. Overriding the preview template (SonataAdminBundle:CRUD:preview.html.twig) can be done either
  30. globally through the template configuration for the key 'preview':
  31. .. configuration-block::
  32. .. code-block:: yaml
  33. # app/config/config.yml
  34. sonata_admin:
  35. templates:
  36. preview: AcmeDemoBundle:CRUD:preview.html.twig
  37. Or per admin entity by overriding the getTemplate($name) and returning the appropriate template when the key
  38. matches 'preview':
  39. .. code-block:: php
  40. <?php // MyAdmin.php
  41. public function getTemplate($name)
  42. {
  43. switch ($name) {
  44. case 'preview':
  45. return 'AcmeDemoBundle:CRUD:preview.html.twig';
  46. break;
  47. default:
  48. return parent::getTemplate($name);
  49. break;
  50. }
  51. }
  52. In either way the template should be extending your own layout, injecting the form in it
  53. and finally overriding the action buttons to show the approve/decline buttons like the
  54. default preview.html.twig.
  55. The entity is passed to the view in a variable called **object**. If your original view expects
  56. a different object you can just set your own variables prior to calling parent().
  57. .. code-block:: jinja
  58. {# 'AcmeDemoBundle:CRUD:preview.html.twig #}
  59. {% extends 'AcmeDemoBundle::layout.html.twig' %}
  60. {% use 'SonataAdminBundle:CRUD:base_edit_form.html.twig' with form as parentForm %}
  61. {% block templateContent %} {# a block in 'AcmeDemoBundle::layout.html.twig' expecting article #}
  62. {% set article = object %}
  63. {{ parent() }}
  64. <div class="sonata-preview-form">
  65. {{ block('parentForm') }}
  66. </div>
  67. {% endblock %}
  68. {% block formactions %}
  69. <input class="btn btn-success" type="submit" name="btn_preview_approve" value="{{ 'btn_preview_approve'|trans({}, 'SonataAdminBundle') }}"/>
  70. <input class="btn btn-danger" type="submit" name="btn_preview_decline" value="{{ 'btn_preview_decline'|trans({}, 'SonataAdminBundle') }}"/>
  71. {% endblock %}
  72. Keep in mind that the whole edit form will now appear in your view.
  73. Hiding the fieldset tags with css (display:none) will be enough to only show the buttons
  74. (which still have to be styled according to your wishes) and create a nice preview-workflow:
  75. .. code-block:: css
  76. div.sonata-preview-form fieldset {
  77. display: none;
  78. };
  79. Or if you prefer less:
  80. .. code-block:: sass
  81. div.sonata-preview-form {
  82. fieldset {
  83. display: none;
  84. };
  85. }