list_field_definition.rst 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. If no type is set, the ``Admin`` class will use the type defined in the doctrine
  48. mapping definition.
  49. List Actions
  50. ------------
  51. You can set actions for the list items by adding an '_action' field in ``configureListFields``:
  52. .. code-block:: php
  53. <?php
  54. $listMapper->add('_action', 'actions', array(
  55. 'actions' => array(
  56. 'view' => array(),
  57. 'edit' => array(),
  58. )
  59. ))
  60. Edit and delete actions are enabled in the default configuration. You can add
  61. your own! Default template file is: ``SonataAdminBundle:CRUD:list__action_[ACTION_NAME].html.twig``
  62. You can specify your own by setting up the 'template' option like so:
  63. .. code-block:: php
  64. <?php
  65. $listMapper->add('_action', 'actions', array(
  66. 'actions' => array(
  67. 'view' => array(),
  68. 'edit' => array(),
  69. 'delete' => array('template' => 'MyBundle:MyController:my_partial.html.twig'),
  70. )
  71. ))
  72. Advance Usage
  73. -------------
  74. If you need a specific layout for a row cell, you can define a custom template
  75. .. code-block:: php
  76. <?php
  77. namespace Sonata\MediaBundle\Admin;
  78. use Sonata\AdminBundle\Admin\Admin;
  79. use Sonata\AdminBundle\Form\FormMapper;
  80. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  81. use Sonata\AdminBundle\Datagrid\ListMapper;
  82. use Sonata\AdminBundle\Show\ShowMapper;
  83. class MediaAdmin extends Admin
  84. {
  85. protected function configureListFields(ListMapper $listMapper)
  86. {
  87. $listMapper
  88. ->addIdentifier('id')
  89. ->add('image', 'string', array('template' => 'SonataMediaBundle:MediaAdmin:list_image.html.twig'))
  90. ->add('custom', 'string', array('template' => 'SonataMediaBundle:MediaAdmin:list_custom.html.twig'))
  91. ;
  92. }
  93. }
  94. The related template :
  95. .. code-block:: jinja
  96. {% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
  97. {% block field%}
  98. <div>
  99. <strong>{{ object.name }}</strong> <br />
  100. {{ object.providername}} : {{ object.width }}x{{ object.height }} <br />
  101. </div>
  102. {% endblock %}