extensions.rst 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. Extensions
  2. ==========
  3. Admin extensions allow you to add or change features of one or more Admin instances. To create an extension your class
  4. must implement the interface ``Sonata\AdminBundle\Admin\AdminExtensionInterface`` and be registered as a service. The
  5. interface defines a number of functions which you can use to customize the edit form, list view, form validation,
  6. alter newly created objects and other admin features.
  7. .. code-block:: php
  8. use Sonata\AdminBundle\Admin\AbstractAdminExtension;
  9. use Sonata\AdminBundle\Form\FormMapper;
  10. class PublishStatusAdminExtension extends AbstractAdminExtension
  11. {
  12. public function configureFormFields(FormMapper $formMapper)
  13. {
  14. $formMapper
  15. ->add('status', 'choice', array(
  16. 'choices' => array(
  17. 'draft' => 'Draft',
  18. 'published' => 'Published',
  19. ),
  20. ))
  21. ;
  22. }
  23. }
  24. Configuration
  25. ~~~~~~~~~~~~~
  26. There are two ways to configure your extensions and connect them to an admin.
  27. You can include this information in the service definition of your extension.
  28. Add the tag *sonata.admin.extension* and use the *target* attribute to point to
  29. the admin you want to modify. Please note you can specify as many tags you want.
  30. Set the *global* attribute to *true* and the extension will be added to all admins.
  31. .. configuration-block::
  32. .. code-block:: yaml
  33. services:
  34. app.publish.extension:
  35. class: AppBundle\Admin\Extension\PublishStatusAdminExtension
  36. tags:
  37. - { name: sonata.admin.extension, target: app.admin.article }
  38. - { name: sonata.admin.extension, target: app.admin.blog }
  39. app.order.extension:
  40. class: AppBundle\Admin\Extension\OrderAdminExtension
  41. tags:
  42. - { name: sonata.admin.extension, global: true }
  43. The second option is to add it to your config.yml file.
  44. .. configuration-block::
  45. .. code-block:: yaml
  46. # app/config/config.yml
  47. sonata_admin:
  48. extensions:
  49. app.publish.extension:
  50. admins:
  51. - app.admin.article
  52. Using the ``config.yml`` file has some advantages, it allows you to keep your configuration centralized and it provides some
  53. extra options you can use to wire your extensions in a more dynamic way. This means you can change the behaviour of all
  54. admins that manage a class of a specific type.
  55. admins:
  56. specify one or more admin service ids to which the Extension should be added
  57. excludes:
  58. specify one or more admin service ids to which the Extension should not be added (this will prevent it matching
  59. any of the other settings)
  60. extends:
  61. specify one or more classes. If the managed class of an admin extends one of the specified classes the extension
  62. will be added to that admin.
  63. implements:
  64. specify one or more interfaces. If the managed class of an admin implements one of the specified interfaces the
  65. extension will be added to that admin.
  66. instanceof:
  67. specify one or more classes. If the managed class of an admin extends one of the specified classes or is an instance
  68. of that class the extension will be added to that admin.
  69. uses:
  70. Requires PHP >= 5.4.0. Specify one or more traits. If the managed class of an admin uses one of the specified traits the extension will be
  71. added to that admin.
  72. .. configuration-block::
  73. .. code-block:: yaml
  74. # app/config/config.yml
  75. sonata_admin:
  76. extensions:
  77. app.publish.extension:
  78. admins:
  79. - app.admin.article
  80. implements:
  81. - AppBundle\Publish\PublishStatusInterface
  82. excludes:
  83. - app.admin.blog
  84. - app.admin.news
  85. extends:
  86. - AppBundle\Document\Blog
  87. instanceof:
  88. - AppBundle\Document\Page
  89. uses:
  90. - AppBundle\Trait\Timestampable