getting-started.rst 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. Getting started with the Sonata Admin
  2. =====================================
  3. Here is a checklist of what is needed to create an admin interface for one Entity:
  4. **1. Setup the Sonata Admin dependencies.**
  5. See the install documentation. Remember to enable translations.
  6. **2. Setup the dependency to the ORM bundle you want to use.**
  7. Either SonataDoctrineORMAdminBundle, SonataDoctrineMongoDBAdminBundle or SonataDoctrinePhpcrAdminBundle.
  8. **3. Create an Admin class that extends the Sonata admin class**
  9. The easiest way to do this is to extend the Sonata\AdminBundle\Admin\Admin class. Here's an example from the SonataNewsBundle:
  10. ::
  11. /*
  12. * This file is part of the Sonata package.
  13. *
  14. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  15. *
  16. * For the full copyright and license information, please view the LICENSE
  17. * file that was distributed with this source code.
  18. */
  19. namespace Sonata\NewsBundle\Admin;
  20. use Sonata\AdminBundle\Admin\Admin;
  21. use Sonata\AdminBundle\Datagrid\ListMapper;
  22. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  23. use Sonata\AdminBundle\Validator\ErrorElement;
  24. use Sonata\AdminBundle\Form\FormMapper;
  25. class TagAdmin extends Admin
  26. {
  27. /**
  28. * @param \Sonata\AdminBundle\Form\FormMapper $formMapper
  29. * @return void
  30. */
  31. protected function configureFormFields(FormMapper $formMapper)
  32. {
  33. $formMapper
  34. ->add('name')
  35. ->add('enabled', null, array('required' => false))
  36. ;
  37. }
  38. /**
  39. * @param \Sonata\AdminBundle\Datagrid\DatagridMapper $datagridMapper
  40. * @return void
  41. */
  42. protected function configureDatagridFilters(DatagridMapper $datagridMapper)
  43. {
  44. $datagridMapper
  45. ->add('name')
  46. ->add('posts')
  47. ;
  48. }
  49. /**
  50. * @param \Sonata\AdminBundle\Datagrid\ListMapper $listMapper
  51. * @return void
  52. */
  53. protected function configureListFields(ListMapper $listMapper)
  54. {
  55. $listMapper
  56. ->addIdentifier('name')
  57. ->add('slug')
  58. ->add('enabled')
  59. ;
  60. }
  61. /**
  62. * @param \Sonata\AdminBundle\Validator\ErrorElement $errorElement
  63. * @param $object
  64. * @return void
  65. */
  66. public function validate(ErrorElement $errorElement, $object)
  67. {
  68. $errorElement
  69. ->with('name')
  70. ->assertMaxLength(array('limit' => 32))
  71. ->end()
  72. ;
  73. }
  74. }
  75. **4. Link the class to the dashboard.**
  76. The easiest way to do this is to create a default group in the dashboard config::
  77. dashboard_groups:
  78. default: ~
  79. **5. Create an admin service**
  80. You need to create a service for the new admin class and link it into the framework by setting the sonata.admin tag.
  81. ::
  82. <container xmlns="http://symfony.com/schema/dic/services"
  83. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  84. xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
  85. <services>
  86. <service id="sonata.admin.course" class="YourNS\AdminBundle\Admin\BlogAdmin">
  87. <tag name="sonata.admin" manager_type="orm" group="Posts" label="Blog"/>
  88. <argument />
  89. <argument>YourNS\AdminBundle\Entity\Course</argument>
  90. <argument>SonataAdminBundle:CRUD</argument>
  91. <call method="setTranslationDomain">
  92. <argument>YourNSAdminBundle</argument>
  93. </call>
  94. </service>
  95. </services>
  96. </container>
  97. That should be it!