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!
33.
Conditionally Disabling an 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
}
}
11 Comments
What about disabling DELETE action on both detail and index pages, based on some if condition ?
Hey @ahmedbhs
You can do what Ryan did here for each page where you want to apply that logic
https://symfonycasts.com/screencast/easyadminbundle/conditional-action#codeblock-2188da90bb
Cheers!
This example is not working anymore. And there is no clue shown how to fix this.
But I found a solution. Please give me a comment if we can do better than this
public function configureActions(Actions $actions): Actions
Hey @Mumm
The reason your code wasn't working with
staticwas because you're using$thiswithin it. Astaticclosure cannot access anything outside it's scope. You can see more detail in the PHP documentation.Hope this helps clear things up!
Hey @Mumm
Can you tell me what error you got, or why it didn't work? I see your example very similar to the one in this chapter
Cheers!
It seems static makes the difference. The actual example will throw an exception while the fn concept is working out-of-the-box
(php 8.3 + SF 7.0.6)
That's interesting! It might come from PHP's latest version
Cheers!
Hello, is it possible to add conditions to each row of data somehow?
Can't attach a screenshot, so I'll try to describe the issue:
I have a BooleanField "enabled" displayed as a switch and the idea is to not allow current user to disable himself, so switch should become inactive for the logged in user somehow.
Hey Vitalii,
About screenshots - you can't insert them directly in comments, but you can upload them to a public image hosting, e.g. Imgur and link to them in the comments :)
About your question - I think
setPermission()method on thatBooleanFieldis what you need. Otherwise, the only solution would be to override some internal EA templates for the specific controller and write some logic there, e.g. seesetTemplatePath()on the sameBooleanFieldobject. Though, keep in mind that even if you disable that switch - it may be still possible to send the request to that endpoint, it's up to you if it's ok for you and if this frontend disabling enough, or if you need to also override some internal PHP logic to make it not throw on any such attempts.I hope this helps!
Cheers!
Hi, Is there any difference between putting
static functionand just putfunctionin Closure?Hey Sergio,
Sure, there is a difference! But the difference is subtle :) Basically, if you need to use "$this" inside the closure to access the context of the current class - you can't declare that function as "static", as you may already know from static vs non-static methods :) And that's mostly it. Well, technically, for performance reasons, it's better to make your closures as static if you can... but if you have to use "$this" inside - nevermind about that static :)
Cheers!
"Houston: no signs of life"
Start the conversation!