Slide 1

Slide 1 text

Building great admin panels with Symfony and SonataAdminBundle Victoria Quirante

Slide 2

Slide 2 text

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 [email protected]

Slide 3

Slide 3 text

I. Introduction Or why are we here

Slide 4

Slide 4 text

Some context “A bundle is simply a structured set of files within a directory that implement a single feature.“

Slide 5

Slide 5 text

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.“

Slide 6

Slide 6 text

SonataAdminBundle: The author

Slide 7

Slide 7 text

Main contributors

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

Main problems historically attributed - Difficult to install

Slide 11

Slide 11 text

Main problems historically attributed - Difficult to install - Ugly

Slide 12

Slide 12 text

Main problems historically attributed - Difficult to install - Ugly - Poor documentation

Slide 13

Slide 13 text

Main problems historically attributed - Difficult to install - Ugly - Poor documentation - A lot of code, difficult to dig in

Slide 14

Slide 14 text

Main problems historically attributed - Difficult to install - Ugly - Poor documentation - A lot of code, difficult to dig in

Slide 15

Slide 15 text

Main problems historically attributed - Difficult to install - Ugly - Poor documentation - A lot of code, difficult to dig in

Slide 16

Slide 16 text

Main problems historically attributed - Difficult to install - Ugly - Poor documentation can still improve - A lot of code, difficult to dig in

Slide 17

Slide 17 text

Main problems historically attributed - Difficult to install - Ugly - Poor documentation can still improve - A lot of code, difficult to dig in

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

The problem that we want to solve...

Slide 20

Slide 20 text

...is to implement an admin panel

Slide 21

Slide 21 text

What Sonata is, what it is not It is not Michelangelo’s David

Slide 22

Slide 22 text

What Sonata is, what it is not It is not Michelangelo’s David But it is something very useful

Slide 23

Slide 23 text

What Sonata is, what it is not It is not Michelangelo’s David But it is something very useful Like Symfony, like PHP

Slide 24

Slide 24 text

Practical talk Not an exhaustive review of the documentation A walk-through from a clean installation showing:

Slide 25

Slide 25 text

Practical talk Not an exhaustive review of the documentation A walk-through from a clean installation showing: 1. What Sonata gives you “for free”

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

Facing two opposite forces

Slide 28

Slide 28 text

Burn it ALL

Slide 29

Slide 29 text

Do not touch ANYTHING

Slide 30

Slide 30 text

Practical talk https://github.com/VictoriaQ/sonatademo-en

Slide 31

Slide 31 text

II. The basic Admin Or what does SonataAdminBundle give you for free

Slide 32

Slide 32 text

Installation, very briefly

Slide 33

Slide 33 text

Install Symfony ● Get the Symfony Installer ● symfony new my_project_name ● php bin/console server:run

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

Screenshots (II): Install Symfony

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

Screenshots (II): Install SonataAdminBundle

Slide 39

Slide 39 text

Screenshots (II): Install SonataAdminBundle

Slide 40

Slide 40 text

Screenshots (II): Install SonataAdminBundle

Slide 41

Slide 41 text

How do we start?

Slide 42

Slide 42 text

We need first to have an entity We create our first entity with Symfony (Doctrine)

Slide 43

Slide 43 text

We need first to have an entity We create our first entity with Symfony (Doctrine) php bin/console generate:doctrine:entity

Slide 44

Slide 44 text

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; }

Slide 45

Slide 45 text

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; }

Slide 46

Slide 46 text

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; }

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

Basic Admin: 2 steps, 2 minutes Have entity Gift, want to create Admin: 1) Create GiftAdmin class 2) Configure the service

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

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'); } }

Slide 53

Slide 53 text

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'); } }

Slide 54

Slide 54 text

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'); } }

Slide 55

Slide 55 text

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 }

Slide 56

Slide 56 text

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 }

Slide 57

Slide 57 text

Screenshots (II): First Admin

Slide 58

Slide 58 text

Screenshots (II): First Admin

Slide 59

Slide 59 text

Screenshots (II): First Admin

Slide 60

Slide 60 text

