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

Actions Config

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 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:

... 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():

... 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():

... lines 1 - 10
class GenusNote
{
... lines 13 - 115
public function __toString()
{
return (string) $this->getNote();
}
}

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

... 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:

... 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:

... 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:

... 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:

... 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:

... 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:

... 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:

... 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!

Leave a comment!

21
Login or Register to join the conversation
Default user avatar
Default user avatar jose | posted 5 years ago | edited

Following along with the tut...

This...


SubFamily:
    class: AppBundle\Entity\SubFamily
    list:
        actions: ['-show']

... didnt work for me.

But this...


SubFamily:
    class: AppBundle\Entity\SubFamily
    disabled_actions: ['show']

...did.

The docs show both approaches, but describes the first as "removing" the action and the second as "disabling" it. Seems to me that the disabled_actions did the trick and also didnt allow the hack you show in the vid. It gives me a juicy 403 error when I try it. I'm cool with the second approach just wanna make sure I'm understanding the usage.

BTW, thx for the vids. For the first time ever I feel like a grown-up developer and not just some hack gluing php thingys together. The OOP stuff has really clicked for me.

6 Reply

Yo jose!

Yea, you've nailed it. When you do actions: [-show], it's superficial. With this, you should see the link disappear... but it's still totally accessible if you click on it. With disabled_actions... it's totally disabled. A juicy 403 error when you try to access it ;). I actually use both config by the end of the video (https://knpuniversity.com/screencast/easyadminbundle/actions#disabling-actions), but really, only disabled_actions should be necessary: it's the "stronger" of the two (if an action is disabled, there will of course not be any links to it).

Translation, you're totally understanding things correctly! And I'm SO glad the dug the OOP tutorials! If that clicked... you are unstoppable. I remember when OOP clicked for me - it was one of the few programming things that I really just needed someone else to explain to me. It's a beautiful (& dangerous?) feeling.

Cheers!

Reply
Default user avatar
Default user avatar Andy @ melt | jose | posted 5 years ago

FWIW... I have the same experience. The only thing that makes the "Show" action go away is to add the "disabled_actions: ['show']" line directly under the "SubFamily:" section in config.yml. The ["-show"] doesn't do anything no matter where it's located.

Reply

Hey Andy,

Thanks for sharing it with others. Probably EasyAdminBundle slightly changed its behavior in newer versions or just tweaked their templates, so the problem could be in overridden templates of EasyAdminBundle, if you have ones.

Cheers!

Reply

Hello SymfonyCast team! - didn't find a better place to write my question - so here it is: I'm trying to make a crud for my ProductSupply class with EasyAdmin, but the problem is that on details(show) of the ProductSupply i need to make a view of all ProductSupplyList objects(Associations) that belong to ProductSupply entity NOT as dropdown list, but as a form-list with ability to add/remove them like an index of some object in EA inside a show of parent object. Is it somehow possible? Would be grateful for any help)

Reply

Hey Andrejus,

So, you need to add a form on the "detail" page of ProductSupply that will help you to add/remove some items to the collections - yeah, sounds kinda complex :) I don't think there's something out of the box, as we're talking about detail page, not about new/edit page that is based on forms. You probably should think of be able to add those on the new/edit form, and on edit form you probably can make some fields read-only to avoid modifying them unintentionally. And then try to use custom form types, though I think it should work out of the box of EA as well. Or another solution would to override some templates and actions, specifically, you need to override detail.html.twig EA template and render a custom form on that page, probably somewhere below of the main content, so that you can leverage "{{ parent() }}" call to avoid overriding everything and just add your form below the parent content. Then, you can try to override detail() action... or probably better to send that form to a different new action that will handle it and redirect you back to the ProductSupply detail page. Or, you can even handle that form via fancy AJAX request to stay on the detail page, but it may be even more tricky, so it's up to you :)

I hope this helps!

Cheers!

Reply

