Current theme directory programmatically? Documentation looks outdated (deprecated function suggested)

Current theme directory programmatically? Documentation looks outdated (deprecated function suggested)

by Olja Petrovic -
Number of replies: 5

I was wondering what is the best way to refer to the current theme directory in code.

I found a page in dev documentation

http://docs.moodle.org/dev/Theme_directory_guide

But it is out of date since it says to use function current_theme() which is deprecated and found in deprecatedlib.php (documentation should be updated)

Comment in deprecatedlib.php says to use $PAGE->theme->name, but that only gives the theme name, I also need the theme directory.

I thought I'd use $CFG->themedir expecting it to be said to document root slash theme if not otherwise specified, but find it empty (moodle 2.3). How is it supposed to work?

Thank you in advance, and thank you for all the great work.

Average of ratings: -
In reply to Olja Petrovic

Re: Current theme directory programmatically? Documentation looks outdated (deprecated function suggested)

by Richard Oelmann -
Picture of Core developers Picture of Plugin developers Picture of Testers

Hi Olja,

not sure about the rest of it, but the $CFG->themedir is, I believe, a variable which can be set in the moodle config file which allows you to set an alternative location for your theme directory and if it is not set in the moodle config.php then it would be empty and themes are found from their default location in the moodle folder.

Richard

In reply to Olja Petrovic

Re: Current theme directory programmatically? Documentation looks outdated (deprecated function suggested)

by Jean-Michel Védrine -

Once you have the theme name using $PAGE->theme->name just append $CFG->themedir .'/' (local path before it to get what you want.

If $CFG->themedir is empty then it's just the default value $CFG->dirroot . '/theme/' that you need to append.

In reply to Jean-Michel Védrine

Re: Current theme directory programmatically? Documentation looks outdated (deprecated function suggested)

by Danny Wahl -

actually there's currently an issue (MDL-36065, MDL-35138) with $CFG->themedir so I suppose you should do something like this:

if (isset($CFG->themedir)) {
    $theme_path = $CFG->themedir . '/' . $PAGE->theme->name;
} else {
$theme_path = $CFG->dirroot . '/theme/' . $PAGE->theme->name;
}

and of course there are other issues/bugs with using $CFG->themedir like MDL-37436

In reply to Danny Wahl

Re: Current theme directory programmatically? Documentation looks outdated (deprecated function suggested)

by Olja Petrovic -

Thank you all very much!

I'll do that, and put the logic in a function so I don't repeat it in code.

Interesting tracker links, thanks Danny for all the work.

Maybe I won't use a custom themedir for now, but let's hope in the future all might work, might as well prepare for it now. I'll add a cautionary comment to my code also, about the bugs smile

Have a good day.

In reply to Olja Petrovic

Re: Current theme directory programmatically? Documentation looks outdated (deprecated function suggested)

by Robert Allerstorfer -

Retrieve the current theme directory this way:

$theme_dir = $PAGE->theme->dir;

Tested with Moodle 2.6