3

I would like to implement a custom theme, and in my theme, I need to change the way that JavaScript is rendered. By default, Drupal 8 will use the Drupal\Core\Asset\JsCollectionRenderer class, and create a bunch of html_tag render arrays.

I want to change this to allow returning just the name of the JS file, so I can present it in a different way using Twig.

It would appear that I need to define a ServiceProvider class that can register a change to asset.js.collection_renderer (core.services.yml), and implement a custom class to replace JsCollectionRenderer's behaviour.

However, it looks as though I would need a custom module in order to get the ServiceProvider's register() method to fire.

If possible, I wanted to avoid needing a custom module just to have a custom theme, because that seems like needless overhead, so I'm wondering if there's a way to fully encapsulate this behaviour in a single theme, or whether my approach is misguided in some way and there's a better way to handle this.

1

Themes have no mechanism to provider or alter services. A custom module is needed.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.