Hey Victor,
It really is! I appreciate it. I managed to make a collection field in edit/new - but I can't find any info on few things. As i mentioned before I have a ProductSupply and ProductSupplyList Entities with a relationship OneToMany(mapped by productSupply). And used the custom form type, so now in my ProductSupply class i managet to generate a collection that is a ProductSupplyListType, but can't find out how I can define a productSupply field as a current ProductSupply that I'm in. And the second issue is when I'm trying to add to ProductSupplyLists(it's a collection field of ProductSupply entity) it doesn't save to db. Maybe I have an issue with a relation? Sorry for my bad English ((

Reply

Hey Andrejus,

Hm, it's difficult to say what may go wrong, you probably better debug things... Most probably you just need to persist those new entities in the collection in case you managed to render the form as you want with all the necessary fields. You can do it manually by overriding createEntity/persistEntity/updateEntity methods from the EA, depends on which page you're on. In those methods first of all you can debug if the entity looks ok and contains all the entities in the collection you sent in the form - if everything is ok, probably iterate over the collection and persist easy entity in the collection so that the flush method saves them in the DB. As a second alternative, you may want to add cascade persist for that collection in your mapping and Doctrine will automatically do that persisting.

I hope this helps!

Cheers!

Reply

Thx again Victor, I managed to figure that out and used cascade and added 'by reference' => false. Answers were really helpful. Have a nice day!

Reply

Hey Andrejus,

Ah, yes, "by_reference" form field option was also important here, good catch!

I'm happy to hear it was helpful ;)

Cheers!

Reply
Alessandro-D Avatar
Alessandro-D Avatar Alessandro-D | posted 3 years ago

Hi Ryan,
When I add the option "show" to the list view, I correctly see the link, but when I press it, I have a 500 error that I am not sure how to overcome.
The error says:
Variable "form" does not exist
in vendor/easycorp/easyadmin-bundle/src/Resources/views/default/new.html.twig (line 1)

Any idea on what might cause it?

Reply

Hey Alessandro,

Hm, interesting... what version of EasyAdmin do you have? This sounds like a bug, probably if you could upgrade to the latest EasyAdmin it will fix the problem. Or probably the variable was renamed to form but in your application it calls differently. Did you override any parts of EasyAdmin controllers? Did you pass any forms to it? It might be you just need to rename variable of the form you're passing to the template, or probable you just override some controllers but forgot to pass all the necessary variables?

I hope this helps!

Cheers!

Reply
Diaconescu Avatar
Diaconescu Avatar Diaconescu | posted 5 years ago

I don't get get a SubFamily link in Show view. Only a plain div.

Reply

Hey Diaconescu

Could you show me your code? so I can help you debugging

Cheers!

Reply
Diaconescu Avatar
Diaconescu Avatar Diaconescu | posted 5 years ago

Google for petre-symfony github. You'll see a link GitHub - petre-symfony/getting-crazy-with-form-themes. Follow the link. Go to repositories link. You'll see EasyAdminBundle-KNP-tutorial repository. Then go to branch 3.Actions_Config. Copy that branch if you want. Code until commit 43490721d44de584c5a40013b3347b9051f0d8d5 (Removing Actions)

Cheers.

Reply

Hey Diaconescu

You don't have a link to show action because you have it disabled in your configuration
https://github.com/petre-sy...

Cheers!

Reply
Diaconescu Avatar
Diaconescu Avatar Diaconescu | posted 5 years ago | edited

You speak about the lines


Subfamily:  
      class: AppBundle\Entity\Subfamily
      list:
        actions: ['-show']

But they exist in commit 43490721d44de584c5a40013b3347b9051f0d8d5(Removing Actions) In commits before this one this lines doesn't exist so show action is allowed there. SubFamily link in Show view genus is also a link before disabling it.

Reply

Hey Diaconescu
I'm not sure why this is happening (I just checked your code and looks correct), I believe is something internal about the bundle, but updating it was not the answer, I'll keep digging to find a reason. If in the meanwhile you find the solution, let us know :)

Cheers!

Reply

Hey MolloKhan!

I found the problem - it's VERY small :). In your config, you have:


        Subfamily:
            class: AppBundle\Entity\subfamily

But, it should be:


        Subfamily:
            class: AppBundle\Entity\SubFamily

Notice the capital F in SubFamily. Yes that's the problem! Actually, it's a mystery to me that this works at all! EasyAdminBundle must "correct" this somehow... because the SubFamily admin section DOES work. But, this small inconsistency was enough for the admin to think that there was no SubFamily section to link to. As soon as I changed the f to F, I got the link.

Btw, nice job following along with the tutorial! It looks like you're coding through really well :).

Cheers!

Reply
Default user avatar
Default user avatar Chahir Jbali | posted 5 years ago

paypal unavailable in my country can i pay with Bitcoin ?

Reply

Hey Chahir Jbali!

Ah yes, sorry about that! Can you email us - hello@knpuniversity.com and tell us what country you are in? We can definitely work together to find something that works :).

Cheers!

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