Screenshots (II): First Admin

Slide 61

Slide 61 text

Basic Admin - Step 3 (optional): Sidebar # app/config/config.yml sonata_admin: dashboard: groups: demo.admin.main: label: Admin label_catalogue: AppBundle icon: '' items: - admin.gift

Slide 62

Slide 62 text

Basic Admin - Step 3 (optional): Sidebar # app/config/config.yml sonata_admin: dashboard: groups: demo.admin.main: label: Admin label_catalogue: AppBundle icon: '' items: - admin.gift

Slide 63

Slide 63 text

Basic Admin - Step 3 (optional): Sidebar # app/config/config.yml sonata_admin: dashboard: groups: demo.admin.main: label: Admin label_catalogue: AppBundle icon: '' items: - admin.gift

Slide 64

Slide 64 text

Screenshots (II): First Admin

Slide 65

Slide 65 text

Screenshots (II): First Admin

Slide 66

Slide 66 text

Ok... and what do I get?

Slide 67

Slide 67 text

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

Slide 68

Slide 68 text

Screenshots (II): First Admin - The form

Slide 69

Slide 69 text

Basic Admin - What do I get? Create, edit, delete… Sortable, exportable, filterable, paginated list

Slide 70

Slide 70 text

Basic Admin - What do I get? Create, edit, delete… Sortable, exportable, filterable, paginated list

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

Screenshots (II): First Admin - The list

Slide 73

Slide 73 text

Screenshots (II): First Admin - The list

Slide 74

Slide 74 text

Screenshots (II): First Admin - The list

Slide 75

Slide 75 text

Screenshots (II): First Admin - The list

Slide 76

Slide 76 text

Screenshots (II): First Admin - The list

Slide 77

Slide 77 text

Screenshots (II): First Admin - The filters

Slide 78

Slide 78 text

Basic list configuration

Slide 79

Slide 79 text

Basic list configuration # src/AppBundle/Admin/GiftAdmin.php class GiftAdmin extends AbstractAdmin { protected function configureListFields(ListMapper $listMapper) { $listMapper ->add('name') ; } }

Slide 80

Slide 80 text

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') ; } }

Slide 81

Slide 81 text

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') ; } }

Slide 82

Slide 82 text

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') ; } }

Slide 83

Slide 83 text

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') ; } }

Slide 84

Slide 84 text

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') ; } }

Slide 85

Slide 85 text

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

Slide 86

Slide 86 text

Screenshots (II): First Admin - The list

Slide 87

Slide 87 text

Screenshots (II): First Admin - The list

Slide 88

Slide 88 text

Screenshots (II): First Admin - The list

Slide 89

Slide 89 text

Screenshots (II): First Admin - The list

Slide 90

Slide 90 text

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(), ) )) ; } }

Slide 91

Slide 91 text

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(), ) )) ; } }

Slide 92

Slide 92 text

Screenshots (II): First Admin - The list

Slide 93

Slide 93 text

Screenshots (II): First Admin - The list

Slide 94

Slide 94 text

Basic form configuration

Slide 95

Slide 95 text

Basic form configuration # src/AppBundle/Admin/GiftAdmin.php class GiftAdmin extends AbstractAdmin { protected function configureFormFields(FormMapper $formMapper) { $formMapper ->add('name') ; } }

Slide 96

Slide 96 text

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') ; } }

Slide 97

Slide 97 text

Screenshots (II): First Admin - The form

Slide 98

Slide 98 text

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') ; } }

Slide 99

Slide 99 text

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() ; } }

Slide 100

Slide 100 text

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() ; } }

Slide 101

Slide 101 text

Screenshots (II): First Admin - The form

Slide 102

Slide 102 text

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() ; }

Slide 103

Slide 103 text

Screenshots (II): First Admin - The form

Slide 104

Slide 104 text

What if one field has a relation with another entity?

Slide 105

Slide 105 text

Many-to-one relations # src/AppBundle/Entity/Gift.php class Gift { … /** * @Column(type="string") */ private $addressee; … }

Slide 106

Slide 106 text

