I have a file: etc/adminhtml/system.xml where I have added some form fields. However I need to add a custom script being executed while someone types in one of the fields.
How can I include a .js in this page? It appears I can't simply include it like I would do in a simple xml magento 2 page
<head>
<css src="Vendor_Module::test.css"/>
<link src="Vendor_Module::test.js"/>
</head>
1 Answer 1
Create in one of your extensions the file view/adminhtml/layout/adminhtml_system_config_edit.xml and put your content in there.
adminhtml_system_config_edit is the layout handle for the configuration page.
-
Thank you but can you be more detailed? I don't have any files on the
view/adminhtmlfolder, as my only page is set at theetc/adminhtml/system.xmlso how do I declare that the system.xml has an external test.js file? I tried what you said and added fileview/adminhtml/layout/adminhtml_system_config_edit.xmlwith the code:<layout version="0.1.0"> <adminhtml_system_config_edit> <reference name="head"> <action method="addJs"><file>test.js</file></action> </reference> </adminhtml_system_config_edit> </layout>but it wasn't included in the outputIrini Koutaki– Irini Koutaki2017年07月12日 13:14:21 +00:00Commented Jul 12, 2017 at 13:14 -
Maybe I should add the .js and .css in the
etc/acl.xmlfile, or theetc/adminhtml/menu.xmlfile?Irini Koutaki– Irini Koutaki2017年07月12日 15:40:54 +00:00Commented Jul 12, 2017 at 15:40 -
1you cannot add js and css files from the system or acl xml files. They serve different purposes. You can add resources from the layout files as explained in the answer. For every configuration page the layout files named
adminhtml_system_config_edit.xmlwill be loaded. that's where you can add your custom js or custom css.Marius– Marius2017年07月12日 15:46:02 +00:00Commented Jul 12, 2017 at 15:46 -
OK but I would like some sample code please. Right now I tried to write some sample code in the
view/adminhtml/layout/adminhtml_system_config_edit.xmlbut the .js file was not includedIrini Koutaki– Irini Koutaki2017年07月12日 16:07:38 +00:00Commented Jul 12, 2017 at 16:07
requirejs-config.js(I posted it as a reply below). However in my case I wish to include the .js file only in the system page of my module, not in every single page of the admin panel. Any ideas on that?view/adminhtml/layout/adminhtml_system_config_edit.xmlthat was suggested in the first reply didn't work... I keep searching thoughview/adminhtml/requirejs-config.jswith my own code inside and it seems to be loaded in every single page of the admin panel. I don't like this practice, but it's the only thing working right now. I also get this error:Uncaught ReferenceError: config is not defined at requirejs-config.js:423 at requirejs-config.js:424 at requirejs-config.js:428 (anonymous) @ requirejs-config.js:423 (anonymous) @ requirejs-config.js:424 (anonymous) @ requirejs-config.js:428