More with EasyAdminBundle

Posted on: 2015-06-20 | Categories: PHP

A few weeks ago I’ve posted a beginner tutorial on how to start working with Symfony2 EasyAdminBundle admin panel which has been rewritten twice because of the newly introduced functionalities. Today I want to show you more: some advanced customization and integration with FOSUserBundle and VichUploaderBundle (and maybe more soon) which are often used.

Better configuration

For typical application it’s not convenient to store all bundles configuration in one (config.yml) file. For better organization we can move EasyAdminBundle configuration (or any other bundle if you wish) to separate file (admin.yml in this case) and than import it on the top of app/config/config.yml file:

You can view the changes in this commit.

Flip Switches

EasyAdminBundle boolean field toggle Another really nice feature are flip switches. Flip switches allow users to flip state of boolean properties. For example if we have User entity with boolean property: enabled or locked, we can change its value and disable User from the list view. This is done by calling proper AJAX action by EasyAdminBundle. No extra code is necessary to handle those actions. What is also worth noting we can modify it from standard edit form view with checkbox widget.

Overriding templates

Usually default theme is not enough, we and our clients need some changes that we need to introduce to admin panel. EasyAdminBundle starting from version 1.4 offers nice and developer friendly customization possibilities.

How to add Logout button?

In this case if we want to modify template that is used by all EasyAdminBundle views we should override layout file: layout.html.twig. To do that we should create new easy_admin folder in app/Resources/views directory and copy / paste default EasyAdminBundle layout to be able to modify and extend it.

Notice: In this example we have used FOSUserBundle route: fos_user_security_logout as we have integrated it earlier with our EasyAdminBundle.

After those changes were introduced we need to reset Symfony2 cache and refresh the page to view the changes (logout link is shown on images below).

Notice: If you are new to Symfony2 commands shortcuts and aliases, I encourage you to read this blog entry: Using Symfony2 console the right way.

This was really easy and small change in our layout but we have almost unlimited possibilities:

  • any template or fragment of the backend can be overridden
  • each template can be overridden globally or per entity
  • we are not limited only to override templates – starting form EasyAdminBundle version 1.5, we can override any action executed during the backend workflow.

FOSUserBundle integration

This is one of the most common integration as we always need some authentication and users to login to EasyAdminBundle application. Installation has been described in previous entry.

Before we start integration we need to update User entity and add two properties:

and after that generate setters and getters from shell:

and update DB schema:

Those properties are needed to complete integration process with FOSUserBundle: we need information about creation date and date of update. In the previous entry I have described how to configure basic users list and add / edit form.

To biggest problem with this integration is creating and updating users and saving / updating their passwords.

Latest version of FOSUserBundle provides UserListener which is updating canonical fields and password field on prePersist and preUpdate events. The thing is that we need to remember that configuring new or edit user forms we must embed plainPassword attribute instead of password. This will cause setting and updating password property by UserListener and its prePersist and preUpdate events listeners.

To display password field in User add / edit form we need to configure it in app/config/admin.yml file in form section – we can configure instead new and edit sections to provide different configuration for each form type.

Notice: if we want to update for example only password attribute during one User update, we need to implement special hook as plainPassword field is not User mapped property and updating it will not trigger UserListener to update password field.

Update password hook

Now all we have to do is to update updatedAt property each time plainPassword property will be set.

That’s it. We can now set or update User password from EasyAdminBundle panel.

EasyAdminBundle form user

Notice: We can also implement Doctrine timestamable behavior which will set updatedAt and createdAt properties each time we change / create User entity.

VichUploaderBundle integration

The VichUploaderBundle is a Symfony2 bundle that attempts to ease file uploads that are attached to ORM entities, MongoDB ODM documents, PHPCR ODM documents or Propel models.

This bundle is really useful for uploading almost any kind of files. I’ve listed below some major features:

  • automatically uploads files to a configured directory (mappings)
  • loads the file back into the entity when it is loaded from database as an instance of Symfony\Component\HttpFoundation\File\File
  • deletes the file from the file system on removing entity from DB

In this example I’m going to add support for uploading customer logo image in EasyAdminBundle panel.
Before we start we need to install VichUploaderBundle bundle and setup necessary configuration. To install VichUploaderBundle follow the steps from documentation.

After that we need to configure VichUploader to work with our Customer entity but first we should add logo property to Customer entity.

Customer’s logo

To add logo property to Customer entity we need to update entity class and add the following code to src/AppBundle/entity/Customer.php:

and as for User entity we need to generate setters and getters for Customer:

and update DB schema:

Our entity has now logo property and we can setup logo uploading process.

Configure an upload mapping

First we are going to set mappings in app/config/config.yml file. Those mappings are later used for uploading Customer logo to proper location.

Link the upload mapping to an entity

Now we need to make some kind of connection between the filesystem and Customerentity. To do that we must add the following code to src/AppBundle/Entity/Customer.php file:

And that’s all. Now we will use EasyAdminBundle syntax to add logoFile field to Customer form and after submit, the uploaded file will automatically be moved to the location we configured in app/config/config.yml and the logo property will be set to the filename of the uploaded file.

Note: I do not recommend uploading files / images the way it’s described in the official Symfony2 documentation. Just try both methods and make a choice.

EasyAdminBundle forms

To handle file upload in EasyAdminBundle panel we need to add to Customer form logoFile field with proper configuration – this syntax has been described below:

As a result we should have form similar to this one:

EasyAdminBundle customer form


Note: Another nice option is help attribute that we cen set for each form field. Help property is message displayed below the form field in the edit, new and show views.


We can also display this image on Customer’s list and show views. To do that we only need to set proper option in app/config/admin.yml file:

And the final result:

EasyAdminBundle customer list

EasyAdminBundle show customer

As always source code used in this examples can be downloaded / cloned from our Level7 GitHub repository.

Let me know if you have any problems with other EasyAdminBundle integrations – I will try to solve it and describe here.

Good luck with your integrations!