Many-to-one relations # src/AppBundle/Entity/Gift.php class Gift { … /** * @ORM\ManyToOne(targetEntity="Addressee") */ private $addressee; … }

Slide 107

Slide 107 text

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

Slide 108

Slide 108 text

Screenshots (II): First Admin - The form

Slide 109

Slide 109 text

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() ; } }

Slide 110

Slide 110 text

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() ; } }

Slide 111

Slide 111 text

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() ; } }

Slide 112

Slide 112 text

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() ; } }

Slide 113

Slide 113 text

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() ; } }

Slide 114

Slide 114 text

Screenshots (II): First Admin - The form

Slide 115

Slide 115 text

What about the one-to-many?

Slide 116

Slide 116 text

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() ; } }

Slide 117

Slide 117 text

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(); } }

Slide 118

Slide 118 text

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(); } }

Slide 119

Slide 119 text

Screenshots (II): A collection in the form

Slide 120

Slide 120 text

Screenshots (II): A collection in the form

Slide 121

Slide 121 text

Screenshots (II): A collection in the form

Slide 122

Slide 122 text

And the many-to-many?

Slide 123

Slide 123 text

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(); }

Slide 124

Slide 124 text

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(); }

Slide 125

Slide 125 text

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(); }

Slide 126

Slide 126 text

Screenshots (II): Many-to-many relation

Slide 127

Slide 127 text

Screenshots (II): Many-to-many relation

Slide 128

Slide 128 text

Screenshots (II): Many-to-many relation

Slide 129

Slide 129 text

Screenshots (II): Many-to-many relation

Slide 130

Slide 130 text

Basic filters configuration

Slide 131

Slide 131 text

Basic filters configuration # src/AppBundle/Admin/GiftAdmin.php protected function configureDatagridFilters(DatagridMapper $datagridMapper) { $datagridMapper ->add('name') ; }

Slide 132

Slide 132 text

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')) ; }

Slide 133

Slide 133 text

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

Slide 134

Slide 134 text

Screenshots (II): Basic filters configuration

Slide 135

Slide 135 text

So far, what we got “for free”

Slide 136

Slide 136 text

Basic Admin - How much do I get? With very little effort you have a lot Actually, you get most of what you need

Slide 137

Slide 137 text

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

Slide 138

Slide 138 text

No content

Slide 139

Slide 139 text

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?

Slide 140

Slide 140 text

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?

Slide 141

Slide 141 text

No content

Slide 142

Slide 142 text

III. Customizing Or how to do my own thing

Slide 143

Slide 143 text

Customizing ● Templates ● Queries ● Actions

Slide 144

Slide 144 text

Customizing templates

Slide 145

Slide 145 text

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' ...

Slide 146

Slide 146 text

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

Slide 147

Slide 147 text

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' ...

Slide 148

Slide 148 text

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' ...

Slide 149

Slide 149 text

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

Slide 150

Slide 150 text

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

Slide 151

Slide 151 text

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

Slide 152

Slide 152 text

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 %}

Slide 153

Slide 153 text

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

Slide 154

Slide 154 text

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 %}

Slide 155

Slide 155 text

Screenshots (III): Customizing templates

Slide 156

Slide 156 text

Screenshots (III): Customizing templates

Slide 157

Slide 157 text

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 %}

Slide 158

Slide 158 text

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 %}

Slide 159

Slide 159 text

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

Slide 160

Slide 160 text

What if I only want to override the template for one Admin?

Slide 161

Slide 161 text

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 }

Slide 162

Slide 162 text

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]]

Slide 163

Slide 163 text

Screenshots (III): Customizing templates

Slide 164

Slide 164 text

How do I customize a field on the list?

Slide 165

Slide 165 text

Customizing a single field on the list (step 1) # src/AppBundle/Admin/GiftAdmin.php protected function configureListFields(ListMapper $listMapper) { $listMapper ... ; }

Slide 166

Slide 166 text

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')) ; }

Slide 167

Slide 167 text

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')) ; }

Slide 168

Slide 168 text

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 %} Send {% endblock %}

Slide 169

Slide 169 text

