advance.rst 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. Advance
  2. =======
  3. Service Configuration
  4. ---------------------
  5. When you create a new Admin service you can configure its dependencies, the services which are injected by default are:
  6. ========================= =============================================
  7. Dependencies Service Id
  8. ========================= =============================================
  9. model_manager sonata.admin.manager.%manager-type%
  10. form_contractor sonata.admin.builder.%manager-type%_form
  11. show_builder sonata.admin.builder.%manager-type%_show
  12. list_builder sonata.admin.builder.%manager-type%_list
  13. datagrid_builder sonata.admin.builder.%manager-type%_datagrid
  14. translator translator
  15. configuration_pool sonata.admin.pool
  16. router router
  17. validator validator
  18. security_handler sonata.admin.security.handler
  19. menu_factory knp_menu.factory
  20. router_builder sonata.admin.route.path_info
  21. label_translator_strategy sonata.admin.label.strategy.form_component
  22. ========================= =============================================
  23. Note: %manager-type% is to be replaced by the manager type (orm, doctrine_mongodb...)
  24. You have 2 ways of defining the dependencies inside a ``services.xml``.
  25. * With a tag attribute, less verbose :
  26. .. code-block:: xml
  27. <service id="acme.project.admin.security_feed" class="AcmeBundle\ProjectBundle\Admin\ProjectAdmin">
  28. <tag
  29. name="sonata.admin"
  30. manager_type="orm"
  31. group="Project"
  32. label="Project"
  33. label_translator_strategy="sonata.admin.label.strategy.native"
  34. router_builder="sonata.admin.route.path_info"
  35. />
  36. <argument />
  37. <argument>AcmeBundle\ProjectBundle\Entity\Project</argument>
  38. <argument />
  39. </service>
  40. * With a method call, more verbose
  41. .. code-block:: xml
  42. <service id="acme.project.admin.project" class="AcmeBundle\ProjectBundle\Admin\ProjectAdmin">
  43. <tag
  44. name="sonata.admin"
  45. manager_type="orm"
  46. group="Project"
  47. label="Project"
  48. />
  49. <argument />
  50. <argument>AcmeBundle\ProjectBundle\Entity\Project</argument>
  51. <argument />
  52. <call method="setLabelTranslatorStrategy">
  53. <argument type="service" id="sonata.admin.label.strategy.native" />
  54. </call>
  55. <call method="setRouterBuilder">
  56. <argument type="service" id="sonata.admin.route.path_info" />
  57. </call>
  58. </service>
  59. If you want to modify the service that is going to be injected, add the following code to your
  60. application's config file:
  61. .. code-block:: yaml
  62. # app/config/config.yml
  63. admins:
  64. sonata_admin: #method name, you can find the list in the table above
  65. sonata.order.admin.order: #id of the admin service's
  66. model_manager: sonata.order.admin.order.manager #id of the your service
  67. Admin Extension
  68. ---------------
  69. Configure the default page and ordering in the list view
  70. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  71. Configuring the default page and ordering column can simply be achieved by overriding
  72. the ``datagridValues`` array property. All three keys ``_page``, ``_sort_order`` and
  73. ``_sort_by`` can be omitted.
  74. .. code-block:: php
  75. <?php
  76. use Sonata\AdminBundle\Admin\Admin;
  77. class PageAdmin extends Admin
  78. {
  79. // ...
  80. /**
  81. * Default Datagrid values
  82. *
  83. * @var array
  84. */
  85. protected $datagridValues = array(
  86. '_page' => 1, // Display the first page (default = 1)
  87. '_sort_order' => 'DESC', // Descendant ordering (default = 'ASC')
  88. '_sort_by' => 'updated' // name of the ordered field (default = the model id field, if any)
  89. // the '_sort_by' key can be of the form 'mySubModel.mySubSubModel.myField'.
  90. );
  91. // ...
  92. }
  93. Inherited classes
  94. -----------------
  95. You can manage inherited classes by injected subclasses using the service configuration.
  96. Lets consider a base class named `Person` and its subclasses `Student` and `Teacher`:
  97. .. code-block:: xml
  98. <services>
  99. <service id="sonata.admin.person" class="YourNS\AdminBundle\Admin\PersonAdmin">
  100. <tag name="sonata.admin" manager_type="orm" group="admin" label="Person"/>
  101. <argument/>
  102. <argument>YourNS\AdminBundle\Entity\Person</argument>
  103. <argument></argument>
  104. <call method="setSubClasses">
  105. <argument type="collection">
  106. <argument key="student">YourNS\AdminBundle\Entity\Student</argument>
  107. <argument key="teacher">YourNS\AdminBundle\Entity\Teacher</argument>
  108. </argument>
  109. </call>
  110. </service>
  111. </services>
  112. You will just need to change the way forms are configured in order to take into account this new subclasses:
  113. .. code-block:: php
  114. <?php
  115. protected function configureFormFields(FormMapper $form)
  116. {
  117. $subject = $this->getSubject();
  118. $form->add('name');
  119. if ($subject instanceof Teacher) {
  120. $form->add('course', 'text');
  121. }
  122. elseif ($subject instanceof Student) {
  123. $form->add('year', 'integer');
  124. }
  125. }