extensions.rst 3.3 KB

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