advanced.rst 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. Advanced configuration
  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. route_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 ``services.xml``:
  25. * With a tag attribute, less verbose:
  26. .. configuration-block::
  27. .. code-block:: xml
  28. <service id="acme.project.admin.project" class="Acme\ProjectBundle\Admin\ProjectAdmin">
  29. <tag
  30. name="sonata.admin"
  31. manager_type="orm"
  32. group="Project"
  33. label="Project"
  34. label_translator_strategy="sonata.admin.label.strategy.native"
  35. route_builder="sonata.admin.route.path_info"
  36. />
  37. <argument />
  38. <argument>Acme\ProjectBundle\Entity\Project</argument>
  39. <argument />
  40. </service>
  41. .. configuration-block::
  42. .. code-block:: yaml
  43. acme.project.admin.project:
  44. class: Acme\ProjectBundle\Admin\ProjectAdmin
  45. tags:
  46. - name: sonata.admin
  47. manager_type: orm
  48. group: "Project"
  49. label: "Project"
  50. label_translator_strategy: "sonata.admin.label.strategy.native"
  51. route_builder: "sonata.admin.route.path_info"
  52. arguments:
  53. - ~
  54. - Acme\ProjectBundle\Entity\Project
  55. - ~
  56. * With a method call, more verbose
  57. .. configuration-block::
  58. .. code-block:: xml
  59. <service id="acme.project.admin.project" class="Acme\ProjectBundle\Admin\ProjectAdmin">
  60. <tag
  61. name="sonata.admin"
  62. manager_type="orm"
  63. group="Project"
  64. label="Project"
  65. />
  66. <argument />
  67. <argument>Acme\ProjectBundle\Entity\Project</argument>
  68. <argument />
  69. <call method="setLabelTranslatorStrategy">
  70. <argument type="service" id="sonata.admin.label.strategy.native" />
  71. </call>
  72. <call method="setRouteBuilder">
  73. <argument type="service" id="sonata.admin.route.path_info" />
  74. </call>
  75. </service>
  76. .. configuration-block::
  77. .. code-block:: yaml
  78. acme.project.admin.project:
  79. class: Acme\ProjectBundle\Admin\ProjectAdmin
  80. tags:
  81. - { name: sonata.admin, manager_type: orm, group: "Project", label: "Project" }
  82. arguments:
  83. - ~
  84. - Acme\ProjectBundle\Entity\Project
  85. - ~
  86. calls:
  87. - [ setLabelTranslatorStrategy, [ @sonata.admin.label.strategy.native ]]
  88. - [ setRouteBuilder, [ @sonata.admin.route.path_info ]]
  89. If you want to modify the service that is going to be injected, add the following code to your
  90. application's config file:
  91. .. configuration-block::
  92. .. code-block:: yaml
  93. # app/config/config.yml
  94. admins:
  95. sonata_admin:
  96. sonata.order.admin.order: # id of the admin service this setting is for
  97. model_manager: # dependency name, from the table above
  98. sonata.order.admin.order.manager # customised service id
  99. Creating a custom RouteBuilder
  100. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  101. To create your own RouteBuilder create the PHP class and register it as a service:
  102. * php Route Generator
  103. .. code-block:: php
  104. <?php
  105. namespace Acme\AdminBundle\Route;
  106. use Sonata\AdminBundle\Builder\RouteBuilderInterface;
  107. use Sonata\AdminBundle\Admin\AdminInterface;
  108. use Sonata\AdminBundle\Route\PathInfoBuilder;
  109. use Sonata\AdminBundle\Route\RouteCollection;
  110. class EntityRouterBuilder extends PathInfoBuilder implements RouteBuilderInterface
  111. {
  112. /**
  113. * @param \Sonata\AdminBundle\Admin\AdminInterface $admin
  114. * @param \Sonata\AdminBundle\Route\RouteCollection $collection
  115. */
  116. public function build(AdminInterface $admin, RouteCollection $collection)
  117. {
  118. parent::build($admin,$collection);
  119. $collection->add('yourSubAction');
  120. // Create button will dissappear, delete functionality will be disabled as well
  121. // No more changes needed!
  122. $collection->remove('create');
  123. $collection->remove('delete');
  124. }
  125. }
  126. * xml service registration
  127. .. configuration-block::
  128. .. code-block:: xml
  129. <service id="acme.admin.route.entity" class="Acme\AdminBundle\Route\EntityRouterBuilder">
  130. <argument type="service" id="sonata.admin.audit.manager" />
  131. </service>
  132. * YAML service registration
  133. .. configuration-block::
  134. .. code-block:: yaml
  135. parameters:
  136. acme.admin.entity_route_builder.class: Acme\AdminBundle\Route\EntityRouterBuilder
  137. services:
  138. acme.admin.entity_route_builder:
  139. class: %acme.admin.entity_route_builder.class%
  140. arguments:
  141. - @sonata.admin.audit.manager
  142. Inherited classes
  143. -----------------
  144. You can manage inherited classes by injecting subclasses using the service configuration.
  145. Lets consider a base class named `Person` and its subclasses `Student` and `Teacher`:
  146. .. configuration-block::
  147. .. code-block:: xml
  148. <services>
  149. <service id="sonata.admin.person" class="YourNS\AdminBundle\Admin\PersonAdmin">
  150. <tag name="sonata.admin" manager_type="orm" group="admin" label="Person"/>
  151. <argument/>
  152. <argument>YourNS\AdminBundle\Entity\Person</argument>
  153. <argument></argument>
  154. <call method="setSubClasses">
  155. <argument type="collection">
  156. <argument key="student">YourNS\AdminBundle\Entity\Student</argument>
  157. <argument key="teacher">YourNS\AdminBundle\Entity\Teacher</argument>
  158. </argument>
  159. </call>
  160. </service>
  161. </services>
  162. You will just need to change the way forms are configured in order to take into account this new subclasses:
  163. .. code-block:: php
  164. <?php
  165. // YourNS\AdminBundle\Admin\PersonAdmin.php
  166. protected function configureFormFields(FormMapper $form)
  167. {
  168. $subject = $this->getSubject();
  169. $form->add('name');
  170. if ($subject instanceof Teacher) {
  171. $form->add('course', 'text');
  172. }
  173. elseif ($subject instanceof Student) {
  174. $form->add('year', 'integer');
  175. }
  176. }