Buy Access to Course
04.

Actions Config

Share this awesome video!

|

Keep on Learning!

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

Login Subscribe

We now know that there are 5 views: list, search, edit, new and banana... show. EasyAdminBundle also has an idea of "actions", which are basically the links and buttons that show up on each view. For example, on list, we have a search action, a new action and edit & delete actions. In the docs, they have a section called Actions Configuration that shows the "default" actions for each view. For example, from the "Edit" view, you have a delete button and a list link... but you do not have a link to create a new entity or eat a banana.

These actions can be customized a lot: we can add some, take some away, tweak their design and - gasp - create custom actions.

Adding the show Action

In the docs, it says that the list view does not have the show action by default. Yep, there's no little "show" link next to each item. If you want that, add it! Under the global list, add actions, then show:

108 lines | app/config/config.yml
// ... lines 1 - 80
easy_admin:
// ... lines 82 - 86
list:
// ... line 88
actions: ['show']
// ... lines 90 - 108

We only need to say show, we don't need to re-list all the actions. That's because the, actions configuration is a merge: it takes all of the original actions and adds whatever we have here.

Adding __toString() Methods

So... how would we remove an action? We'll get there! But first, if you click, "Show"... wow. Geez. Dang! The page is very broken:

Object of class GenusNote could not be converted to string.

In a lot of places, EasyAdminBundle tries to convert your entity objects into strings... ya know, so it can print them in a list or create a drop-down menu. To get this working, we need to add __toString() methods to each entity. Let's do that real quick!

In Genus, add public function __toString() and return $this->getName():

243 lines | src/AppBundle/Entity/Genus.php
// ... lines 1 - 17
class Genus
{
// ... lines 20 - 237
public function __toString()
{
return (string) $this->getName();
}
}

I'm casting this into a string just to be safe - if it's null, you'll get an error. In GenusNote, do the same thing: return $this->getNote():

121 lines | src/AppBundle/Entity/GenusNote.php
// ... lines 1 - 10
class GenusNote
{
// ... lines 13 - 115
public function __toString()
{
return (string) $this->getNote();
}
}

In GenusScientist, we can return $this->getUser():

85 lines | src/AppBundle/Entity/GenusScientist.php
// ... lines 1 - 17
class GenusScientist
{
// ... lines 20 - 79
public function __toString()
{
return (string) $this->getUser();
}
}

That's an object... but we're about to add a __toString() for it too!

SubFamily, well hey! It already has a __toString(). I'll just add the string cast:

45 lines | src/AppBundle/Entity/SubFamily.php
// ... lines 1 - 10
class SubFamily
{
// ... lines 13 - 39
public function __toString()
{
return (string) $this->getName();
}
}

And finally, in User ... make this a bit fancier. Return $this->getFullName() or $this->getEmail(), in case the user doesn't have a first or last name in the database:

271 lines | src/AppBundle/Entity/User.php
// ... lines 1 - 16
class User implements UserInterface
{
// ... lines 19 - 233
public function __toString()
{
return (string) $this->getFullName() ? $this->getFullName() : $this->getEmail();
}
// ... lines 238 - 269
}

Try the show page again! Nice! It renders all of the properties, including the relations, which is why it needed that __toString() method.

And because we have a SubFamily admin section, the SubFamily is a link that takes us to its show view.

Removing Actions

Speaking of SubFamily, as you can see... there's not much to it: just an id and a name. And the "show" view, well, it's just the id and name again. Not too interesting. In this case, I think it's overkill to have the show action for the SubFamily entity. So let's kill it!

Back in config.yml, we just added the, show action to the list view globally. Now, under SubFamily, we can override that list config. Add actions and - to remove an action - use -show:

110 lines | app/config/config.yml
// ... lines 1 - 80
easy_admin:
// ... lines 82 - 89
entities:
// ... lines 91 - 103
SubFamily:
// ... line 105
list:
actions: ['-show']
// ... lines 108 - 110

Yep, use "minus" to take an action away.

Refresh! Ah, ha! The show link is gone.

Disabling Actions

But... even though the link is gone, you can totally still get to the show page! For example, if we click, "Edit", we can be annoying and change the action in the URL to show. Genus!

Or... you can just go to the show page for any Genus: there is still a link to the SubFamily show page.

That might be ok, but if you truly want to disable the show view, there's a special config key for that. Under the entity itself, add disabled_actions set to an array with show inside:

111 lines | app/config/config.yml
// ... lines 1 - 80
easy_admin:
// ... lines 82 - 89
entities:
// ... lines 91 - 103
SubFamily:
// ... line 105
list:
actions: ['-show']
disabled_actions: ['show']
// ... lines 109 - 111

As soon as we do that ... the link vanishes in dramatic fashion! Let's be annoying again: I'll go forward in my browser to get back to the show page URL. Refresh! Dang! Now we get a huge error. The show view is totally gone.

Customizing Action Design

There are two more things you can do with actions: custom actions - we'll talk about those later - and making your actions pretty. That's probably even more important!

I want to tweak how the list actions look... but only for the Genus section. Ok, find its config, go under list and add actions:

114 lines | app/config/config.yml
// ... lines 1 - 80
easy_admin:
// ... lines 82 - 89
entities:
Genus:
// ... lines 92 - 94
list:
// ... line 96
actions:
// ... lines 98 - 114

This time, rather than adding or removing actions, we want to customize them. So instead of using a simple string like show, use an expanded configuration with name: edit. We are now proudly configuring the edit action.

There are a few things we can do here, like icon: pencil and label: Edit:

114 lines | app/config/config.yml
// ... lines 1 - 80
easy_admin:
// ... lines 82 - 89
entities:
Genus:
// ... lines 92 - 94
list:
// ... line 96
actions:
- { name: 'edit', icon: 'pencil', label: 'Edit' }
// ... lines 99 - 114

The icon option - which shows up in a few places - allows you to add Font Awesome icons. For the value, just use whatever comes after the fa-. So, to get the fa-pencil icon, say pencil.

Make the show action just as fancy, with icon: info-circle and a blank label:

114 lines | app/config/config.yml
// ... lines 1 - 80
easy_admin:
// ... lines 82 - 89
entities:
Genus:
// ... lines 92 - 94
list:
// ... line 96
actions:
- { name: 'edit', icon: 'pencil', label: 'Edit' }
- { name: 'show', icon: 'info-circle', label: '' }
// ... lines 100 - 114

OoooOooo. Refresh to see how that looks!

Oh man, it's actually kind of ugly... I need to work on my styling skills. But it totally works!