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!
21.
Custom Stimulus JavaScript Controller
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
}
}
14 Comments
If does not work for anyone under symfony6 add to the DashboardController
Hey Jony,
Thanks for this hint! Yeah, you should have this to make it work... we did it in previous chapters: https://symfonycasts.com/screencast/easyadminbundle/assets#codeblock-9cb529831f - so if you follow this course from the beginning - you should have this.
Cheers!
Yes, Victor.
In my case was that I called the CSS with another name than
adminso: If you follow the tutorial there is no problem when admin.js include the admin.css due to the name being the same in the->addWebpackEncoreEntry('admin');but in my case, I calledadminstylesso I had->addWebpackEncoreEntry('adminstyles');.When I created an
admin.jsthat includesadminstyles.cssin the tutorial we updated thewebpack.config.jsbut no reference to taking care of theconfigureAsset()method of DashboardController, obviously because it is the same name.So the advice here is: "if you are taking a look at a tutorial, follow the tutorial! " XDD.
Hey Jony,
Ah, I see! Yes... that's a good practice to name the JS and CSS files with the same exact name :) I think it's enough, because you have the file extension anyway, so it's clear that
admin.cssfile is for "styles" foradmin.js:)Cheers!
What is the best way to render data in real time? I want to show in a field the subtotal price of items that are selected in another collection field.
The controller:
Hey @Courtney-T!
Sorry for the slow reply! You've got the general approach that I would use: a Stimulus controller that responds when those fields changes, and does the calculation. Is this working? The only notes I'd have are:
A) If calculating client-side is good enough, awesome. Otherwise, you can always send the data back to an Ajax endpoint.
B) Some of the JavaScript to fetch the data looks a bit complex - as you probably now. Like, grabbing the
itemclass elements, thentextContenton them (does that work?getElementsByClassNamereturns a collection). I'm not sure if there's an easier way to do it - it depends on what the fields look like (are theseoptionelements)? I would normally recommend perhaps adding some extradataattributes to the elements to help with this - e.g. adata-pricewith the actual price, so you don't need to parse the text - but adding extra attributes tooptionelements is not something the form theming system is built to do easily, I believe.So, in short, while it's a bit ugly, I think you're on the right track here.
Cheers!
Hello, I have a question if it's possible setting a data-controller attribute to a panel in stead of a field. To create a controller surrounding multiple forms at the same time. I'm not able to get this working.
The following code works to add an extra class value to the panel:
But the following code doesn't do anything:
The goal is to group a couple of fields and create a single controller. Then adding the fields as a target.
Best regards
Hey Coding010,
Yeah,
setFormTypeOptions()won't work for thataddPanel()because in the source code EA just does not render any additional things like this. There's another method calledsetCssClass()but it will help to set a CSS class to that whole panel wrapper and that's it. But if you want to add some data attributes - I think you should override a template for this, in specificvendor/easycorp/easyadmin-bundle/src/Resources/views/crud/form_theme.html.twigone - you will find a<div class="form-panel">tag there, add your data controller there.I hope this helps!
Cheers!
Hi Victor, thanks for your reply.
I browsed around a bit on EasyAdmin's Github page and found this newly added feature:
https://github.com/EasyCorp/EasyAdminBundle/pull/5488.
It was added in Easy Admin 4.4.3.
This helped me out, as now I can set a Stimulus controller data attribute on the
<body>element of a crud.It's not specifically around a group of field as I first wanted, but this should do the trick.
Here a quick code example:
Override the templates via the CrudController.
Note that
'crud/layout'doesn't work, even though the examples on the Symfony documentation page say it should.And then create the template to override:
This adds the
data-controllerattribute to thebody.Hope this helps out others with this "problem" as well.
Hello! If I start a stimulus application, this way
or this way
{{ encore_entry_script_tags('admin') }}then actions menu(3 dorts) stops working. It doesn't expand if i click it.
I found the problem it's related to the bootstrap import
import { Modal } from 'bootstrap';Oh yea, that pesky thing! Good job debugging that!
I know it is out of scope, but what if we do not want admin related (stimulus) controllers to load at all on non-admin pages? Let's say someone manipulate the DOM.
Hey julien_bonnier
Great question. I had the same when developed some internal stuff. And there is pretty easy question. IIRC Stimulus controller loader is configured in
assets/bootstrap.jsso you can duplicate it and configure to load a separate folder for admin, so you will have everything separated.Cheers!
"Houston: no signs of life"
Start the conversation!