advance.rst 6.1 KB

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