We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Specifiying Images/CSS/JS in extensions/modules

Development and Programming

Newism's avatar
Newism
30 posts
16 years ago
Newism's avatar Newism

Hey guys,

I was testing some of my extension code and ran into a problem. How do you reference images for a module/extension in the CP?

So far I have tried:

'" . $PREFS->ini('site_url') . $PREFS->ini('system_folder') . "/extensions/lg_twitter_ext/bg.png'

Fails when you browse to a secondary msm site

'/" . $PREFS->ini('system_folder') . "/extensions/lg_twitter_ext/bg.png'

Fails when the user has EE installed in a sub folder

The only reliable way seems to be throwing all the files in a new folder in the themes directory as the user has to type this in manually.

I have seen Solspace call another CP page that returns the JS/CSS but this seems like a bit of overkill.

Any ideas?

       
Leevi Graham's avatar
Leevi Graham
1,143 posts
16 years ago
Leevi Graham's avatar Leevi Graham

I guess I should have posted that under my name not the company 😊

       
Ben @PutYourLightsOn's avatar
Ben @PutYourLightsOn
295 posts
16 years ago
Ben @PutYourLightsOn's avatar Ben @PutYourLightsOn

have you tried with the constants PATH_EXT and PATH_MOD?

PATH_EXT.’lg_twitter_ext/bg.png’

       
Leevi Graham's avatar
Leevi Graham
1,143 posts
16 years ago
Leevi Graham's avatar Leevi Graham

PATH_EXT and PATH_MOD print out the system path… not a url :(

PATH_EXT = /Users/Leevi/Sites/Internal/ee.1.6.4-20080710/ee-admin/extensions/

So no good :(

       
Ben @PutYourLightsOn's avatar
Ben @PutYourLightsOn
295 posts
16 years ago
Ben @PutYourLightsOn's avatar Ben @PutYourLightsOn

what about using $PREFS->ini(‘cp_url’) and removing the index.php?

$url = str_replace('index.php', '', $PREFS->ini('cp_url', false));
       
Leevi Graham's avatar
Leevi Graham
1,143 posts
16 years ago
Leevi Graham's avatar Leevi Graham

@Ben:

The issue with that is that people may be masking their control panel index page. So they could be using admin.php inside a different folder.

I’m starting to think this is one of those 80-20 situations.

       
Ben @PutYourLightsOn's avatar
Ben @PutYourLightsOn
295 posts
16 years ago
Ben @PutYourLightsOn's avatar Ben @PutYourLightsOn

it may well be. good luck!

       
Leevi Graham's avatar
Leevi Graham
1,143 posts
16 years ago
Leevi Graham's avatar Leevi Graham
Well typically i do this:
$this->lib_path        = $PREFS->ini('site_url', TRUE).$PREFS->ini('system_folder', TRUE).'modules/'.$this->module_name.'/lib/';
Even if people mask their CP it will still work since the original CP url is always accessible. Or am i wrong? I guess that’s why Solspace went for the “themes” solution, since that always works..

The issue with that approach is that a msm site has a different site url which doesn’t contain the EE installation. :( So the assets are 404

       
Leevi Graham's avatar
Leevi Graham
1,143 posts
16 years ago
Leevi Graham's avatar Leevi Graham

Bump… Do any Ellislab developers have any suggestions? 😊

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
16 years ago
Mark Bowen's avatar Mark Bowen

Just out of interest is there no way of finding the path of your extension itself and then working back from there?

Just a thought though.

Best wishes,

Mark

       
Leevi Graham's avatar
Leevi Graham
1,143 posts
16 years ago
Leevi Graham's avatar Leevi Graham

I can find the server path… not sure if I can turn that into a url… checking the dev docs now 😊

       
Leevi Graham's avatar
Leevi Graham
1,143 posts
16 years ago
Leevi Graham's avatar Leevi Graham

Ok answering my own post here but I think the best way to reference images and scripts for extensions and modules is to:

  1. create a new folder in the /themes/cp_themes/default/ directory
  2. put all your images / css / js in there
  3. build the url:
$PREFS->ini('theme_folder_url', 1) . "cp_themes/".$PREFS->ini('cp_theme')."/lg_twitter/admin.js"

This way your extension can be tweaked for custom cp themes in the future. Seems ok and is fail proof on msm sites and masked CPs

       
Robin Sowell's avatar
Robin Sowell
13,160 posts
16 years ago
Robin Sowell's avatar Robin Sowell

Seems reasonable from here. I took a look- Tag is using the THEME_FOLDER constant, which is defined in core.system.php

// ----------------------------------------------
//  Theme Paths
// ----------------------------------------------

    if ($PREFS->ini('theme_folder_path') !== FALSE && $PREFS->ini('theme_folder_path') != '')
    {
        $theme_path = preg_replace("#/+#", "/", $PREFS->ini('theme_folder_path').'/');
    }
    else
    {
           $theme_path = substr(PATH, 0, - strlen($PREFS->ini('system_folder').'/')).'themes/';
        $theme_path = preg_replace("#/+#", "/", $theme_path);
    }
    
    define('PATH_THEMES',         $theme_path);
    define('PATH_SITE_THEMES',    PATH_THEMES.'site_themes/'); 
    define('PATH_MBR_THEMES',    PATH_THEMES.'profile_themes/'); 
    define('PATH_CP_IMG',         $PREFS->ini('theme_folder_url', 1).'cp_global_images/');
    
    if (REQ == 'CP')
    {
        define('PATH_CP_THEME', PATH_THEMES.'cp_themes/');
    }
    
    unset($theme_path);

But I’m not dead sure what the preferred approach is. Took a look at the file upload button in publish, it’s using

/** --------------------------------
        /**  Upload link        
        /** --------------------------------*/
        
        $up_img = ''.PATH_CP_IMG.line('file_upload').'" >';

I’m going to shift this down to ‘Modules’ and give the crew a bump on it.

       
Leevi Graham's avatar
Leevi Graham
1,143 posts
16 years ago
Leevi Graham's avatar Leevi Graham

Hey Robin,

Thanks for your input. Using the PATH_CP_IMG variable and the $PREFS->ini(‘theme_folder_url’, 1) method is the only foolproof way I have found so far.

Honestly its not too much extra time to implememnt but it does mean that the installation has a couple of extra steps. Unfortunatley sometimes that’s all it takes to confuse someone.

Cheers

       
Wouter Vervloet's avatar
Wouter Vervloet
758 posts
16 years ago
Wouter Vervloet's avatar Wouter Vervloet

I was wondering if anyone gave this some extra thought (preferably the developers at EllisLab)?

The ideal situation would be to have a directory called css/img/js in your module directory to simplify installation, but how does one get the path to that folder in a foolproof way? I intend to use this module I’m developing myself, but making things easier is never a bad thing…

       
1 2

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.