extensions.rst 4.2 KB

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