advance.rst 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. Configure the default page and ordering in the list view
  102. --------------------------------------------------------
  103. Configuring the default page and ordering column can simply be achieved by overriding
  104. the ``datagridValues`` array property. All three keys ``_page``, ``_sort_order`` and
  105. ``_sort_by`` can be omitted.
  106. .. code-block:: php
  107. <?php
  108. use Sonata\AdminBundle\Admin\Admin;
  109. class PageAdmin extends Admin
  110. {
  111. // ...
  112. /**
  113. * Default Datagrid values
  114. *
  115. * @var array
  116. */
  117. protected $datagridValues = array(
  118. '_page' => 1, // display the first page (default = 1)
  119. '_sort_order' => 'DESC', // reverse order (default = 'ASC')
  120. '_sort_by' => 'updated' // name of the ordered field
  121. // (default = the model's id field, if any)
  122. // the '_sort_by' key can be of the form 'mySubModel.mySubSubModel.myField'.
  123. );
  124. // ...
  125. }
  126. Inherited classes
  127. -----------------
  128. You can manage inherited classes by injecting subclasses using the service configuration.
  129. Lets consider a base class named `Person` and its subclasses `Student` and `Teacher`:
  130. .. code-block:: xml
  131. <services>
  132. <service id="sonata.admin.person" class="YourNS\AdminBundle\Admin\PersonAdmin">
  133. <tag name="sonata.admin" manager_type="orm" group="admin" label="Person"/>
  134. <argument/>
  135. <argument>YourNS\AdminBundle\Entity\Person</argument>
  136. <argument></argument>
  137. <call method="setSubClasses">
  138. <argument type="collection">
  139. <argument key="student">YourNS\AdminBundle\Entity\Student</argument>
  140. <argument key="teacher">YourNS\AdminBundle\Entity\Teacher</argument>
  141. </argument>
  142. </call>
  143. </service>
  144. </services>
  145. You will just need to change the way forms are configured in order to take into account this new subclasses:
  146. .. code-block:: php
  147. <?php
  148. // YourNS\AdminBundle\Admin\PersonAdmin.php
  149. protected function configureFormFields(FormMapper $form)
  150. {
  151. $subject = $this->getSubject();
  152. $form->add('name');
  153. if ($subject instanceof Teacher) {
  154. $form->add('course', 'text');
  155. }
  156. elseif ($subject instanceof Student) {
  157. $form->add('year', 'integer');
  158. }
  159. }