Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine
This tutorial has a new version, check it out!

Configuring the List Fields

Keep on Learning!

If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.

Start your All-Access Pass
Buy just this tutorial for $8.00

With a Subscription, click any sentence in the script to jump to that part of the video!

Login Subscribe

We've been tweaking a bunch of stuff on the list view. But... what about this big giant table in the middle!? How can we customize that?

Actually, EasyAdminBundle did a pretty good job with it: it guesses which fields to show, humanizes the column header labels and renders things nicely. Good job Javier!

The EasyAdminBundle Profiler

Before we tweak all of this, see that magic wand in the web debug toolbar? Say "Alohomora" and click to open that in a new tab. This is the EasyAdminBundle profiler... and it's awesome. Here, under "Current entity configuration", we can see all of the config we've been building for this entity, including default values that it's guessing for us. This is a sweet map for knowing what can be changed and how.

Under list, then fields, it shows details about all the columns used for the table. For example, under name, you can see type => string. Actually, dataType is the really important one.

Here's the deal: each field that's rendered in the table has a different data type, like string, float, date, email and a bunch others. EasyAdminBundle guesses a type, and it affects how the data for that field is rendered. We can change the type... and anything else you see here.

Controller Fields

How? Under Genus and list, add fields. Now, list the exact fields that you want to display, like id, name and isPublished:

... lines 1 - 80
easy_admin:
... lines 82 - 89
entities:
Genus:
... lines 92 - 94
list:
... lines 96 - 99
fields:
- 'id'
- 'name'
- 'isPublished'
... lines 104 - 121

These 3 fields were already shown before.

Let's also show firstDiscoveredAt... but! I want to tweak it a little. Just like with actions, there is an "expanded" config format. Add { } with property: firstDiscoveredAt.

Now... what configuration can we put here? Because this is a date field, it has a format option. Set it to M Y. And, all fields have a label option. Use "Discovered":

... lines 1 - 80
easy_admin:
... lines 82 - 89
entities:
Genus:
... lines 92 - 94
list:
... lines 96 - 99
fields:
- 'id'
- 'name'
- 'isPublished'
- { property: 'firstDiscoveredAt', format: 'M Y', label: 'Discovered' }
... lines 105 - 121

Keep going! Add funFact and then one more expanded property: property: speciesCount. This is an integer type, which also has a format option. For fun, set it to %b - binary format!

... lines 1 - 80
easy_admin:
... lines 82 - 89
entities:
Genus:
... lines 92 - 94
list:
... lines 96 - 99
fields:
- 'id'
- 'name'
- 'isPublished'
- { property: 'firstDiscoveredAt', format: 'M Y', label: 'Discovered' }
- 'funFact'
- { property: 'speciesCount', format: '%b' }
... lines 107 - 121

Yea know because, scientists are nerds and like puzzles.

Tip

The format option for number fields is passed to the sprintf() function.

If your head is starting to spin with all of these types and options that I'm pulling out of the air, don't worry! Almost all of the options - like label - are shared across all the types. There are very few type-specific options like format.

And more importantly, in a few minutes, we'll look at a list of all of the valid types and their options.

Ok! Close the profiler tab and refresh. Bam! The table has our 6 columns!

Customizing the Search View

Try out the search again: look for "quo". Ok nice! Without any work, the search view re-uses the fields config from list.

You can add a fields key under search, but it means something different. Add fields: [id, name]:

... lines 1 - 80
easy_admin:
... lines 82 - 89
entities:
Genus:
... lines 92 - 106
search:
... line 108
fields: ['id', 'name']
... lines 110 - 122

Out-of-the-box, the bundle searches every field for the search string. You can see that in the queries. But now, it only searches id and name.

Next, let's dive into some of the more interesting field types and their config.

Leave a comment!

4
Login or Register to join the conversation
Shaun T. Avatar
Shaun T. Avatar Shaun T. | posted 4 years ago

Hi guys,

In my user entity I have set class constants relating to the user type, for example:

const TYPE_STAFF = 0;
const TYPE_SUPERVISOR = 1;
const TYPE_MANAGER = 2;
const TYPE_ADMIN = 3;

Is it possible to display something more descriptive in the list rather than just the id field?

Reply

Hey Shaun T.

You can define your constants using strings, e.g. const TYPE_STAFF = 'Staff';
Or you can implement a method getUserType() that returns the specific string you need.

Cheers!

Reply
Shaun T. Avatar
Shaun T. Avatar Shaun T. | MolloKhan | posted 4 years ago | edited

Thanks MolloKhan, can you tell me how I can configure my easy_admin.yaml so that the userType value is displayed in the list view?

Reply

Give it a check to the "Virtual properties" section of the documentation: https://symfony.com/doc/mas...
I believe that's exactly what you need in this case

Reply
Cat in space

"Houston: no signs of life"
Start the conversation!

This tutorial is built on an older version of Symfony & EasyAdminBundle. Many of the concepts are the same, but you can expect major differences in newer versions.

What PHP libraries does this tutorial use?

// 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
    }
}
userVoice