list_field_definition.rst 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. If you need a specific layout for a row cell, you can define a custom template
  76. .. code-block:: php
  77. <?php
  78. namespace Sonata\MediaBundle\Admin;
  79. use Sonata\AdminBundle\Admin\Admin;
  80. use Sonata\AdminBundle\Form\FormMapper;
  81. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  82. use Sonata\AdminBundle\Datagrid\ListMapper;
  83. use Sonata\AdminBundle\Show\ShowMapper;
  84. class MediaAdmin extends Admin
  85. {
  86. protected function configureListFields(ListMapper $listMapper)
  87. {
  88. $listMapper
  89. ->addIdentifier('id')
  90. ->add('image', 'string', array('template' => 'SonataMediaBundle:MediaAdmin:list_image.html.twig'))
  91. ->add('custom', 'string', array('template' => 'SonataMediaBundle:MediaAdmin:list_custom.html.twig'))
  92. ;
  93. }
  94. }
  95. The related template :
  96. .. code-block:: jinja
  97. {% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
  98. {% block field%}
  99. <div>
  100. <strong>{{ object.name }}</strong> <br />
  101. {{ object.providername}} : {{ object.width }}x{{ object.height }} <br />
  102. </div>
  103. {% endblock %}