Screenshots (III): Customizing templates

Slide 170

Slide 170 text

Customizing queries

Slide 171

Slide 171 text

Screenshots (III): Customizing list query

Slide 172

Slide 172 text

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; }

Slide 173

Slide 173 text

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; }

Slide 174

Slide 174 text

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

Slide 175

Slide 175 text

Screenshots (III): Customizing list query

Slide 176

Slide 176 text

What about the filter queries?

Slide 177

Slide 177 text

Screenshots (III): Customizing filter query

Slide 178

Slide 178 text

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' )); }

Slide 179

Slide 179 text

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' )); }

Slide 180

Slide 180 text

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' )); }

Slide 181

Slide 181 text

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' )); }

Slide 182

Slide 182 text

Screenshots (III): Customizing filter query

Slide 183

Slide 183 text

Screenshots (III): Customizing filter query

Slide 184

Slide 184 text

Screenshots (III): Customizing filter query

Slide 185

Slide 185 text

Customizing controllers

Slide 186

Slide 186 text

Screenshots (III): Creating custom action

Slide 187

Slide 187 text

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')); } }

Slide 188

Slide 188 text

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')); } }

Slide 189

Slide 189 text

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]]

Slide 190

Slide 190 text

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]]

Slide 191

Slide 191 text

Creating a custom action (step 3) # src/AppBundle/Admin/GiftAdmin.php protected function configureRoutes(RouteCollection $collection) { $collection->add('sendEmail', $this->getRouterIdParameter().'/send-email'); }

Slide 192

Slide 192 text

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

Slide 193

Slide 193 text

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

Slide 194

Slide 194 text

Screenshots (III): Creating custom action

Slide 195

Slide 195 text

Screenshots (III): Creating custom batch action

Slide 196

Slide 196 text

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')); }

Slide 197

Slide 197 text

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')); }

Slide 198

Slide 198 text

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; }

Slide 199

Slide 199 text

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; }

Slide 200

Slide 200 text

Screenshots (III): Creating custom batch action

Slide 201

Slide 201 text

Screenshots (III): Creating custom batch action

Slide 202

Slide 202 text

IV. Useful tips Or how to solve a few things that we need in every project

Slide 203

Slide 203 text

How can I change the admin panel appearance? Tip 1 / 5

Slide 204

Slide 204 text

Screenshots (IV): Customizing the styles

Slide 205

Slide 205 text

Screenshots (IV): Customizing the styles

Slide 206

Slide 206 text

Customizing the styles # app/config/config.php sonata_admin: title: SunshinePHP

Slide 207

Slide 207 text

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

Slide 208

Slide 208 text

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 …

Slide 209

Slide 209 text

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

Slide 210

Slide 210 text

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 …

Slide 211

Slide 211 text

Screenshots (IV): Customizing the styles

Slide 212

Slide 212 text

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 …

Slide 213

Slide 213 text

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 …

Slide 214

Slide 214 text

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/

Slide 215

Slide 215 text

Screenshots (IV): Overriding key templates

Slide 216

Slide 216 text

Screenshots (IV): Overriding key templates

Slide 217

Slide 217 text

Screenshots (IV): Overriding key templates

Slide 218

Slide 218 text

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' ...

Slide 219

Slide 219 text

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' ...

Slide 220

Slide 220 text

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' ...

Slide 221

Slide 221 text

Screenshots (IV): Overriding key templates

Slide 222

Slide 222 text

Screenshots (IV): Overriding key templates

Slide 223

Slide 223 text

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' ...

Slide 224

Slide 224 text

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' ...

Slide 225

Slide 225 text

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' ...

Slide 226

Slide 226 text

Screenshots (IV): Overriding key templates

Slide 227

Slide 227 text

Screenshots (IV): Overriding key templates

Slide 228

Slide 228 text

Screenshots (IV): Overriding key templates

Slide 229

Slide 229 text

Can I create two Admins for the same Entity? Tip 2 / 5

Slide 230

Slide 230 text

Creating two Admins for the same Entity # src/AppBundle/ProcessedGiftAdmin.php class ProcessedGiftAdmin extends Admin { } Sure. We just create a new Admin class...

