Add javascripts to CMS in SS4?

Silverstripe Version:
4+
Question:
In SS3, tihs is how I add js to the CMS

LeftAndMain::require_javascript('path_to/js'); // in _config.php

How do I do this in SS4? PS: require_javascript is no longer available.

Thanks in advance!

You should be able to use the LeftAndMain::$extra_requirements_javascript config variable to require extra JS. There’s an example here but it’s marked as deprecated due to the gradual move to React.

I think what’s being deprecated is the use of entwine, but using extra_requirements_javascript is still valid.

Thanks guys, I’ve found it the other day right after I asked the question (couldn’t withdraw the post, because I couldn’t find it - I guess there must be some caching mechanism behind)

this is how I added into mysite.yml

SilverStripe\Admin\LeftAndMain:
  extra_requirements_javascript:
    - '/resources/cms-js/cms.js'

Glad you sorted it. Your post was flagged for review (maybe because it was your first post?) so that’s probably why you couldn’t find it!

Doing this will not work if you run your project in a subfolder, since you’re using a path relative from the site root. Instead it would be better to use the “expose configuration” and use a module relative path, e.g.

SilverStripe\Admin\LeftAndMain:
  extra_requirements_javascript:
    - 'mycompany/myproject: cms-js/cms.js'