extensions.rst 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 (this will prevent it matching
  48. any of the other settings)
  49. extends:
  50. specify one or more classes. If the managed class of an admin extends one of the specified classes the extension
  51. will be added to that admin.
  52. implements:
  53. specify one or more interfaces. If the managed class of an admin implements one of the specified interfaces the
  54. extension will be added to that admin.
  55. instanceof:
  56. specify one or more classes. If the managed class of an admin extends one of the specified classes or is an instance
  57. of that class the extension will be added to that admin.
  58. .. code-block:: yaml
  59. # app/config/config.yml
  60. sonata_admin:
  61. extensions:
  62. acme.demo.publish.extension:
  63. admins:
  64. - acme.demo.admin.article
  65. implements:
  66. - Acme\Demo\Publish\PublishStatusInterface
  67. excludes:
  68. - acme.demo.admin.blog
  69. - acme.demo.admin.news
  70. extends:
  71. - Acme\Demo\Document\Blog
  72. instanceof:
  73. - Acme\Demo\Document\Page