Form Panels
…one CRUD controller... or you could put this in the dashboard
to apply everywhere.
But, apart from a custom form theme, there are a few other ways that EasyAdmin
allows us to control what this page looks like... which, right now, is just a long…
Overriding Field Templates
…But, hmm. We also have a "votes" field inside of the Questions section. While it
would be pretty easy to also point that votes field to the same new template,
instead, I want to create a brand new custom field in EasyAdmin. That's next.
The Filter System
…filter the records in this index section by some criteria, for example, to only
show users that are enabled or not enabled. Fortunately, EasyAdmin has a system
for this called, well, filters!
Over in UserCrudController, I'll go to the bottom and override yet another…
Design Config & Security Setup
…then dive into the
really tough stuff. Let's start with design.
Right now, in the upper left corner, it says, "EasyAdmin"... which is probably
not what I want. Like most stuff, this can be changed in the config. Add a site_name
key set…
Customize all the Templates!
…And that will probably mean overriding the templates used by the bundle.
First, let's go look at those templates! Open vendor/javiereguiluz/easyadmin-bundle/Resources/views/default.
Ah, ha! These are the many templates used to render every single part of our admin.
We…
Dynamically Remove the delete Action Link
…It would use the new, smaller _list_item_actions.
Start by deleting everything and extending the base layout: @EasyAdmin/default/list.html.twig...
so that we don't need to duplicate everything:
Next, add block item_actions and endblock:
Twig isn't really meant for…
Views & entities Config
…for all list views, but then override that
list tweak for one specific entity.
Let's see this in action! In config.yml, under easy_admin, you can configure the
list view by adding a list key. Set title: 'List':
And yep! Each view can…
Customize Template for One Field
…how about, _field_user_full_name.html.twig:
Copy that name. It expects this to live in the standard easy_admin directory.
Create it there!
Since this is a template for one field, it will have access to the User object as
an item variable…
Controlling the Dashboard Menu
…method really means:
Link to somewhere... but run that controller through the admin section.
This can be useful if you have a custom controller but want to leverage some of the
EasyAdmin tools from inside it. If you just want to link to a page…
The AssociationField
…excellent!
Let's do the $topic field! This is an interesting one because it's a relation
to the Topic entity. How can we handle that in EasyAdmin? With the super
powerful AssociationField. Yield AssociationField::new() and pass topic:
That's it!
Click "Questions" to…
Auto-complete Association Field & Controlling the Query
…users.
How? Once again, we can call a custom method on AssociationField called
->setQueryBuilder(). Pass this a function() with a QueryBuilder $queryBuilder
argument:
When EasyAdmin generates the list of results, it creates the query builder for
us, and then we can modify it. Say $queryBuilder…
The Field Configurator System
…
Right now, I'm using Field which tells EasyAdmin to guess the best field type.
This is printing as a textarea... so its field type is really TextareaField...
and we can use that if we want to.
Here's the new goal: I want to…
Entity & Field Permissions
…pass ADMIN_USER_EDIT.
Notice this is not a role. EasyAdmin calls the security system for each entity
that it's about to display and passes this ADMIN_USER_EDIT string into the security
system. If we used a role here - like ROLE_SUPER_ADMIN…
Multiple Cruds for a Single Entity?
…time, I'll refresh... go down to the Question field, search for the string
and... it finds it!
Next, what if we want to run some code before or after an entity is updated, created,
or deleted? EasyAdmin has two solutions: Events and controller methods.
Extending with Events
…normal Symfony event subscriber and, thanks to auto configuration,
Symfony will instantly see this and start using it. In other words, whenever EasyAdmin
dispatches BeforeEntityUpdatedEvent, it will call our method.
This $event object is packed with useful info. For example, if I just say
$event…
Conditionally Disabling an Action
…name. Action::EDIT is a just constant that
resolves to the string "edit".
But, behind the scenes, EasyAdmin creates an Action object to represent this.
And that Action object knows everything about how that action should look,
including its label, CSS classes, and other stuff…
True Custom Action
…linkUrl }}"...
and then method="POST". Inside, we need the button. We could create it ourselves...
or we can be lazy and {{ include('@EasyAdmin/crud/action.html.twig') }}.
That's all we need! Reload the page... and inspect that element to see... exactly
what we want:…
Restricting Access to an Entire Crud Section
… But go
back to /admin. Uh oh: the Questions link does show up. EasyAdmin isn't smart
enough to realize that if we clicked this, we wouldn't have access. It's our
responsibility to make sure that the permissions on our link are set…
The Dashboard Page
…index.html.twig...
and I'll paste in the contents.
But there's nothing tricky here. I am extending @EasyAdmin/page/content.html.twig.
If you ever need to render a custom page... but one that still looks like it
lives inside the admin area…
Service Action Injection
…you to make sure that a service
is only instantiated for the action that needs it, instead of in all situations.
Next: let's learn how to override templates, like EasyAdmin's layout template, or
how an IdField is rendered across our entire admin area.
x
597