Building great admin panels with Symfony and SonataAdminBundle

Building great admin panels with Symfony and SonataAdminBundle

Coding an admin panel is not the problem you love to face when you wake up in the morning, but is something you need to do very often. Having an environment that allows you to implement this efficiently, is something key for a developer. Symfony and SonataAdminBundle provide a way to do it that gives you as many out-of-the-box functionalities as you could dream of, plus the chance to customize every piece -if you know how to do it. The purpose of this talk is to show how easily can you get profit of SonataAdminBundle once you know how to deal with it.

We will start with a clean installation of Symfony and SonataAdminBundle, showing how to build up form there, and how to solve the main problems and questions that you usually face when having a complex admin panel in your hands. The main purposes are to show how to use SonataAdminBundle in the most standard way, and how to build up from that point, customizing whatever you need to meet the exact requirements of the project.

281604c1a5357a164f2a9cd6e403b4e3?s=128

Victoria Quirante

February 04, 2017
Tweet

Transcript

  1. 2.

    I work at Limenius We build tailor-made projects with Symfony

    and React Almost every project needs an admin panel We rely on SonataAdminBundle to deal with this part Victoria Quirante @vicqr victoria@limenius.com
  2. 4.

    Some context “A bundle is simply a structured set of

    files within a directory that implement a single feature.“
  3. 5.

    Some context Released with no ‘official’ admin panel solution “A

    bundle is simply a structured set of files within a directory that implement a single feature.“
  4. 8.
  5. 9.
  6. 13.

    Main problems historically attributed - Difficult to install - Ugly

    - Poor documentation - A lot of code, difficult to dig in
  7. 14.

    Main problems historically attributed - Difficult to install - Ugly

    - Poor documentation - A lot of code, difficult to dig in
  8. 15.

    Main problems historically attributed - Difficult to install - Ugly

    - Poor documentation - A lot of code, difficult to dig in
  9. 16.

    Main problems historically attributed - Difficult to install - Ugly

    - Poor documentation can still improve - A lot of code, difficult to dig in
  10. 17.

    Main problems historically attributed - Difficult to install - Ugly

    - Poor documentation can still improve - A lot of code, difficult to dig in
  11. 18.

    Main problems historically attributed - Difficult to install - Ugly

    - Poor documentation can still improve - A lot of code, difficult to dig in But that’s why we are here
  12. 21.
  13. 22.

    What Sonata is, what it is not It is not

    Michelangelo’s David But it is something very useful
  14. 23.

    What Sonata is, what it is not It is not

    Michelangelo’s David But it is something very useful Like Symfony, like PHP
  15. 24.

    Practical talk Not an exhaustive review of the documentation A

    walk-through from a clean installation showing:
  16. 25.

    Practical talk Not an exhaustive review of the documentation A

    walk-through from a clean installation showing: 1. What Sonata gives you “for free”
  17. 26.

    Practical talk Not an exhaustive review of the documentation A

    walk-through from a clean installation showing: 1. What Sonata gives you “for free” 2. How you customize everything from that point
  18. 33.

    Install Symfony • Get the Symfony Installer • symfony new

    my_project_name • php bin/console server:run
  19. 34.

    Install Symfony • Get the Symfony Installer • symfony new

    my_project_name • php bin/console server:run http://symfony.com/doc/current/setup.html Symfony documentation is very good
  20. 37.

    Install SonataAdminBundle • composer require sonata-project/admin-bundle • composer require sonata-project/doctrine-orm-admin-bundle

    • Activate bundles • Add configuration to config.yml and routing.yml https://sonata-project.org/bundles/admin/master/doc/reference/installation.html
  21. 42.

    We need first to have an entity We create our

    first entity with Symfony (Doctrine)
  22. 43.

    We need first to have an entity We create our

    first entity with Symfony (Doctrine) php bin/console generate:doctrine:entity
  23. 44.

    We need first to have an entity /** * @ORM\Table(name="gift")

    * @ORM\Entity(repositoryClass="AppBundle\Repository\GiftRepository") */ class Gift { /** * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @ORM\Column(name="name", type="string", length=255) */ private $name; }
  24. 45.

    We need first to have an entity /** * @ORM\Table(name="gift")

    * @ORM\Entity(repositoryClass="AppBundle\Repository\GiftRepository") */ class Gift { /** * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @ORM\Column(name="name", type="string", length=255) */ private $name; }
  25. 46.

    We need first to have an entity /** * @ORM\Table(name="gift")

    * @ORM\Entity(repositoryClass="AppBundle\Repository\GiftRepository") */ class Gift { /** * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @ORM\Column(name="name", type="string", length=255) */ private $name; }
  26. 47.

    We need first to have an entity /** * @ORM\Table(name="gift")

    * @ORM\Entity(repositoryClass="AppBundle\Repository\GiftRepository") */ class Gift { /** * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @ORM\Column(name="name", type="string", length=255) */ private $name; } And now we can start working with SonataAdminBundle
  27. 49.

    Basic Admin: 2 steps, 2 minutes Have entity Gift, want

    to create Admin: 1) Create GiftAdmin class 2) Configure the service
  28. 50.

    Basic Admin: 2 steps, 2 minutes Have entity Gift, want

    to create Admin: 1) Create GiftAdmin class 2) Configure the service You may want to read about Symfony’s Service Container http://symfony.com/doc/current/service_container.html
  29. 51.

    Basic Admin: 2 steps, 2 minutes Have entity Gift, want

    to create Admin: 1) Create GiftAdmin class 2) Configure the service You may want to read about Symfony’s Service Container http://symfony.com/doc/current/service_container.html
  30. 52.

    Basic Admin - Step 1: Create Admin class # src/AppBundle/Admin/GiftAdmin.php

    class GiftAdmin extends AbstractAdmin { protected function configureFormFields(FormMapper $formMapper) { $formMapper->add('name'); } protected function configureDatagridFilters(DatagridMapper $datagridMapper) { $datagridMapper->add('name'); } protected function configureListFields(ListMapper $listMapper) { $listMapper->addIdentifier('name'); } }
  31. 53.

    Basic Admin - Step 1: Create Admin class # src/AppBundle/Admin/GiftAdmin.php

    class GiftAdmin extends AbstractAdmin { protected function configureFormFields(FormMapper $formMapper) { $formMapper->add('name'); } protected function configureDatagridFilters(DatagridMapper $datagridMapper) { $datagridMapper->add('name'); } protected function configureListFields(ListMapper $listMapper) { $listMapper->addIdentifier('name'); } }
  32. 54.

    Basic Admin - Step 1: Create Admin class # src/AppBundle/Admin/GiftAdmin.php

    class GiftAdmin extends AbstractAdmin { protected function configureFormFields(FormMapper $formMapper) { $formMapper->add('name'); } protected function configureDatagridFilters(DatagridMapper $datagridMapper) { $datagridMapper->add('name'); } protected function configureListFields(ListMapper $listMapper) { $listMapper->addIdentifier('name'); } }
  33. 55.

    Basic Admin - Step 2: Register service # app/config/services.yml services:

    admin.gift class: AppBundle\Admin\GiftAdmin arguments: [~, AppBundle\Entity\Gift, ~] tags: - { name: sonata.admin, manager_type: orm, label: Gifts }
  34. 56.

    Basic Admin - Step 2: Register service # app/config/services.yml services:

    admin.gift: class: AppBundle\Admin\GiftAdmin arguments: [~, AppBundle\Entity\Gift, ~] tags: - { name: sonata.admin, manager_type: orm, label: Gifts }
  35. 61.

    Basic Admin - Step 3 (optional): Sidebar # app/config/config.yml sonata_admin:

    dashboard: groups: demo.admin.main: label: Admin label_catalogue: AppBundle icon: '<i class="fa fa-database"></i>' items: - admin.gift
  36. 62.

    Basic Admin - Step 3 (optional): Sidebar # app/config/config.yml sonata_admin:

    dashboard: groups: demo.admin.main: label: Admin label_catalogue: AppBundle icon: '<i class="fa fa-database"></i>' items: - admin.gift
  37. 63.

    Basic Admin - Step 3 (optional): Sidebar # app/config/config.yml sonata_admin:

    dashboard: groups: demo.admin.main: label: Admin label_catalogue: AppBundle icon: '<i class="fa fa-database"></i>' items: - admin.gift
  38. 69.

    Basic Admin - What do I get? Create, edit, delete…

    Sortable, exportable, filterable, paginated list
  39. 70.

    Basic Admin - What do I get? Create, edit, delete…

    Sortable, exportable, filterable, paginated list
  40. 71.
  41. 79.

    Basic list configuration # src/AppBundle/Admin/GiftAdmin.php class GiftAdmin extends AbstractAdmin {

    protected function configureListFields(ListMapper $listMapper) { $listMapper ->add('name') ; } }
  42. 80.

    Basic list configuration # src/AppBundle/Admin/GiftAdmin.php class GiftAdmin extends AbstractAdmin {

    protected function configureListFields(ListMapper $listMapper) { $listMapper ->add('name') ->add('price') ->add('description') ->add('addressee') ->add('buyer') ; } }
  43. 81.

    Basic list configuration # src/AppBundle/Admin/GiftAdmin.php class GiftAdmin extends AbstractAdmin {

    protected function configureListFields(ListMapper $listMapper) { $listMapper ->addIdentifier('name') ->add('price') ->add('description') ->add('addressee') ->add('buyer') ; } }
  44. 82.

    Basic list configuration # src/AppBundle/Admin/GiftAdmin.php class GiftAdmin extends AbstractAdmin {

    protected function configureListFields(ListMapper $listMapper) { $listMapper ->addIdentifier('name') ->add('price', 'currency', array('currency' => 'USD')) ->add('description') ->add('addressee') ->add('buyer') ; } }
  45. 83.

    Basic list configuration # src/AppBundle/Admin/GiftAdmin.php class GiftAdmin extends AbstractAdmin {

    protected function configureListFields(ListMapper $listMapper) { $listMapper ->addIdentifier('name') ->add('price', 'currency', array('currency' => 'USD')) ->add('description', null, array('label' => 'Details')) ->add('addressee') ->add('buyer') ; } }
  46. 84.

    Basic list configuration # src/AppBundle/Admin/GiftAdmin.php class GiftAdmin extends AbstractAdmin {

    protected function configureListFields(ListMapper $listMapper) { $listMapper ->addIdentifier('name') ->add('price', 'currency', array('currency' => 'USD')) ->add('description', null, array('label' => 'Details')) ->add('addressee', null, array('editable' => true)) ->add('buyer') ; } }
  47. 85.

    Basic list configuration # src/AppBundle/Admin/GiftAdmin.php class GiftAdmin extends AbstractAdmin {

    protected function configureListFields(ListMapper $listMapper) { $listMapper ->addIdentifier('name') ->add('price', 'currency', array('currency' => 'USD')) ->add('description', null, array('label' => 'Details')) ->add('addressee', null, array('editable' => true)) ->add('buyer') ; } } https://sonata-project.org/bundles/admin/master/doc/reference/field_types.html
  48. 90.

    Basic list configuration # src/AppBundle/Admin/GiftAdmin.php class GiftAdmin extends AbstractAdmin {

    protected function configureListFields(ListMapper $listMapper) { $listMapper ... ->add('_action', null, array( 'actions' => array( 'show' => array(), 'edit' => array(), 'delete' => array(), ) )) ; } }
  49. 91.

    Basic list configuration # src/AppBundle/Admin/GiftAdmin.php class GiftAdmin extends AbstractAdmin {

    protected function configureListFields(ListMapper $listMapper) { $listMapper ... ->add('_action', null, array( 'actions' => array( 'show' => array(), 'edit' => array(), 'delete' => array(), ) )) ; } }
  50. 95.

    Basic form configuration # src/AppBundle/Admin/GiftAdmin.php class GiftAdmin extends AbstractAdmin {

    protected function configureFormFields(FormMapper $formMapper) { $formMapper ->add('name') ; } }
  51. 96.

    Basic form configuration # src/AppBundle/Admin/GiftAdmin.php class GiftAdmin extends AbstractAdmin {

    protected function configureFormFields(FormMapper $formMapper) { $formMapper ->add('name') ->add('price') ->add('description') ->add('addressee') ->add('buyer') ; } }
  52. 98.

    Basic form configuration # src/AppBundle/Admin/GiftAdmin.php class GiftAdmin extends AbstractAdmin {

    protected function configureFormFields(FormMapper $formMapper) { $formMapper ->add('name') ->add('price') ->add('description') ->add('addressee') ->add('buyer') ; } }
  53. 99.

    Basic form configuration # src/AppBundle/Admin/GiftAdmin.php class GiftAdmin extends AbstractAdmin {

    protected function configureFormFields(FormMapper $formMapper) { $formMapper ->with('Gift', array('class' => 'col-md-6')) ->add('name') ->add('price') ->add('description') ->end() ->with('Participants', array('class' => 'col-md-6')) ->add('addressee') ->add('buyer') ->end() ; } }
  54. 100.

    Basic form configuration # src/AppBundle/Admin/GiftAdmin.php class GiftAdmin extends AbstractAdmin {

    protected function configureFormFields(FormMapper $formMapper) { $formMapper ->with('Gift', array('class' => 'col-md-6')) ->add('name') ->add('price') ->add('description') ->end() ->with('Participants', array('class' => 'col-md-6')) ->add('addressee') ->add('buyer') ->end() ; } }
  55. 102.

    Basic form configuration # src/AppBundle/Admin/GiftAdmin.php protected function configureFormFields(FormMapper $formMapper) {

    $formMapper ->tab('Tab 1') ->with('Gift', array('class' => 'col-md-6')) ->add('name') ->add('price') ->add('description') ->end() ->with('Participants', array('class' => 'col-md-6')) ->add('addressee') ->add('buyer') ->end() ->end() ->tab('Tab 2') ->end() ; }
  56. 105.

    Many-to-one relations # src/AppBundle/Entity/Gift.php class Gift { … /** *

    @Column(type="string") */ private $addressee; … }
  57. 106.

    Many-to-one relations # src/AppBundle/Entity/Gift.php class Gift { … /** *

    @ORM\ManyToOne(targetEntity="Addressee") */ private $addressee; … }
  58. 107.

    Many-to-one relations # src/AppBundle/Entity/Gift.php class Gift { … /** *

    @ORM\ManyToOne(targetEntity="Addressee") */ private $addressee; … } We don’t need to do anything in our Admin class
  59. 109.

    Many-to-one relations # src/AppBundle/Admin/GiftAdmin.php class GiftAdmin extends AbstractAdmin { protected

    function configureFormFields(FormMapper $formMapper) { $formMapper ... ->with('Participants', array('class' => 'col-md-6')) ->add('addressee') ->add('buyer') ->end() ; } }
  60. 110.

    Many-to-one relations # src/AppBundle/Admin/GiftAdmin.php class GiftAdmin extends AbstractAdmin { protected

    function configureFormFields(FormMapper $formMapper) { $formMapper ... ->with('Participants', array('class' => 'col-md-6')) ->add('addressee', null) ->add('buyer', null) ->end() ; } }
  61. 111.

    Many-to-one relations # src/AppBundle/Admin/GiftAdmin.php class GiftAdmin extends AbstractAdmin { protected

    function configureFormFields(FormMapper $formMapper) { $formMapper ... ->with('Participants', array('class' => 'col-md-6')) ->add('addressee', 'entity', array( 'class' => 'AppBundle\Entity\Addressee')) ->add('buyer', 'entity', array( 'class' => 'AppBundle\Entity\Buyer')) ->end() ; } }
  62. 112.

    Many-to-one relations # src/AppBundle/Admin/GiftAdmin.php class GiftAdmin extends AbstractAdmin { protected

    function configureFormFields(FormMapper $formMapper) { $formMapper ... ->with('Participants', array('class' => 'col-md-6')) ->add('addressee', null, array( 'class' => 'AppBundle\Entity\Addressee', 'choice_label' => 'lastName')) ->add('buyer', null, array( 'class' => 'AppBundle\Entity\Buyer', 'choice_label' => 'lastName')) ->end() ; } }
  63. 113.

    Many-to-one relations # src/AppBundle/Admin/GiftAdmin.php class GiftAdmin extends AbstractAdmin { protected

    function configureFormFields(FormMapper $formMapper) { $formMapper ... ->with('Participants', array('class' => 'col-md-6')) ->add('addressee', null, array( 'class' => 'AppBundle\Entity\Addressee', 'choice_label' => 'fullName')) ->add('buyer', null, array( 'class' => 'AppBundle\Entity\Buyer', 'choice_label' => 'fullName')) ->end() ; } }
  64. 116.

    A collection in the form (one-to-many) # src/AppBundle/Admin/BuyerAdmin.php class BuyerAdmin

    extends AbstractAdmin { protected function configureFormFields(FormMapper $formMapper) { $formMapper ->with('Personal Data', array('class' => 'col-md-6')) ->add('firstName') ->add('lastName') ->end() ; } }
  65. 117.

    A collection in the form (one-to-many) # src/AppBundle/Admin/BuyerAdmin.php class BuyerAdmin

    extends AbstractAdmin { protected function configureFormFields(FormMapper $formMapper) { $formMapper ->with('Personal Data', array('class' => 'col-md-6')) ->add('firstName') ->add('lastName') ->end() ->with('Payments', array('class' => 'col-md-6')) ->add('payments', 'sonata_type_collection', array( ), array( 'edit' => 'inline', 'inline' => 'table', )) ->end(); } }
  66. 118.

    A collection in the form (one-to-many) # src/AppBundle/Admin/BuyerAdmin.php class BuyerAdmin

    extends AbstractAdmin { protected function configureFormFields(FormMapper $formMapper) { $formMapper ->with('Personal Data', array('class' => 'col-md-6')) ->add('firstName') ->add('lastName') ->end() ->with('Payments', array('class' => 'col-md-6')) ->add('payments', 'sonata_type_collection', array( ), array( 'edit' => 'inline', 'inline' => 'table', )) ->end(); } }
  67. 123.

    A many-to-many in the form # src/AppBundle/Admin/GiftAdmin.php protected function configureFormFields(FormMapper

    $formMapper) { $formMapper ... ->with('Shops', array('class' => 'col-md-6')) ->add('shops', 'sonata_type_model', array( 'by_reference' => false, 'expanded' => true, 'multiple' => true, 'label' => 'Shops') ) ->end(); }
  68. 124.

    A many-to-many in the form # src/AppBundle/Admin/GiftAdmin.php protected function configureFormFields(FormMapper

    $formMapper) { $formMapper ... ->with('Shops', array('class' => 'col-md-6')) ->add('shops', 'sonata_type_model', array( 'by_reference' => false, 'expanded' => true, 'multiple' => true, 'label' => 'Shops') ) ->end(); }
  69. 125.

    A many-to-many in the form # src/AppBundle/Admin/GiftAdmin.php protected function configureFormFields(FormMapper

    $formMapper) { $formMapper ... ->with('Shops', array('class' => 'col-md-6')) ->add('shops', 'sonata_type_model', array( 'by_reference' => false, 'expanded' => true, 'multiple' => true, 'label' => 'Shops') ) ->end(); }
  70. 132.

    Basic filters configuration # src/AppBundle/Admin/GiftAdmin.php protected function configureDatagridFilters(DatagridMapper $datagridMapper) {

    $datagridMapper ->add('name') ->add('price') ->add('addressee', null, array(), 'entity', array( 'class' => 'AppBundle\Entity\Addressee', 'choice_label' => 'fullName')) ; }
  71. 133.

    Basic filters configuration # src/AppBundle/Admin/GiftAdmin.php protected function configureDatagridFilters(DatagridMapper $datagridMapper) {

    $datagridMapper ->add('name') ->add('price') ->add('addressee', null, array(), 'entity', array( 'class' => 'AppBundle\Entity\Addressee', 'choice_label' => 'fullName')) ; } Careful with the number of arguments, different in configureFormFields
  72. 136.

    Basic Admin - How much do I get? With very

    little effort you have a lot Actually, you get most of what you need
  73. 137.

    Basic Admin - How much do I get? With very

    little effort you have a lot Actually, you get most of what you need But the world is not perfect… … and we will need more stuff
  74. 138.
  75. 139.

    And what if I want to… … have a form

    with a different layout? … add something ‘weird’ in a list field? … create a section in the sidebar that is not an Admin? … add something that has nothing to do with the admin panel?
  76. 140.

    And what if I want to… … have a form

    with a different layout? … add something ‘weird’ in a list field? … create a section in the sidebar that is not an Admin? … add something that has nothing to do with the admin panel? How much effort will that take? Wouldn’t it be easier to start from scratch?
  77. 141.
  78. 145.

    Customizing templates # app/config/config.yml sonata_admin: templates: user_block: 'SonataAdminBundle:Core:user_block.html.twig' add_block: 'SonataAdminBundle:Core:add_block.html.twig'

    layout: 'SonataAdminBundle::standard_layout.html.twig' ajax: 'SonataAdminBundle::ajax_layout.html.twig' dashboard: 'SonataAdminBundle:Core:dashboard.html.twig' search: 'SonataAdminBundle:Core:search.html.twig' list: 'SonataAdminBundle:CRUD:list.html.twig' show: 'SonataAdminBundle:CRUD:show.html.twig' show_compare: 'SonataAdminBundle:CRUD:show_compare.html.twig' edit: 'SonataAdminBundle:CRUD:edit.html.twig' ...
  79. 146.

    Customizing templates # app/config/config.yml sonata_admin: templates: user_block: 'SonataAdminBundle:Core:user_block.html.twig' add_block: 'SonataAdminBundle:Core:add_block.html.twig'

    layout: 'SonataAdminBundle::standard_layout.html.twig' ajax: 'SonataAdminBundle::ajax_layout.html.twig' dashboard: 'SonataAdminBundle:Core:dashboard.html.twig' search: 'SonataAdminBundle:Core:search.html.twig' list: 'SonataAdminBundle:CRUD:list.html.twig' show: 'SonataAdminBundle:CRUD:show.html.twig' show_compare: 'SonataAdminBundle:CRUD:show_compare.html.twig' edit: 'SonataAdminBundle:CRUD:edit.html.twig' ... https://sonata-project.org/bundles/admin/master/doc/reference/templates.html
  80. 147.

    Customizing templates # app/config/config.yml sonata_admin: templates: user_block: 'SonataAdminBundle:Core:user_block.html.twig' add_block: 'SonataAdminBundle:Core:add_block.html.twig'

    layout: 'SonataAdminBundle::standard_layout.html.twig' ajax: 'SonataAdminBundle::ajax_layout.html.twig' dashboard: 'SonataAdminBundle:Core:dashboard.html.twig' search: 'SonataAdminBundle:Core:search.html.twig' list: 'SonataAdminBundle:CRUD:list.html.twig' show: 'SonataAdminBundle:CRUD:show.html.twig' show_compare: 'SonataAdminBundle:CRUD:show_compare.html.twig' edit: 'SonataAdminBundle:CRUD:edit.html.twig' ...
  81. 148.

    Customizing templates # app/config/config.yml sonata_admin: templates: user_block: 'SonataAdminBundle:Core:user_block.html.twig' add_block: 'SonataAdminBundle:Core:add_block.html.twig'

    layout: 'SonataAdminBundle::standard_layout.html.twig' ajax: 'SonataAdminBundle::ajax_layout.html.twig' dashboard: 'SonataAdminBundle:Core:dashboard.html.twig' search: 'SonataAdminBundle:Core:search.html.twig' list: 'SonataAdminBundle:CRUD:list.html.twig' show: 'SonataAdminBundle:CRUD:show.html.twig' show_compare: 'SonataAdminBundle:CRUD:show_compare.html.twig' edit: 'AppBundle:Admin:edit.html.twig' ...
  82. 149.

    Customizing templates # app/config/config.yml sonata_admin: templates: user_block: 'SonataAdminBundle:Core:user_block.html.twig' add_block: 'SonataAdminBundle:Core:add_block.html.twig'

    layout: 'SonataAdminBundle::standard_layout.html.twig' ajax: 'SonataAdminBundle::ajax_layout.html.twig' dashboard: 'SonataAdminBundle:Core:dashboard.html.twig' search: 'SonataAdminBundle:Core:search.html.twig' list: 'SonataAdminBundle:CRUD:list.html.twig' show: 'SonataAdminBundle:CRUD:show.html.twig' show_compare: 'SonataAdminBundle:CRUD:show_compare.html.twig' edit: 'AppBundle:Admin:edit.html.twig' ... Find the original template and see what exactly you want to override
  83. 153.

    Customizing templates # vendor/sonata-project/admin-bundle/Resources/views/CRUD/edit.html.twig {% extends 'SonataAdminBundle:CRUD:base_edit.html.twig' %} # vendor/sonata-project/admin-bundle/Resources/views/CRUD/base_edit.html.twig

    {% use 'SonataAdminBundle:CRUD:base_edit_form.html.twig' with form as parentForm %} {% block form %} {{ block('parentForm') }} {% endblock %} http://twig.sensiolabs.org/doc/2.x/tags/use.html
  84. 154.

    Customizing templates # vendor/sonata-project/admin-bundle/Resources/views/CRUD/edit.html.twig {% extends 'SonataAdminBundle:CRUD:base_edit.html.twig' %} # vendor/sonata-project/admin-bundle/Resources/views/CRUD/base_edit.html.twig

    {% use 'SonataAdminBundle:CRUD:base_edit_form.html.twig' with form as parentForm %} {% block form %} {{ block('parentForm') }} {% endblock %} # vendor/sonata-project/admin-bundle/Resources/views/CRUD/base_edit_form.html.twig {% block formactions %} ... {% block formactions %}
  85. 157.

    Customizing templates Our custom edit would look like this #

    app/Resources/views/Admin/edit.html.twig {% extends 'SonataAdminBundle:CRUD:edit.html.twig' %} {% block formactions %} ... {% block formactions %}
  86. 158.

    Customizing templates Our custom edit would look like this #

    app/Resources/views/Admin/edit.html.twig {% extends 'SonataAdminBundle:CRUD:edit.html.twig' %} {% block formactions %} ... {% block formactions %}
  87. 159.

    Customizing templates Our custom edit would look like this #

    app/Resources/views/Admin/edit.html.twig {% extends 'SonataAdminBundle:CRUD:edit.html.twig' %} {% block formactions %} ... {% block formactions %} Just Twig inheritance, but important when working with Sonata
  88. 161.

    Customizing template for a single Admin # app/config/services.yml services: admin.buyer

    class: AppBundle\Admin\BuyerAdmin arguments: [~, AppBundle\Entity\Buyer, ~] tags: - { name: sonata.admin, manager_type: orm, label: Buyers }
  89. 162.

    Customizing template for a single Admin # app/config/services.yml services: admin.buyer

    class: AppBundle\Admin\BuyerAdmin arguments: [~, AppBundle\Entity\Buyer, ~] tags: - { name: sonata.admin, manager_type: orm, label: Buyers } calls: - [ setTemplate, [edit, :Admin:edit_buyer.html.twig]]
  90. 165.

    Customizing a single field on the list (step 1) #

    src/AppBundle/Admin/GiftAdmin.php protected function configureListFields(ListMapper $listMapper) { $listMapper ... ; }
  91. 166.

    Customizing a single field on the list (step 1) #

    src/AppBundle/Admin/GiftAdmin.php protected function configureListFields(ListMapper $listMapper) { $listMapper ... ->add('myField', 'string', array('template' => ':Admin:field_send_email.html.twig')) ; }
  92. 167.

    Customizing a single field on the list (step 1) #

    src/AppBundle/Admin/GiftAdmin.php protected function configureListFields(ListMapper $listMapper) { $listMapper ... ->add('myField', 'string', array('template' => ':Admin:field_send_email.html.twig')) ; }
  93. 168.

    Customizing a single field on the list (step 2) #

    app/Resources/views/Admin/field_send_email.html.twig {% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %} {% block field %} <a class="btn btn-primary btn-sm" href=""> <i class="fa fa-envelope"></i> Send </a> {% endblock %}
  94. 172.

    Customizing the list query # src/AppBundle/Admin/GiftAdmin.php public function createQuery($context =

    'list') { $query = parent::createQuery($context); $rootAlias = $query->getRootAliases()[0]; $query ->andWhere( $query->expr()->eq($rootAlias.'.delivered', ':wasDelivered')); $query->setParameter('wasDelivered', false); return $query; }
  95. 173.

    Customizing the list query # src/AppBundle/Admin/GiftAdmin.php public function createQuery($context =

    'list') { $query = parent::createQuery($context); $rootAlias = $query->getRootAliases()[0]; $query ->andWhere( $query->expr()->eq($rootAlias.'.delivered', ':wasDelivered')); $query->setParameter('wasDelivered', false); return $query; }
  96. 174.

    Customizing the list query # src/AppBundle/Admin/GiftAdmin.php public function createQuery($context =

    'list') { $query = parent::createQuery($context); $rootAlias = $query->getRootAliases()[0]; $query ->andWhere( $query->expr()->eq($rootAlias.'.delivered', ':wasDelivered')); $query->setParameter('wasDelivered', false); return $query; } http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/query-builder.html
  97. 178.

    Customizing the filter query # src/AppBundle/Admin/GiftAdmin.php protected function configureDatagridFilters(DatagridMapper $datagridMapper)

    { $datagridMapper ->add('myFilter', 'doctrine_orm_callback', array( 'callback' => function($queryBuilder, $alias, $field, $value) { if (!$value['value']) { return; } $queryBuilder->andWhere($alias.'.status != :status'); $queryBuilder->setParameter('status', 'delivered'); return true; }, 'field_type' => 'checkbox', 'label' => 'Not delivered' )); }
  98. 179.

    Customizing the filter query # src/AppBundle/Admin/GiftAdmin.php protected function configureDatagridFilters(DatagridMapper $datagridMapper)

    { $datagridMapper ->add('myFilter', 'doctrine_orm_callback', array( 'callback' => function($queryBuilder, $alias, $field, $value) { if (!$value['value']) { return; } $queryBuilder->andWhere($alias.'.status != :status'); $queryBuilder->setParameter('status', 'delivered'); return true; }, 'field_type' => 'checkbox', 'label' => 'Not delivered' )); }
  99. 180.

    Customizing the filter query # src/AppBundle/Admin/GiftAdmin.php protected function configureDatagridFilters(DatagridMapper $datagridMapper)

    { $datagridMapper ->add('myFilter', 'doctrine_orm_callback', array( 'callback' => function($queryBuilder, $alias, $field, $value) { if (!$value['value']) { return; } $queryBuilder->andWhere($alias.'.status != :status'); $queryBuilder->setParameter('status', 'delivered'); return true; }, 'field_type' => 'checkbox', 'label' => 'Not delivered' )); }
  100. 181.

    Customizing the filter query # src/AppBundle/Admin/GiftAdmin.php protected function configureDatagridFilters(DatagridMapper $datagridMapper)

    { $datagridMapper ->add('myFilter', 'doctrine_orm_callback', array( 'callback' => function($queryBuilder, $alias, $field, $value) { if (!$value['value']) { return; } $queryBuilder->andWhere($alias.'.status != :status'); $queryBuilder->setParameter('status', 'delivered'); return true; }, 'field_type' => 'checkbox', 'label' => 'Not delivered' )); }
  101. 187.

    Creating a custom action (step 1) # src/AppBundle/Controller/GiftAdminController.php use Sonata\AdminBundle\Controller\CRUDController

    as Controller; use Symfony\Component\HttpFoundation\RedirectResponse; class GiftAdminController extends Controller { public function sendEmailAction() { $regalo = $this->admin->getSubject(); $email = $regalo->getAddressee()->getEmail(); // Here code to send email $this->addFlash('sonata_flash_success', 'Email sent to '.$email); return new RedirectResponse($this->admin->generateUrl('list')); } }
  102. 188.

    Creating a custom action (step 1) # src/AppBundle/Controller/GiftAdminController.php use Sonata\AdminBundle\Controller\CRUDController

    as Controller; use Symfony\Component\HttpFoundation\RedirectResponse; class GiftAdminController extends Controller { public function sendEmailAction() { $regalo = $this->admin->getSubject(); $email = $regalo->getAddressee()->getEmail(); // Here code to send email $this->addFlash('sonata_flash_success', 'Email sent to '.$email); return new RedirectResponse($this->admin->generateUrl('list')); } }
  103. 189.

    Creating a custom action (step 2) # app/config/services.yml services: admin.gift:

    class: AppBundle\Admin\GiftAdmin arguments: [~, AppBundle\Entity\Gift, ~] tags: - { name: sonata.admin, manager_type: orm, label: Gifts } calls: - [ setTemplate, [edit, :Admin:edit_gift.html.twig]]
  104. 190.

    Creating a custom action (step 2) # app/config/services.yml services: admin.gift:

    class: AppBundle\Admin\GiftAdmin arguments: [~, AppBundle\Entity\Gift, AppBundle\GiftAdmin] tags: - { name: sonata.admin, manager_type: orm, label: Gifts } calls: - [ setTemplate, [edit, :Admin:edit_gift.html.twig]]
  105. 191.

    Creating a custom action (step 3) # src/AppBundle/Admin/GiftAdmin.php protected function

    configureRoutes(RouteCollection $collection) { $collection->add('sendEmail', $this->getRouterIdParameter().'/send-email'); }
  106. 192.

    Creating a custom action # app/Resources/views/Admin/field_send_email.html.twig {% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}

    {% block field %} <a class="btn btn-primary btn-sm" href=""> <i class="fa fa-envelope"></i> Send </a> {% endblock %}
  107. 193.

    Creating a custom action # app/Resources/views/Admin/field_send_email.html.twig {% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}

    {% block field %} <a class="btn btn-primary btn-sm" href="{{ admin.generateObjectUrl('sendEmail', object) }}"> <i class="fa fa-envelope"></i> Send </a> {% endblock %}
  108. 196.

    Creating a custom batch action (step 1) # src/AppBundle/Controller/GiftAdminController.php public

    function batchActionSendEmail($selectedModelQuery) { // Here code to send emails return new RedirectResponse($this->admin->generateUrl('list')); }
  109. 197.

    Creating a custom batch action (step 1) # src/AppBundle/Controller/GiftAdminController.php public

    function batchActionSendEmail($selectedModelQuery) { // Here code to send emails return new RedirectResponse($this->admin->generateUrl('list')); }
  110. 198.

    Creating a custom batch action (step 2) # src/AppBundle/Admin/GiftAdmin.php public

    function getBatchActions() { $actions = parent::getBatchActions(); if ($this->hasRoute('edit') && $this->isGranted('EDIT')) { $actions['send_email'] = [ 'label' => 'Send email', 'ask_confirmation' => false, ]; } return $actions; }
  111. 199.

    Creating a custom batch action (step 2) # src/AppBundle/Admin/GiftAdmin.php public

    function getBatchActions() { $actions = parent::getBatchActions(); if ($this->hasRoute('edit') && $this->isGranted('EDIT')) { $actions['send_email'] = [ 'label' => 'Send email', 'ask_confirmation' => false, ]; } return $actions; }
  112. 202.

    IV. Useful tips Or how to solve a few things

    that we need in every project
  113. 208.

    Customizing the styles # app/config/config.php sonata_admin: title: SunshinePHP title_logo: 'img/logo.png'

    assets: stylesheets: - bundles/sonatacore/vendor/bootstrap/dist/css/bootstrap.min.css - bundles/sonatacore/vendor/ionicons/css/ionicons.min.css - bundles/sonataadmin/vendor/admin-lte/dist/css/AdminLTE.min.css - bundles/sonataadmin/vendor/admin-lte/dist/css/skins/skin-black.min.css - bundles/sonataadmin/vendor/iCheck/skins/square/blue.css - bundles/sonataadmin/vendor/jqueryui/themes/base/jquery-ui.css - bundles/sonatacore/vendor/select2/select2.css - bundles/sonataadmin/css/styles.css - bundles/sonataadmin/css/layout.css - bundles/sonataadmin/css/tree.css - bundles/sonataadmin/css/colors.css …
  114. 209.

    Customizing the styles # app/config/config.php sonata_admin: title: SunshinePHP title_logo: 'img/logo.png'

    assets: stylesheets: - bundles/sonatacore/vendor/bootstrap/dist/css/bootstrap.min.css - bundles/sonatacore/vendor/ionicons/css/ionicons.min.css - bundles/sonataadmin/vendor/admin-lte/dist/css/AdminLTE.min.css - bundles/sonataadmin/vendor/admin-lte/dist/css/skins/skin-black.min.css - bundles/sonataadmin/vendor/iCheck/skins/square/blue.css - bundles/sonataadmin/vendor/jqueryui/themes/base/jquery-ui.css - bundles/sonatacore/vendor/select2/select2.css - bundles/sonataadmin/css/styles.css - bundles/sonataadmin/css/layout.css - bundles/sonataadmin/css/tree.css - bundles/sonataadmin/css/colors.css … https://sonata-project.org/bundles/admin/master/doc/reference/configuration.html
  115. 210.

    Customizing the styles # app/config/config.php sonata_admin: title: SunshinePHP title_logo: 'img/logo.png'

    assets: stylesheets: - bundles/sonatacore/vendor/bootstrap/dist/css/bootstrap.min.css - bundles/sonatacore/vendor/ionicons/css/ionicons.min.css - bundles/sonataadmin/vendor/admin-lte/dist/css/AdminLTE.min.css - bundles/sonataadmin/vendor/admin-lte/dist/css/skins/skin-black.min.css - bundles/sonataadmin/vendor/iCheck/skins/square/blue.css - bundles/sonataadmin/vendor/jqueryui/themes/base/jquery-ui.css - bundles/sonatacore/vendor/select2/select2.css - bundles/sonataadmin/css/styles.css - bundles/sonataadmin/css/layout.css - bundles/sonataadmin/css/tree.css - bundles/sonataadmin/css/colors.css - css/styles.css …
  116. 212.

    Customizing the styles # app/config/config.php sonata_admin: title: SunshinePHP title_logo: 'img/logo.png'

    assets: stylesheets: - bundles/sonatacore/vendor/bootstrap/dist/css/bootstrap.min.css - bundles/sonatacore/vendor/ionicons/css/ionicons.min.css - bundles/sonataadmin/vendor/admin-lte/dist/css/AdminLTE.min.css - bundles/sonataadmin/vendor/admin-lte/dist/css/skins/skin-black.min.css - bundles/sonataadmin/vendor/iCheck/skins/square/blue.css - bundles/sonataadmin/vendor/jqueryui/themes/base/jquery-ui.css - bundles/sonatacore/vendor/select2/select2.css - bundles/sonataadmin/css/styles.css - bundles/sonataadmin/css/layout.css - bundles/sonataadmin/css/tree.css - bundles/sonataadmin/css/colors.css - css/styles.css …
  117. 213.

    Customizing the styles # app/config/config.php sonata_admin: title: SunshinePHP title_logo: 'img/logo.png'

    assets: stylesheets: - bundles/sonatacore/vendor/bootstrap/dist/css/bootstrap.min.css - bundles/sonatacore/vendor/ionicons/css/ionicons.min.css - bundles/sonataadmin/vendor/admin-lte/dist/css/AdminLTE.min.css - bundles/sonataadmin/vendor/admin-lte/dist/css/skins/skin-black.min.css - bundles/sonataadmin/vendor/iCheck/skins/square/blue.css - bundles/sonataadmin/vendor/jqueryui/themes/base/jquery-ui.css - bundles/sonatacore/vendor/select2/select2.css - bundles/sonataadmin/css/styles.css - bundles/sonataadmin/css/layout.css - bundles/sonataadmin/css/tree.css - bundles/sonataadmin/css/colors.css - css/styles.css …
  118. 214.

    Customizing the styles # app/config/config.php sonata_admin: title: SunshinePHP title_logo: 'img/logo.png'

    assets: stylesheets: - bundles/sonatacore/vendor/bootstrap/dist/css/bootstrap.min.css - bundles/sonatacore/vendor/ionicons/css/ionicons.min.css - bundles/sonataadmin/vendor/admin-lte/dist/css/AdminLTE.min.css - bundles/sonataadmin/vendor/admin-lte/dist/css/skins/skin-black.min.css - bundles/sonataadmin/vendor/iCheck/skins/square/blue.css SonataAdminBundle uses AdminLTE, good to have a look there https://almsaeedstudio.com/
  119. 218.

    Overriding some key templates # app/config/config.php sonata_admin: templates: user_block: 'SonataAdminBundle:Core:user_block.html.twig'

    add_block: 'SonataAdminBundle:Core:add_block.html.twig' layout: 'SonataAdminBundle::standard_layout.html.twig' ajax: 'SonataAdminBundle::ajax_layout.html.twig' dashboard: 'SonataAdminBundle:Core:dashboard.html.twig' search: 'SonataAdminBundle:Core:search.html.twig' list: 'SonataAdminBundle:CRUD:list.html.twig' show: 'SonataAdminBundle:CRUD:show.html.twig' show_compare: 'SonataAdminBundle:CRUD:show_compare.html.twig' edit: 'SonataAdminBundle:CRUD:edit.html.twig' ...
  120. 219.

    Overriding some key templates # app/config/config.php sonata_admin: templates: user_block: 'SonataAdminBundle:Core:user_block.html.twig'

    add_block: 'SonataAdminBundle:Core:add_block.html.twig' layout: 'SonataAdminBundle::standard_layout.html.twig' ajax: 'SonataAdminBundle::ajax_layout.html.twig' dashboard: 'SonataAdminBundle:Core:dashboard.html.twig' search: 'SonataAdminBundle:Core:search.html.twig' list: 'SonataAdminBundle:CRUD:list.html.twig' show: 'SonataAdminBundle:CRUD:show.html.twig' show_compare: 'SonataAdminBundle:CRUD:show_compare.html.twig' edit: 'SonataAdminBundle:CRUD:edit.html.twig' ...
  121. 220.

    Overriding some key templates # app/config/config.php sonata_admin: templates: user_block: 'SonataAdminBundle:Core:user_block.html.twig'

    add_block: 'SonataAdminBundle:Core:add_block.html.twig' layout: ':Admin:layout.html.twig' ajax: 'SonataAdminBundle::ajax_layout.html.twig' dashboard: 'SonataAdminBundle:Core:dashboard.html.twig' search: 'SonataAdminBundle:Core:search.html.twig' list: 'SonataAdminBundle:CRUD:list.html.twig' show: 'SonataAdminBundle:CRUD:show.html.twig' show_compare: 'SonataAdminBundle:CRUD:show_compare.html.twig' edit: 'SonataAdminBundle:CRUD:edit.html.twig' ...
  122. 223.

    Overriding some key templates # app/config/config.php sonata_admin: templates: user_block: 'SonataAdminBundle:Core:user_block.html.twig'

    add_block: 'SonataAdminBundle:Core:add_block.html.twig' layout: ':Admin:layout.html.twig' ajax: 'SonataAdminBundle::ajax_layout.html.twig' dashboard: 'SonataAdminBundle:Core:dashboard.html.twig' search: 'SonataAdminBundle:Core:search.html.twig' list: 'SonataAdminBundle:CRUD:list.html.twig' show: 'SonataAdminBundle:CRUD:show.html.twig' show_compare: 'SonataAdminBundle:CRUD:show_compare.html.twig' edit: 'SonataAdminBundle:CRUD:edit.html.twig' ...
  123. 224.

    Overriding some key templates # app/config/config.php sonata_admin: templates: user_block: 'SonataAdminBundle:Core:user_block.html.twig'

    add_block: 'SonataAdminBundle:Core:add_block.html.twig' layout: ':Admin:layout.html.twig' ajax: 'SonataAdminBundle::ajax_layout.html.twig' dashboard: 'SonataAdminBundle:Core:dashboard.html.twig' search: 'SonataAdminBundle:Core:search.html.twig' list: 'SonataAdminBundle:CRUD:list.html.twig' show: 'SonataAdminBundle:CRUD:show.html.twig' show_compare: 'SonataAdminBundle:CRUD:show_compare.html.twig' edit: 'SonataAdminBundle:CRUD:edit.html.twig' ...
  124. 225.

    Overriding some key templates # app/config/config.php sonata_admin: templates: user_block: 'SonataAdminBundle:Core:user_block.html.twig'

    add_block: 'SonataAdminBundle:Core:add_block.html.twig' layout: ':Admin:layout.html.twig' ajax: 'SonataAdminBundle::ajax_layout.html.twig' dashboard: ':Admin:dashboard.html.twig' search: 'SonataAdminBundle:Core:search.html.twig' list: 'SonataAdminBundle:CRUD:list.html.twig' show: 'SonataAdminBundle:CRUD:show.html.twig' show_compare: 'SonataAdminBundle:CRUD:show_compare.html.twig' edit: 'SonataAdminBundle:CRUD:edit.html.twig' ...
  125. 230.

    Creating two Admins for the same Entity # src/AppBundle/ProcessedGiftAdmin.php class

    ProcessedGiftAdmin extends Admin { } Sure. We just create a new Admin class...
  126. 231.

    Creating two Admins for the same Entity # src/AppBundle/ProcessedGiftAdmin.php class

    ProcessedGiftAdmin extends Admin { protected $baseRouteName = 'processed_gift'; protected $baseRoutePattern = 'processed-gift' ... } (adding these properties for the routing)
  127. 232.

    Creating two Admins for the same Entity # app/config/services.yml services:

    admin.gift: class: AppBundle\Admin\GiftAdmin arguments: [~, AppBundle\Entity\Gift, AppBundle:GiftAdmin] tags: - { name: sonata.admin, manager_type: orm, label: Gifts } calls: - [ setTemplate, [edit, :Admin:edit_gift.html.twig]] ...and register the new service.
  128. 233.

    Creating two Admins for the same Entity # app/config/services.yml services:

    admin.gift: class: AppBundle\Admin\GiftAdmin arguments: [~, AppBundle\Entity\Gift, AppBundle:GiftAdmin] tags: - { name: sonata.admin, manager_type: orm, label: Gifts } calls: - [ setTemplate, [edit, :Admin:edit_gift.html.twig]] admin.processed_gift: class: AppBundle\Admin\ProcessedGiftAdmin arguments: [~, AppBundle\Entity\Gift, ~] tags: - { name: sonata.admin, manager_type: orm, label: Processed } ...and register the new service.
  129. 234.

    Creating two Admins for the same Entity # app/config/services.yml services:

    admin.gift: class: AppBundle\Admin\GiftAdmin arguments: [~, AppBundle\Entity\Gift, AppBundle:GiftAdmin] tags: - { name: sonata.admin, manager_type: orm, label: Active } calls: - [ setTemplate, [edit, :Admin:edit_gift.html.twig]] admin.processed_gift: class: AppBundle\Admin\ProcessedGiftAdmin arguments: [~, AppBundle\Entity\Gift, ~] tags: - { name: sonata.admin, manager_type: orm, label: Processed } ...and register the new service.
  130. 241.

    Admin as a child in some other Admin’s menu #

    app/config/services.yml admin.buyer: class: AppBundle\Admin\BuyerAdmin arguments: [~, AppBundle\Entity\Buyer, ~] tags: - { name: sonata.admin, manager_type: orm, label: Buyers } calls: - [ addChild, [ '@admin.payment' ] ] admin.payment: class: AppBundle\Admin\PaymentAdmin arguments: [~, AppBundle\Entity\Payment, ~] tags: - { name: sonata.admin, manager_type: orm, label: Payments }
  131. 242.

    Admin as a child in some other Admin’s menu #

    app/config/services.yml admin.buyer: class: AppBundle\Admin\BuyerAdmin arguments: [~, AppBundle\Entity\Buyer, ~] tags: - { name: sonata.admin, manager_type: orm, label: Buyers } calls: - [ addChild, [ '@admin.payment' ] ] admin.payment: class: AppBundle\Admin\PaymentAdmin arguments: [~, AppBundle\Entity\Payment, ~] tags: - { name: sonata.admin, manager_type: orm, label: Payments }
  132. 243.

    Admin as a child in some other Admin’s menu #

    app/config/services.yml admin.buyer: class: AppBundle\Admin\BuyerAdmin arguments: [~, AppBundle\Entity\Buyer, ~] tags: - { name: sonata.admin, manager_type: orm, label: Buyers } calls: - [ addChild, [ '@admin.payment' ] ] admin.payment: class: AppBundle\Admin\PaymentAdmin arguments: [~, AppBundle\Entity\Payment, ~] tags: - { name: sonata.admin, manager_type: orm, label: Payments }
  133. 244.

    Admin as a child in some other Admin’s menu #

    app/config/services.yml admin.buyer: class: AppBundle\Admin\BuyerAdmin arguments: [~, AppBundle\Entity\Buyer, ~] tags: - { name: sonata.admin, manager_type: orm, label: Buyers } calls: - [ addChild, [ '@admin.payment' ] ] admin.payment: class: AppBundle\Admin\PaymentAdmin arguments: [~, AppBundle\Entity\Payment, ~] tags: - { name: sonata.admin, manager_type: orm, label: Payments }
  134. 245.

    Admin as a child in some other Admin’s menu #

    src/AppBundle/Admin/BuyerAdmin.php protected function configureSideMenu(ItemInterface $menu, $action, AdminInterface $childAdmin = null) { ... $menu->addChild( 'Payments', $admin->generateMenuUrl('admin.buyer|admin.payment.list', array('id' => $id)) ); }
  135. 246.

    Admin as a child in some other Admin’s menu #

    src/AppBundle/Admin/BuyerAdmin.php protected function configureSideMenu(ItemInterface $menu, $action, AdminInterface $childAdmin = null) { ... $menu->addChild( 'Payments', $admin->generateMenuUrl('admin.buyer|admin.payment.list', array('id' => $id)) ); }
  136. 248.

    Admin as a child in some other Admin’s menu #

    src/AppBundle/Admin/PaymentAdmin.php public function configure() { $this->parentAssociationMapping = 'buyer'; }
  137. 250.

    Linking a custom action from the sidebar We create our

    custom action, the template, and the route...
  138. 251.

    Linking a custom action from the sidebar We create our

    custom action, the template, and the route... # app/config/config.yml dashboard: groups: ... demo.admin.settings: label: Settings label_catalogue: AppBundle icon: '<i class="fa fa-gear"></i>' items: - admin.config ...and link it from the sidebar
  139. 252.

    Linking a custom action from the sidebar We create our

    custom action, the template, and the route... # app/config/config.yml dashboard: groups: ... demo.admin.settings: label: Settings label_catalogue: AppBundle icon: '<i class="fa fa-gear"></i>' items: - admin.config - route: config_myEdit label: 'My config' ...and link it from the sidebar
  140. 255.

    Your entire application can be “inside” Sonata Your project can

    have parts that are not an admin panel But are perfectly integrated with everything else You can actually put whatever you want in there
  141. 258.

    Your entire application can be “inside” Sonata Create your action

    in your controller (PatientAdminController.php)
  142. 259.

    Your entire application can be “inside” Sonata Create your action

    in your controller (PatientAdminController.php) Configure the new route in configureRoutes (PatientAdmin.php) $collection->add('editor', $this->getRouterIdParameter().'/editor');
  143. 260.

    Your entire application can be “inside” Sonata Create your action

    in your controller (PatientAdminController.php) Configure the new route in configureRoutes (PatientAdmin.php) $collection->add('editor', $this->getRouterIdParameter().'/editor'); Do setTemplate in the service configuration (services.yml) admin.patient: class: AppBundle\Admin\PatientAdmin arguments: [~, AppBundle\Entity\Patient, AppBundle:PatientAdmin] tags: - { name: sonata.admin, manager_type: orm, label: Activos } calls: - [ setTemplate, [edit, :Admin:my_template.html.twig]]
  144. 261.

    Your entire application can be “inside” Sonata Create your action

    in your controller (PatientAdminController.php) Configure the new route in configureRoutes (PatientAdmin.php) $collection->add('editor', $this->getRouterIdParameter().'/editor'); Do setTemplate in the service configuration (services.yml) admin.patient: class: AppBundle\Admin\PatientAdmin arguments: [~, AppBundle\Entity\Patient, AppBundle:PatientAdmin] tags: - { name: sonata.admin, manager_type: orm, label: Activos } calls: - [ setTemplate, [edit, :Admin:my_template.html.twig]] All stuff that we already know!
  145. 264.

    FOSUserBundle & SonataUserAdminBundle SonataUserAdminBundle works as a layer on top

    of FOSUserBundle It gives you some additional functionalities But you don’t actually need it, if you don’t want to use it https://sonata-project.org/bundles/user/master/doc/reference/introduction.html
  146. 265.

    Security, roles Security can be configured in different ways /

    levels With as much detail as you want https://sonata-project.org/bundles/admin/master/doc/reference/security.html
  147. 266.

    Events There are some events defined in Sonata that can

    be very handy: • sonata.admin.event.persistence.pre_update • sonata.admin.event.persistence.post_update • sonata.admin.event.persistence.pre_persist • sonata.admin.event.persistence.post_persist • sonata.admin.event.persistence.pre_remove • sonata.admin.event.persistence.post_remove https://sonata-project.org/bundles/admin/master/doc/reference/events.html
  148. 274.

    Final thoughts Has solved problems historically attributed, keeps improving Allows

    you to efficiently solve a tedious problem In my experience, a good decision from the business point of view
  149. 276.

    Final thoughts 1. It gives you a lot for free

    2. You can override whatever you want and do your own thing