Block Assets

Permalink
Hi I am trying to setup some assets for a block I have created but the assets are not being added into the header. Am i doing it right? I have this code within my block controller.

public function on_start()
{
$al = \Concrete\Core\Asset\AssetList::getInstance();
$al->register('javascript', 'sapphire_slider', 'blocks/sapphire_slider/slider_js/jquery.themepunch.revolution.min.js');
$al->register( 'javascript', 'sapphire_slider', 'blocks/sapphire_slider/slider_js/jquery.themepunch.tools.min.js');
$al->register( 'javascript', 'sapphire_slider', 'blocks/sapphire_slider/slider_js/run.js');
$al->register('css', 'sapphire_slider', 'blocks/sapphire_slider/slider_css/layers.css');
$al->register('css', 'sapphire_slider', 'blocks/sapphire_slider/slider_css/navigation.css');

$al->registerGroup('sapphire_slider', array(
array('css', 'sapphire_slider'),
array('javascript', 'sapphire_slider')
));

}

public function registerViewAssets($outputContent = '')
{
$this->requireAsset('sapphire_slider');
}

 
mnakalay replied on at Permalink Reply
mnakalay
Hello,
Just a couple of comments that might or might not help, you'll have to check.

First for this to work, you block needs to be in the application folder, not in a package.

Another potential problem is that you are giving all your assets the same label sapphire_slider

I haven't tested that, it is only an educated guess, but I think that different assets of the same type (javascript or CSS) can't be registered under the same name or they overwrite each other.

Using asset group as you do is what allows you to have them all under the same name (that's the second argument in your register call).

So I suggest you modify your asset names so for instance instead of sapphire_slider for the main js file you have sapphire_slider/main and for the run file you have sapphire_slider/run.

Then you modify your registerGroup call to add all the assets

I think it should work
mnakalay replied on at Permalink Reply
mnakalay
Actually I checked the doc and I am correct in my assumption that you can't reuse handles (or names) for the same type of asset.

Read this:https://documentation.concrete5.org/developers/assets/registering-an...

Under "Handle Uniqueness" it says "A handle should be unique across type and handle. This means that if you're registering an asset with the handle "mysite/calendar", you can register one CSS file and one JavaScript file with both that same handle."
JackVanson replied on at Permalink Reply
Thanks for your quick reply.

I have made the changes that you listed above but still no luck.

The block is not in a package. It is in the application folder.

this is my code now:
public function on_start()
    {
        $al = \Concrete\Core\Asset\AssetList::getInstance();
        $al->register('javascript', 'sapphire_slider_revolution', 'blocks/sapphire_slider/slider_js/jquery.themepunch.revolution.min.js');
        $al->register(  'javascript', 'sapphire_slider_tools', 'blocks/sapphire_slider/slider_js/jquery.themepunch.tools.min.js');
        $al->register( 'javascript', 'sapphire_slider_run', 'blocks/sapphire_slider/slider_js/run.js');
        $al->register('css', 'sapphire_slider_layers', 'blocks/sapphire_slider/slider_css/layers.css');
        $al->register('css', 'sapphire_slider_navigation', 'blocks/sapphire_slider/slider_css/navigation.css');
        $al->registerGroup('sapphire_slider', array(
            array('css', 'sapphire_slider_layers'),
            array('css', 'sapphire_slider_navigation'),
            array('javascript', 'sapphire_slider_revolution'),
            array('javascript', 'sapphire_slider_tools'),
            array('javascript', 'sapphire_slider_run')
        ));
JackVanson replied on at Permalink Reply
Okay i have got it working now. thank you all of the able was very helpful.

I was missing some extra JS files.

Thanks

Jack