If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.
With a Subscription, click any sentence in the script to jump to that part of the video!
Login SubscribeWe can do a lot via config... but eventually... we're going to need to really dig in. 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 can override any of these. But even better! We can override any of these for specific entities: using different customized templates for different sections. Or even... different templates to control how individual fields render.
Check out layout.html.twig
... this is the full admin page layout. It's awesome because it's filled with blocks. So instead of completely replacing the layout, you could extend this and override only the blocks you need. We won't do that for the layout, but we will for list.html.twig
.
This is responsible for the list
view we've been working on. And not surprisingly, there are also new, show and edit templates.
But most of the templates start with field_
... interesting. Remember how each field on the list page has a "data type"? We saw this in the EasyAdminBundle configuration. The "data type" is used to determine which template should render the data in that column. firstDiscoveredAt
is a date
type... and hey! It has a template
option that defaults to field_date.html.twig
. And by opening that template, you can see how the date
type is rendered.
Ok, let's finally override some stuff! How!? On the same List, Search and Show Views Configuration page, near the bottom, you'll see an "Advanced Design Configuration" section. There are a bunch of different ways to override a template... ah... too many options! Let's simplify: (A) you can override a template via configuration - which are options 1 and 2 - or (B) by putting a file in an easy_admin
directory - options 3 and 4. We'll try both.
Ok, first challenge! I want to override the way the id
field is rendered for Genus
: add a little key
icon next to the number... ya know, because it's the primary key.
This means we need to override the field_id.html.twig
template, because id
is actually a data type. Copy field_id.html.twig
. Then, in app/Resources/views
, I already have an admin
directory. So inside that, create a new fields
directory and paste the file there, as _id.html.twig
. Now, add the icon: fa fa-key
:
<i class="fa fa-key"></i> {{ value }} |
Cool! I put the file here... just because I already have an admin
directory. But EasyAdminBundle doesn't automagically know it's there. Nope, we need to tell it. In config.yml
, to use this only for Genus
, add a templates
key, then field_id
- the name of the original template - set to admin/fields/_id.html.twig
:
... lines 1 - 80 | |
easy_admin: | |
... lines 82 - 89 | |
entities: | |
Genus: | |
... lines 92 - 110 | |
templates: | |
field_id: 'admin/fields/_id.html.twig' | |
... lines 113 - 115 |
Try that! Yes! It is using our template... and only in the Genus
section. But this key thing is pretty excellent, so we should use it everywhere. Copy the templates config and comment it out:
... lines 1 - 80 | |
easy_admin: | |
... lines 82 - 91 | |
entities: | |
Genus: | |
... lines 94 - 112 | |
# templates: | |
# field_id: 'admin/fields/_id.html.twig' | |
... lines 115 - 117 |
Just like with almost anything, we can also control the template globally: paste this under design
:
... lines 1 - 80 | |
easy_admin: | |
... line 82 | |
design: | |
... lines 84 - 86 | |
templates: | |
field_id: 'admin/fields/_id.html.twig' | |
... lines 89 - 117 |
Now the key icon shows up everywhere.
Next, I want to override something bigger: the entire list template. And we'll use a different convention to do that.
Hey @Floren
What error did you get?
You can define your own templates by following this config:
easy_admin:
entities:
SomeEntity:
...
templates:
show: 'path/to/template.html.twig'
...
Cheers!
Hi Aziz,
This is our custom Twig filter we created on this page: https://knpuniversity.com/s... . In order to be able to use it - you need to create it first as we do in that screencast.
Cheers!
// composer.json
{
"require": {
"php": ">=5.5.9",
"symfony/symfony": "3.3.*", // v3.3.18
"doctrine/orm": "^2.5", // v2.7.2
"doctrine/doctrine-bundle": "^1.6", // 1.10.3
"doctrine/doctrine-cache-bundle": "^1.2", // 1.3.5
"symfony/swiftmailer-bundle": "^2.3", // v2.6.7
"symfony/monolog-bundle": "^2.8", // v2.12.1
"symfony/polyfill-apcu": "^1.0", // v1.17.0
"sensio/distribution-bundle": "^5.0", // v5.0.25
"sensio/framework-extra-bundle": "^3.0.2", // v3.0.29
"incenteev/composer-parameter-handler": "^2.0", // v2.1.4
"knplabs/knp-markdown-bundle": "^1.4", // 1.7.1
"doctrine/doctrine-migrations-bundle": "^1.1", // v1.3.2
"stof/doctrine-extensions-bundle": "^1.2", // v1.3.0
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"javiereguiluz/easyadmin-bundle": "^1.16" // v1.17.21
},
"require-dev": {
"sensio/generator-bundle": "^3.0", // v3.1.7
"symfony/phpunit-bridge": "^3.0", // v3.4.40
"nelmio/alice": "^2.1", // v2.3.5
"doctrine/doctrine-fixtures-bundle": "^2.3" // v2.4.1
}
}
I'm trying to find a way to apply the same principle but this time for the EDIT / NEW view, I tried to put a 'template' option but it does not work, so I am forced to inherit the entire editing template ... and after modifying the {{form (form.element, {'attr'}}? is there a way or I could apply a specific template just for a property well determined.