How to Register Assets Sitewide from Package

Permalink
Does anyone understand how to register/require assets sitewide from a package? In Razor Commerce I've used the package controller on_start() to register the assets. Then for single pages, I require them in the single page controller on_start(). Where I'm stuck is on an effective way to require some CSS for a specific pagetype, in this case "product". The package creates the pagetype, but does not provide a page template for it. Instead it uses "full" template.

What I've tried is requiring the package main CSS in the package controller on_start using:

Theme::requireAsset('css', 'razor_css');


The result is yes the CSS is included, however, it creates a strange Javascript error (which might be a bug). It actually inserts this code into a completely different file, a javascript file:

<script type="text/javascript">ccm_addHeaderItem("/c5-commerce/packages/razor/razor.css", "CSS")</script>


I'm not sure why registering a CSS file would lead to this "ccm_addHeaderItem()" being stuffed into a javascript file, but clearly this method does not work anyway.

 
JohntheFish replied on at Permalink Reply
JohntheFish
The code you are seeing is a lazy load of the css, adding it to the page header using js after the page has loaded.

It would normally be used to load a css asset after the page head section is complete, or even after the page has fully rendered.

As to why. I don't know.
goldhat replied on at Permalink Best Answer Reply
Thanks. I found a better way for this situation - created a pagetype controller for products then put the requireAsset into the on_start.
andrew replied on at Permalink Reply
andrew
Was just about to post that you should use a controller. Yeah, this is the best solution.
everden replied on at Permalink Reply
For clarification this applies to version 8 (tested using 8.5.1 likely to work with earlier versions).

Additionally if anyone needs to do this you can get the controller from the page using an event before render then continue with registering assets how you usually would from the controller.

\Events::addListener(
            'on_before_render',
            function ($e)  {
                $e->getArgument('view')->getPageObject()->getPageController();
            }
 );