Working on a plugin that generates a user account and a blog, and gives that user permissions in that blog. One of the tiny little details required is that the blog have a template assigned. Obviously I have the template in mind and could just hard-code it, but I wanted it to be flexible via the plugin's settings. So here ya go!
Starting with the obvious, let's make a setting for picking a template
PHP:
function GetDefaultSettings( & $params ) | |
{ | |
$default_settings = array( | |
'select_template' => array( | |
'label' => $this->T_('Template'), | |
'type' => 'select', | |
'options' => $this->template_options(), | |
'defaultvalue' => $this->template_default(), | |
), | |
); | |
return $default_settings; | |
} |
Obviously enough we'll need functions to create the options array and select the default value.
PHP:
function template_options() | |
{ | |
global $DB; | |
$templates = $DB->get_results("SELECT template_ID, template_name FROM T_templates__template ORDER BY template_name ASC"); | |
$template_options = array(); | |
foreach( $templates as $template ) | |
{ | |
$key = $template->template_ID; | |
$template_options[$key] = $template->template_name; | |
} | |
return $template_options; | |
} | |
| |
function template_default() | |
{ | |
global $DB; | |
$query = "SELECT template_ID from T_templates__template ORDER BY template_ID DESC LIMIT 1"; | |
$template_default = $DB->get_var( $query ); | |
return $template_default; | |
} |
And there you go! The templates are ordered alphabetically in the dropdown list, and the default selected template happens to be the last one installed. For my current application the preferred template is not a core template, so it gets installed after all others. This plugin uses extra events, which do not get recognized during installation so I have to go in and save the settings to get that to happen. Which means these 2 funcs cause my custom template to be the default value for the template selector. Yay internets!
