advance.rst 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 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. route_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="setRouteBuilder">
  56. <argument type="service" id="sonata.admin.route.path_info" />
  57. </call>
  58. </service>
  59. If you want to create your own RouteBuilder, you can do it using code like
  60. * xml service registration
  61. .. code-block:: xml
  62. <service id="acme.admin.route.entity" class="Acme\AdminBundle\Route\EntityRouterBuilder">
  63. <argument type="service" id="sonata.admin.audit.manager" />
  64. </service>
  65. * php Route Generator
  66. .. code-block:: php
  67. <?php
  68. namespace Acme\AdminBundle\Route;
  69. use Sonata\AdminBundle\Builder\RouteBuilderInterface;
  70. use Sonata\AdminBundle\Admin\AdminInterface;
  71. use Sonata\AdminBundle\Model\AuditManagerInterface;
  72. use Sonata\AdminBundle\Route\PathInfoBuilder;
  73. use Sonata\AdminBundle\Route\RouteCollection;
  74. class EntityRouterBuilder extends PathInfoBuilder implements RouteBuilderInterface
  75. {
  76. /**
  77. * @param \Sonata\AdminBundle\Admin\AdminInterface $admin
  78. * @param \Sonata\AdminBundle\Route\RouteCollection $collection
  79. */
  80. public function build(AdminInterface $admin, RouteCollection $collection)
  81. {
  82. parent::build($admin,$collection);
  83. $collection->add('yourSubAction');
  84. }
  85. }
  86. If you want to modify the service that is going to be injected, add the following code to your
  87. application's config file:
  88. .. code-block:: yaml
  89. # app/config/config.yml
  90. admins:
  91. sonata_admin: #method name, you can find the list in the table above
  92. sonata.order.admin.order: #id of the admin service's
  93. model_manager: sonata.order.admin.order.manager #id of the your service
  94. Admin Extension
  95. ---------------
  96. Configure the default page and ordering in the list view
  97. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  98. Configuring the default page and ordering column can simply be achieved by overriding
  99. the ``datagridValues`` array property. All three keys ``_page``, ``_sort_order`` and
  100. ``_sort_by`` can be omitted.
  101. .. code-block:: php
  102. <?php
  103. use Sonata\AdminBundle\Admin\Admin;
  104. class PageAdmin extends Admin
  105. {
  106. // ...
  107. /**
  108. * Default Datagrid values
  109. *
  110. * @var array
  111. */
  112. protected $datagridValues = array(
  113. '_page' => 1, // Display the first page (default = 1)
  114. '_sort_order' => 'DESC', // Descendant ordering (default = 'ASC')
  115. '_sort_by' => 'updated' // name of the ordered field (default = the model id field, if any)
  116. // the '_sort_by' key can be of the form 'mySubModel.mySubSubModel.myField'.
  117. );
  118. // ...
  119. }
  120. Inherited classes
  121. -----------------
  122. You can manage inherited classes by injected subclasses using the service configuration.
  123. Lets consider a base class named `Person` and its subclasses `Student` and `Teacher`:
  124. .. code-block:: xml
  125. <services>
  126. <service id="sonata.admin.person" class="YourNS\AdminBundle\Admin\PersonAdmin">
  127. <tag name="sonata.admin" manager_type="orm" group="admin" label="Person"/>
  128. <argument/>
  129. <argument>YourNS\AdminBundle\Entity\Person</argument>
  130. <argument></argument>
  131. <call method="setSubClasses">
  132. <argument type="collection">
  133. <argument key="student">YourNS\AdminBundle\Entity\Student</argument>
  134. <argument key="teacher">YourNS\AdminBundle\Entity\Teacher</argument>
  135. </argument>
  136. </call>
  137. </service>
  138. </services>
  139. You will just need to change the way forms are configured in order to take into account this new subclasses:
  140. .. code-block:: php
  141. <?php
  142. protected function configureFormFields(FormMapper $form)
  143. {
  144. $subject = $this->getSubject();
  145. $form->add('name');
  146. if ($subject instanceof Teacher) {
  147. $form->add('course', 'text');
  148. }
  149. elseif ($subject instanceof Student) {
  150. $form->add('year', 'integer');
  151. }
  152. }