Chapters
40 Chapters
|
4:19:16
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!
Subscribe to download the code!
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
36.
True Custom Action
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
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.
This tutorial is built on Symfony 6 but works great on Symfony 7!
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": ">=8.2.0",
"ext-ctype": "*",
"ext-iconv": "*",
"composer/package-versions-deprecated": "^1.11", // 1.11.99.4
"doctrine/doctrine-bundle": "^2.1", // 2.5.5
"doctrine/doctrine-migrations-bundle": "^3.0", // 3.2.1
"doctrine/orm": "^2.7", // 2.10.4
"easycorp/easyadmin-bundle": "^4.0", // v4.0.2
"handcraftedinthealps/goodby-csv": "^1.4", // 1.4.0
"knplabs/knp-markdown-bundle": "dev-symfony6", // dev-symfony6
"knplabs/knp-time-bundle": "^1.11", // 1.17.0
"sensio/framework-extra-bundle": "^6.0", // v6.2.5
"stof/doctrine-extensions-bundle": "^1.4", // v1.7.0
"symfony/asset": "6.0.*", // v6.0.1
"symfony/console": "6.0.*", // v6.0.2
"symfony/dotenv": "6.0.*", // v6.0.2
"symfony/flex": "^2.0.0", // v2.4.5
"symfony/framework-bundle": "6.0.*", // v6.0.2
"symfony/mime": "6.0.*", // v6.0.2
"symfony/monolog-bundle": "^3.0", // v3.7.1
"symfony/runtime": "6.0.*", // v6.0.0
"symfony/security-bundle": "6.0.*", // v6.0.2
"symfony/stopwatch": "6.0.*", // v6.0.0
"symfony/twig-bundle": "6.0.*", // v6.0.1
"symfony/ux-chartjs": "^2.0", // v2.0.1
"symfony/webpack-encore-bundle": "^1.7", // v1.13.2
"symfony/yaml": "6.0.*", // v6.0.2
"twig/extra-bundle": "^2.12|^3.0", // v3.3.7
"twig/twig": "^2.12|^3.0" // v3.3.7
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.3", // 3.4.1
"symfony/debug-bundle": "6.0.*", // v6.0.2
"symfony/maker-bundle": "^1.15", // v1.36.4
"symfony/var-dumper": "6.0.*", // v6.0.2
"symfony/web-profiler-bundle": "6.0.*", // v6.0.2
"zenstruck/foundry": "^1.1" // v1.16.0
}
}
23 Comments
Is there a way to add a popup containing a brief description of a given action?
Hey @ahmedbhs ,
That's possible, you can add some custom JavaScript that will intercept the click and show a popup... or even override some core EA templates to add a custom HTML code if needed.
I hope that helps!
Cheers!
Hi, thanks for the great course.
I have a small problem, I am working on adding a custom action in the detail page, and I created a custom twig template for it, and in order for it to function correctly I need to calculate some data in the backend first, and then pass the result to the frontend to use it in the action.
Here is how I am trying to do it:
and in the action template I have:
The problem is that the admin context in the frontend doesn't contain the variable I set in the backend. Is there another way to pass data from backend to frontend.
Hey @Omar-Drb!
I believe you are doing the correct thing. But I think the result is that you will have a variable called
teston the frontend: it will not be some new variable inside ofea. Try{{ dump() }}to see ALL of the variable you have available.Cheers!
Thank you very much for your reply,
I checked it but unfortunately these are the only variables that exists:
I also tried using the search functionality but there were no variable with the name 'test' anywhere
Hmm, dang! Here how it looks like it should work:
A) You call
parent::detail($context). That returns aKeyValueStoreobject.B) You call
$detail->set('test', $data);, which adds this to theKeyValueStoreC) You return the
KeyValueStorefrom the controllerD) A listener from EasyAdmin is called after your controller. It grabs your return value (the
KeyValueStore) - https://github.com/EasyCorp/EasyAdminBundle/blob/4.x/src/EventListener/CrudResponseListener.php#L28E) These are used as the variables when rendering the template - see https://github.com/EasyCorp/EasyAdminBundle/blob/4.x/src/EventListener/CrudResponseListener.php#L37 and https://github.com/EasyCorp/EasyAdminBundle/blob/4.x/src/EventListener/CrudResponseListener.php#L52
So, I don't know what's going wrong. But perhaps you can add some debugging code in the core of EasyAdmin to see what's not working as I expect.
Cheers!
Just wanted to share this insight:
If you need access to the current entity on a action data attribute, you can use this trick:
Ex show a modal if you want to block a user, that asks if you are sure to block user x?
entity is a var that lives in the twig action template, so you can abuse it like this:
Inside your template you can do this:
You still need to activate this function in Symfony:
More info: https://twig.symfony.com/doc/3.x/functions/template_from_string.html
Hey @Rudi-T
That's a nice trick, but it's not clear to me why you have to write the Twig template code inside a HTML attribute?
Cheers!
Hey @MolloKhan, It's to connect a stimulus controller and set the message of a modal.
A bit like the idea highlighted in this tutorial, that you could implement.
In our solution we don't need a custom template for each modal we show. Very dynamic with almost no code maintenance.
Is it possible that this is bugged on the latest version?
Warning: Undefined array key "ea"
Fix only works if I remove the form attribute of the button
Hey @Rudi-T!
Hmm, it's possible, though EasyAdmin hasn't gotten any major upgrades, so nothing "should" have broken. Where does this error come from - like what's the stack trace? And, is your above code (the 3x
setanddoa work-around for the error)?Cheers!
SomeWhere from this:
I tried adding this manually, but didn't do anything. The only workaround was removing the form attribute.
Hmm, yea, something isn't right. I'm actually a bit confused. Here's what I see:
1) We add a new action called
approve.2) We create a custom template to render this action's button -
approve_action.html.twig. This is ONLY responsible for rendering the "Approve" button and shouldn't affect anything else. This is also where your big fix code lives.3) This new button is surrounded by a
formthat will submit to the "approve" action.The confusing part is that, from your stacktrace, the error is coming from the
editaction - from after a form submit. What I can't figure out is how theapprove_action.html.twigis affecting this. When you click "Approve", it it executing theeditaction (and not theapproveaction)? If so, I'd love to see what theactionattribute looks like in theformin your HTML.Cheers!
Hi! In this lesson we added the "Approve" button to approve questions by users with ROLE_MODERATOR. But what I have to do if I want to show Boolean Field "isApproved" on index or other pages for users with lower roles, for example, ROLE_ADMIN in read-only status? I want that admins can see, approved this question or no.
If I did for this field ->setPermission('ROLE_ADMIN') - the field become editable, If I did ->setPermission('ROLE_MODERATOR') - the field disappears, function hideOn... or showOn do not give me the desired result too.
Thanks in advance!
Hey Ruslan,
Just add a
BooleanFieldand display it on PAGE_INDEX. You can render that field as a a label, see https://symfony.com/bundles/EasyAdminBundle/current/fields/BooleanField.html#renderasswitch - it will mean that users will only be able to read the value. And I suppose you can just show it to anyone, but keep the security role as ROLE_MODERATOR for editing only.Cheers!
Thank you, Viktor. It is not so simple. What, if on PAGE_INDEX this field should stay editable for ROLE_MODERATOR and not editable for ROLE_ADMIN?
Hey Ruslan,
I think it's pretty easy actually... just inject the Security service into your controller's constructor and use it to decide either the field should be rendered as a switch or as a label in the
configureFields(). And you can wrap in an if to hide it or make it only readonly on edit page for example.Cheers!
He there
Is there any other way to get the current entity in configureActions without using
The reason i am asking this is because i need a few parameters that is in the entity. I also have a custom template because the call is made by my stimulus controller. The only method i have found now is using callable on one of the link methods:
I have tried
$this->getContext()->getEntity()butgetContext()is null. I'm a bit stuck here, hope you can help.Hey Scott S.
You can only get the entity instance on the detail or edit page, so, you have to check that first, then you can get the instance through the AdminContext, or you can do it manually by getting the entityId from the request
Cheers!
Hello ! Unfortunately with this kind of action in POST, it causes something bad at the front-end. The action becomes a form as agreed, but it will take up a whole line, and the other actions will be shifted below. See for yourself
https://www.noelshack.com/2...
Hey Fabrice!
Hmm, you might be right! But, where/how are you seeing the icons that you posted? On the "index" page, by default, these should be rendered as a drop-down. And if I call
showEntityActionsInlined()to expand them, I still don't see the bad behavior. Is this on some other page?Cheers!
Hello! Yes, this is on one of my projects, in which the actions are rendered inlined. So maybe it's no wonder it doesn't do it for you. Yet the method of application is the same.
Hi!
Yea... that's super weird. I can't explain why your version looks bad but mine does not. But, in theory, it makes sense: a form will often be styled differently (often with display: block) than a normal button, which could cause this. In that case, you'll need to add some custom CSS, likely to set the form to display: inline. I'm doing some guessing, but that's the most likely culprit!
Cheers!
"Houston: no signs of life"
Start the conversation!