extensions.rst 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 the admin you want to modify.
  27. Set the *global* attribute to *true* and the extension will be added to all admins.
  28. .. configuration-block::
  29. .. code-block:: yaml
  30. services:
  31. acme.demo.publish.extension:
  32. class: Acme\Demo\BlogBundle\Admin\Extension\PublishStatusAdminExtension
  33. tags:
  34. - { name: sonata.admin.extension, target: acme.demo.admin.article }
  35. acme.demo.order.extension:
  36. class: Acme\Demo\BlogBundle\Admin\Extension\OrderAdminExtension
  37. tags:
  38. - { name: sonata.admin.extension, global: true }
  39. The second option is to add it to your config.yml file.
  40. .. configuration-block::
  41. .. code-block:: yaml
  42. # app/config/config.yml
  43. sonata_admin:
  44. extensions:
  45. acme.demo.publish.extension:
  46. admins:
  47. - acme.demo.admin.article
  48. Using the config.yml file has some advantages, it allows you to keep your configuration centralized and it provides some
  49. extra options you can use to wire your extensions in a more dynamic way. This means you can change the behaviour of all
  50. admins that manage a class of a specific type.
  51. admins:
  52. specify one or more admin service id's to which the Extension should be added
  53. excludes:
  54. specify one or more admin service id's to which the Extension should not be added (this will prevent it matching
  55. any of the other settings)
  56. extends:
  57. specify one or more classes. If the managed class of an admin extends one of the specified classes the extension
  58. will be added to that admin.
  59. implements:
  60. specify one or more interfaces. If the managed class of an admin implements one of the specified interfaces the
  61. extension will be added to that admin.
  62. instanceof:
  63. specify one or more classes. If the managed class of an admin extends one of the specified classes or is an instance
  64. of that class the extension will be added to that admin.
  65. .. configuration-block::
  66. .. code-block:: yaml
  67. # app/config/config.yml
  68. sonata_admin:
  69. extensions:
  70. acme.demo.publish.extension:
  71. admins:
  72. - acme.demo.admin.article
  73. implements:
  74. - Acme\Demo\Publish\PublishStatusInterface
  75. excludes:
  76. - acme.demo.admin.blog
  77. - acme.demo.admin.news
  78. extends:
  79. - Acme\Demo\Document\Blog
  80. instanceof:
  81. - Acme\Demo\Document\Page