MDL-33640 - change $templatesinitialized to an array; improve naming and automate use of template

* $templatesinitialized is now an array, so that subsequent calls to initialise_filepicker which request different repositories will include those (and only those) templates which it requires but have not yet been included
* The get_template method has also been renamed to get_upload_template (and the template to "uploadform_" followed by the repository type), since it only applies to upload forms
* If a plugin provides a get_upload_template method, the template it returns will now automatically be used instead of the standard uploadform template when generating an upload form
This commit is contained in:
Paul Nicholls 2012-08-09 09:43:53 +12:00
parent 013cf2878c
commit ec3eaa2c38
2 changed files with 12 additions and 8 deletions

View file

@ -2614,7 +2614,7 @@ final class repository_type_form extends moodleform {
*/
function initialise_filepicker($args) {
global $CFG, $USER, $PAGE, $OUTPUT;
static $templatesinitialized;
static $templatesinitialized = array();
require_once($CFG->libdir . '/licenselib.php');
$return = new stdClass();
@ -2691,16 +2691,19 @@ function initialise_filepicker($args) {
// JavaScript a lot, the key NEEDS to be the repository id.
$return->repositories[$repository->id] = $meta;
// Register custom repository template if it has one
if(method_exists($repository, 'get_template')) {
$templates[$meta->type] = $repository->get_template();
if(method_exists($repository, 'get_upload_template') && !array_key_exists('uploadform_' . $meta->type, $templatesinitialized)) {
$templates['uploadform_' . $meta->type] = $repository->get_upload_template();
$templatesinitialized['uploadform_' . $meta->type] = true;
}
}
if (!$templatesinitialized) {
// we need to send filepicker templates to the browser just once
if (!array_key_exists('core', $templatesinitialized)) {
// we need to send each filepicker template to the browser just once
$fprenderer = $PAGE->get_renderer('core', 'files');
$templates = array_merge($templates, $fprenderer->filepicker_js_templates());
$templatesinitialized['core'] = true;
}
if (sizeof($templates)) {
$PAGE->requires->js_init_call('M.core_filepicker.set_templates', array($templates), true);
$templatesinitialized = true;
}
return $return;
}