How to create another image theme setting?

How to create another image theme setting?

door Mariusz Mit -
Aantal antwoorden: 2

Hello

I added such code in my custom theme:

    // Background image setting.
    $name = 'theme_custom/backgroundimage2';
    $title = 'backgroundimage2';
    $description = 'backgroundimage_desc';
    $setting = new admin_setting_configstoredfile($name, $title, $description, 'backgroundimage2');
    $setting->set_updatedcallback('theme_reset_all_caches');
    $page->add($setting);

Setting seems like OK, I can upload an image, the image is visible under settings tab, but when I try to echo this image using such code

$PAGE->theme->setting_file_url('backgroundimage2', 'backgroundimage2')

it shows such url //mydomainname.com/pluginfile.php/1/theme_custom/backgroundimage2/-1/test.jpg

which points to the not found file.

Gemiddelde van de beoordelingen:  -
Als antwoord op Mariusz Mit

Re: How to create another image theme setting?

door Davo Smith -
Foto van Core developers Foto van Particularly helpful Moodlers Foto van Peer reviewers Foto van Plugin developers

Assuming you are basing your theme on theme_boost, take a look in theme/boost/lib.php at function theme_boost_pluginfile(): https://github.com/moodle/moodle/blob/master/theme/boost/lib.php#L82

This is the callback function that Moodle uses whenever a file is requested, to check that a) the file exists and b) that the current user has permission to view it.

As you should be able to see from that function, it currently only serves up files that are found in the 'logo', 'backgroundimage' or 'loginbackgroundimage' file areas - if you want users to be able to access files in another file area, then you'll need to extend the code to add it to the list.

Gemiddelde van de beoordelingen:  -
Als antwoord op Mariusz Mit

Re: How to create another image theme setting?

door Michael Milette -
Foto van Core developers Foto van Documentation writers Foto van Particularly helpful Moodlers Foto van Plugin developers Foto van Testers Foto van Translators
Hi Mariusz,

The easiest way to figure out how to do a particular type of theme settings is to look at the source codes in other themes.

For example, the Trema theme has a setting for you to upload carousel slide images and also a background image for the login page.

You can learn a lot and save a lot of time by simply watching how other developers have already done it.

Best regards,

Michael Milette
Gemiddelde van de beoordelingen:  -