list_field_definition.rst 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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\Datagrid\ListMapper;
  11. class PostAdmin extends Admin
  12. {
  13. protected $list = array(
  14. 'title' => array('identifier' => true), // add edit link
  15. 'enabled',
  16. 'tags',
  17. 'summary',
  18. );
  19. protected function configureListFields(ListMapper $list) // optional
  20. {
  21. // or equivalent to :
  22. $list->add('title', array('identifier' => true));
  23. $list->add('enabled');
  24. $list->add('tags);
  25. $list->add('summary');
  26. }
  27. }
  28. As you can see, the list fields are defined by overriding the ``list`` property
  29. and giving each definition an array of options. You can also customize each
  30. field further by overriding the ``configureListFields()`` method, which is
  31. blank in the parent class.
  32. Types available
  33. ---------------
  34. The most important option for each field is the ``type``: The available
  35. types include:
  36. * boolean
  37. * datetime
  38. * decimal
  39. * identifier
  40. * integer
  41. * many_to_one : a link will be added to the related edit action
  42. * string
  43. * text
  44. * date
  45. If no type is set, the ``Admin`` class will use the type defined in the doctrine
  46. mapping definition.
  47. List Actions
  48. ------------
  49. You can set actions for the list items by adding an '_action' field in $list:
  50. .. code-block:: php
  51. '_action' => array(
  52. 'actions' => array(
  53. 'delete' => array(),
  54. 'edit' => array()
  55. )
  56. )
  57. Edit and delete actions are enabled in the default configuration. You can add
  58. your own! Default template file is: ``SonataAdminBundle:CRUD:list__action_[ACTION_NAME].html.twig``
  59. You can specify your own by setting up the 'template' option like so:
  60. .. code-block:: php
  61. '_action' => array(
  62. 'actions' => array(
  63. 'delete' => array('template' => 'MyBundle:MyController:my_partial.html.twig'),
  64. 'edit' => array()
  65. )
  66. )
  67. Advance Usage
  68. -------------
  69. If you need a specific layout for a row cell, you can define a custom template
  70. .. code-block:: php
  71. class MediaAdmin extends Admin
  72. {
  73. protected $list = array(
  74. 'custom' => array('template' => 'SonataMediaBundle:MediaAdmin:list_custom.html.twig', 'type' => 'string'),
  75. 'enabled',
  76. )
  77. }
  78. The related template :
  79. .. code-block:: jinja
  80. {% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
  81. {% block field%}
  82. <div>
  83. <strong>{{ object.name }}</strong> <br />
  84. {{ object.providername}} : {{ object.width }}x{{ object.height }} <br />
  85. </div>
  86. {% endblock %}