Slide 231

Slide 231 text

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)

Slide 232

Slide 232 text

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.

Slide 233

Slide 233 text

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.

Slide 234

Slide 234 text

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.

Slide 235

Slide 235 text

Screenshots (IV): Two Admins for same Entity

Slide 236

Slide 236 text

Screenshots (IV): Two Admins for same Entity

Slide 237

Slide 237 text

Screenshots (IV): Two Admins for same Entity

Slide 238

Slide 238 text

Using Admin as a child of another Admin Tip 3 / 5

Slide 239

Slide 239 text

Screenshots (IV): Admin as a child

Slide 240

Slide 240 text

Screenshots (IV): Admin as a child

Slide 241

Slide 241 text

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 }

Slide 242

Slide 242 text

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 }

Slide 243

Slide 243 text

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 }

Slide 244

Slide 244 text

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 }

Slide 245

Slide 245 text

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)) ); }

Slide 246

Slide 246 text

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)) ); }

Slide 247

Slide 247 text

Screenshots (IV): Admin as a child

Slide 248

Slide 248 text

Admin as a child in some other Admin’s menu # src/AppBundle/Admin/PaymentAdmin.php public function configure() { $this->parentAssociationMapping = 'buyer'; }

Slide 249

Slide 249 text

What if the ‘list’ is not the main thing in my Admin? Tip 4 / 5

Slide 250

Slide 250 text

Linking a custom action from the sidebar We create our custom action, the template, and the route...

Slide 251

Slide 251 text

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: '' items: - admin.config ...and link it from the sidebar

Slide 252

Slide 252 text

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: '' items: - admin.config - route: config_myEdit label: 'My config' ...and link it from the sidebar

Slide 253

Slide 253 text

Screenshots (IV): Custom action in sidebar

Slide 254

Slide 254 text

What if I want to add something that is not an Admin? Tip 5 / 5

Slide 255

Slide 255 text

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

Slide 256

Slide 256 text

Your entire application can be “inside” Sonata

Slide 257

Slide 257 text

Your entire application can be “inside” Sonata

Slide 258

Slide 258 text

Your entire application can be “inside” Sonata Create your action in your controller (PatientAdminController.php)

Slide 259

Slide 259 text

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');

Slide 260

Slide 260 text

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]]

Slide 261

Slide 261 text

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!

Slide 262

Slide 262 text

V. Further Or what else is out there

Slide 263

Slide 263 text

SonataAdminBundle documentation

Slide 264

Slide 264 text

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

Slide 265

Slide 265 text

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

Slide 266

Slide 266 text

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

Slide 267

Slide 267 text

Sonata Project Demo

Slide 268

Slide 268 text

Sonata Project Demo

Slide 269

Slide 269 text

Sonata Project Demo

Slide 270

Slide 270 text

And, of course… the code

Slide 271

Slide 271 text

Admin class vendor/sonata-project/admin-bundle/Admin/AbstractAdmin.php Extremely important to look into the Admin class

Slide 272

Slide 272 text

VI. Final thoughts Or what should I remember from all this

Slide 273

Slide 273 text

Final thoughts Has solved problems historically attributed, keeps improving Allows you to efficiently solve a tedious problem

Slide 274

Slide 274 text

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

Slide 275

Slide 275 text

Final thoughts 1. It gives you a lot for free

Slide 276

Slide 276 text

Final thoughts 1. It gives you a lot for free 2. You can override whatever you want and do your own thing

Slide 277

Slide 277 text

NO NEED to start from scratch

Slide 278

Slide 278 text

DO TOUCH whatever you

Slide 279

Slide 279 text

https://github.com/VictoriaQ/sonatademo @vicqr [email protected] Training, consulting and development https://github.com/VictoriaQ/sonatademo @vicqr [email protected]

Slide 280

Slide 280 text

https://github.com/VictoriaQ/sonatademo @vicqr [email protected] Training, consulting and development https://github.com/VictoriaQ/sonatademo @vicqr [email protected] Thanks!