Dear all,
I will admit that being a recent addition that 'admin_setting_configstoredfile' is not documented well anywhere yet, but it does support multiple images etc. So, using Julian's Essential theme as an example - https://github.com/moodleman/moodle-theme_essential :
So, in settings.php define your images:
$name = 'theme_essential/slide1image';
$title = get_string('slide1image', 'theme_essential');
$description = get_string('slide1imagedesc', 'theme_essential');
$setting = new admin_setting_configstoredfile($name, $title, $description, 'slide1image');
$setting->set_updatedcallback('theme_reset_all_caches');
$settings->add($setting);
...
$name = 'theme_essential/slide2image';
$title = get_string('slide2image', 'theme_essential');
$description = get_string('slide2imagedesc', 'theme_essential');
$setting = new admin_setting_configstoredfile($name, $title, $description, 'slide2image');
$setting->set_updatedcallback('theme_reset_all_caches');
$settings->add($setting);
...
$name = 'theme_essential/slide3image';
$title = get_string('slide3image', 'theme_essential');
$description = get_string('slide3imagedesc', 'theme_essential');
$setting = new admin_setting_configstoredfile($name, $title, $description, 'slide3image');
$setting->set_updatedcallback('theme_reset_all_caches');
$settings->add($setting);
...
$name = 'theme_essential/slide4image';
$title = get_string('slide4image', 'theme_essential');
$description = get_string('slide4imagedesc', 'theme_essential');
$setting = new admin_setting_configstoredfile($name, $title, $description, 'slide4image');
$setting->set_updatedcallback('theme_reset_all_caches');
$settings->add($setting);
Then in 'lib.php' there are two processes:
function essential_process_css($css, $theme) {
...
// Set Slide Images.
$setting = 'slide1image';
// Creates the url for image file which is then served up by 'theme_essential_pluginfile' below.
$slideimage = $theme->setting_file_url($setting, $setting);
$css = essential_set_slideimage($css, $slideimage, $setting);
$setting = 'slide2image';
$slideimage = $theme->setting_file_url($setting, $setting);
$css = essential_set_slideimage($css, $slideimage, $setting);
$setting = 'slide3image';
$slideimage = $theme->setting_file_url($setting, $setting);
$css = essential_set_slideimage($css, $slideimage, $setting);
$setting = 'slide4image';
$slideimage = $theme->setting_file_url($setting, $setting);
$css = essential_set_slideimage($css, $slideimage, $setting);
return $css;
}
function essential_set_slideimage($css, $slideimage, $setting) {
global $OUTPUT;
$tag = 'setting:'.$setting.'';
$replacement = $slideimage;
if (is_null($replacement)) {
// Get default image from themes 'images' folder of the name in $setting.
$replacement = $OUTPUT->pix_url('images/'.$setting, 'theme');
}
$css = str_replace($tag, $replacement, $css);
return $css;
}
(assume double '[' for the settings as TinyMCE seems to be dropping them) and
function theme_essential_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array()) {
if ($context->contextlevel == CONTEXT_SYSTEM and $filearea === 'logo') {
$theme = theme_config::load('essential');
return $theme->setting_file_serve('logo', $args, $forcedownload, $options);
} else if ($context->contextlevel == CONTEXT_SYSTEM and $filearea === 'slide1image') {
$theme = theme_config::load('essential');
return $theme->setting_file_serve('slide1image', $args, $forcedownload, $options);
} else if ($context->contextlevel == CONTEXT_SYSTEM and $filearea === 'slide2image') {
$theme = theme_config::load('essential');
return $theme->setting_file_serve('slide2image', $args, $forcedownload, $options);
} else if ($context->contextlevel == CONTEXT_SYSTEM and $filearea === 'slide3image') {
$theme = theme_config::load('essential');
return $theme->setting_file_serve('slide3image', $args, $forcedownload, $options);
} else if ($context->contextlevel == CONTEXT_SYSTEM and $filearea === 'slide4image') {
$theme = theme_config::load('essential');
return $theme->setting_file_serve('slide4image', $args, $forcedownload, $options);
} else {
send_file_not_found();
}
}
Then in the css (in this example 'slides.css'):
.da-slide .da-img1 {
background: url(setting:slide1image) no-repeat 0 0;
}
.da-slide .da-img2 {
background: url(setting:slide2image) no-repeat 0 0;
}
.da-slide .da-img3 {
background: url(setting:slide3image) no-repeat 0 0;
}
.da-slide .da-img4 {
background: url(setting:slide4image) no-repeat 0 0;
}
Assume double '[' for the settings as TinyMCE seems to be dropping them.
So what is going on is that in 'settings.php' you define the setting with the filearea of 'slideXimage' and the setting name of 'slideXimage' being the postfix bit if 'theme_essential/slideXimage'. Then the process css code in lib.php generates URL's that refer to 'pluginfile.php' with some parameters for the image. Then when the web page loads the browser gets the images by calling 'pluginfile.php' which in turn finds your 'theme_essential_pluginfile' function which then serves up the file from the storage area using the 'setting_file_serve' method. Confusion may arise from the name of the setting and the name of the file area being the same in this example (i.e. the same parameter to 'setting_file_url' but in reality its setting and filearea - order not sure), but to explain that I would need to look up the function documentation in the files and this post is from memory! But I hope it helps to get you going.
Cheers,
Gareth