list_field_definition.rst 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. List field definition
  2. =====================
  3. These fields are used to display the information inside the list table.
  4. Example
  5. -------
  6. .. code-block:: php
  7. <?php
  8. namespace Sonata\NewsBundle\Admin;
  9. use Sonata\AdminBundle\Admin\Admin;
  10. use Sonata\AdminBundle\Form\FormMapper;
  11. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  12. use Sonata\AdminBundle\Datagrid\ListMapper;
  13. use Sonata\AdminBundle\Show\ShowMapper;
  14. class PostAdmin extends Admin
  15. {
  16. protected function configureListFields(ListMapper $listMapper)
  17. {
  18. $listMapper
  19. ->addIdentifier('title')
  20. ->add('author')
  21. ->add('enabled')
  22. ->add('tags')
  23. ->add('commentsEnabled')
  24. // add custom action links
  25. ->add('_action', 'actions', array(
  26. 'actions' => array(
  27. 'view' => array(),
  28. 'edit' => array(),
  29. )
  30. ))
  31. ;
  32. }
  33. }
  34. Types available
  35. ---------------
  36. The most important option for each field is the ``type``: The available
  37. types include:
  38. * boolean
  39. * datetime
  40. * decimal
  41. * identifier
  42. * integer
  43. * many_to_one : a link will be added to the related edit action
  44. * string
  45. * text
  46. * date
  47. * time
  48. If no type is set, the ``Admin`` class will use the type defined in the doctrine
  49. mapping definition.
  50. List Actions
  51. ------------
  52. You can set actions for the list items by adding an '_action' field in ``configureListFields``:
  53. .. code-block:: php
  54. <?php
  55. $listMapper->add('_action', 'actions', array(
  56. 'actions' => array(
  57. 'view' => array(),
  58. 'edit' => array(),
  59. )
  60. ))
  61. Edit and delete actions are enabled in the default configuration. You can add
  62. your own! Default template file is: ``SonataAdminBundle:CRUD:list__action_[ACTION_NAME].html.twig``
  63. You can specify your own by setting up the 'template' option like so:
  64. .. code-block:: php
  65. <?php
  66. $listMapper->add('_action', 'actions', array(
  67. 'actions' => array(
  68. 'view' => array(),
  69. 'edit' => array(),
  70. 'delete' => array('template' => 'MyBundle:MyController:my_partial.html.twig'),
  71. )
  72. ))
  73. Advance Usage
  74. -------------
  75. Displaying sub entity properties
  76. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  77. If you need to display only one field from a sub entity in a dedicated column,
  78. you can simply use the dot-separated notation (note that this only makes sense
  79. when the prefix path is made of entities, not collections):
  80. .. code-block:: php
  81. <?php
  82. namespace Acme\AcmeBundle\Admin;
  83. use Sonata\AdminBundle\Admin\Admin;
  84. use Sonata\AdminBundle\Form\FormMapper;
  85. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  86. use Sonata\AdminBundle\Datagrid\ListMapper;
  87. use Sonata\AdminBundle\Show\ShowMapper;
  88. class UserAdmin extends Admin
  89. {
  90. protected function configureListFields(ListMapper $listMapper)
  91. {
  92. $listMapper
  93. ->addIdentifier('id')
  94. ->addIdentifier('firstName')
  95. ->addIdentifier('lastName')
  96. ->addIdentifier('address.street')
  97. ->addIdentifier('address.ZIPCode')
  98. ->addIdentifier('address.town')
  99. ;
  100. }
  101. }
  102. Custom template
  103. ^^^^^^^^^^^^^^^
  104. If you need a specific layout for a row cell, you can define a custom template
  105. .. code-block:: php
  106. <?php
  107. namespace Sonata\MediaBundle\Admin;
  108. use Sonata\AdminBundle\Admin\Admin;
  109. use Sonata\AdminBundle\Form\FormMapper;
  110. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  111. use Sonata\AdminBundle\Datagrid\ListMapper;
  112. use Sonata\AdminBundle\Show\ShowMapper;
  113. class MediaAdmin extends Admin
  114. {
  115. protected function configureListFields(ListMapper $listMapper)
  116. {
  117. $listMapper
  118. ->addIdentifier('id')
  119. ->add('image', 'string', array('template' => 'SonataMediaBundle:MediaAdmin:list_image.html.twig'))
  120. ->add('custom', 'string', array('template' => 'SonataMediaBundle:MediaAdmin:list_custom.html.twig'))
  121. ;
  122. }
  123. }
  124. The related template :
  125. .. code-block:: jinja
  126. {% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
  127. {% block field%}
  128. <div>
  129. <strong>{{ object.name }}</strong> <br />
  130. {{ object.providername}} : {{ object.width }}x{{ object.height }} <br />
  131. </div>
  132. {% endblock %}