advance.rst 5.8 KB

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